#include <iostream> #include <string> #include <stack> #include <vector> using namespace std; #define MAXNODE 7 typedef int DATATYPE; typedef struct TreeNode { DATATYPE data; TreeNode *left; TreeNode *right; }TreeNode; TreeNode *mallocBinaryNode(int data) { TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode)); node->data = data; node->left = node->right = NULL; return node; } TreeNode *createBinaryTree(char buf[], int num) { TreeNode *node = mallocBinaryNode(buf[num]); // printf("node_id:%d, node_data:%d\n", num, node->data); if (2 * num <= MAXNODE) { node->left = createBinaryTree(buf, 2 * num); } if (2 * num + 1 <= MAXNODE) { node->right = createBinaryTree(buf, 2 * num + 1); } return node; } void preOrder(TreeNode *node) // 前序遍历,中左右 { if (node != NULL) { cout << node->data << " "; preOrder(node->left); preOrder(node->right); } } void inOrder(TreeNode *node) // 中序遍历,左中又 { if (node != NULL) { inOrder(node->left); cout << node->data << " "; inOrder(node->right); } } void postOrder(TreeNode *node) // 后序遍历,左右中 { if (node != NULL) { postOrder(node->left); postOrder(node->right); cout << node->data << " "; } } void testTree() { TreeNode *root; char buf[] = {0,11,12,13 ,14, 15, 16, 17}; root = createBinaryTree(buf, 1); preOrder(root); cout << endl; inOrder(root); cout << endl; postOrder(root); cout << endl; }