根据树的先序和中序来创建一棵树

只知道先序序列和后序序列是无法求出唯一的树,所以不做讨论。

  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. using namespace std;  
  5.   
  6. struct BinaryTreeNode  
  7. {  
  8.     char c;  
  9.     BinaryTreeNode *lchild, *rchild;  
  10.     BinaryTreeNode()  
  11.     {  
  12.         lchild = NULL, rchild = NULL;  
  13.     }  
  14. };  
  15. struct BinaryTreeNode *root1,*root2;  
  16.   
  17. char preorder[100], inorder[100], postorder[100];  
  18.   
  19. void preSearch(BinaryTreeNode *root)   //先序遍历树  
  20. {  
  21.     if(root != NULL)  
  22.     {  
  23.         printf("%c", root->c);  
  24.         preSearch(root->lchild);  
  25.         preSearch(root->rchild);  
  26.     }  
  27.     return ;  
  28. }  
  29.   
  30. void midSearch(BinaryTreeNode *root)   //中序遍历树  
  31. {  
  32.     if(root != NULL)  
  33.     {  
  34.         midSearch(root->lchild);  
  35.         printf("%c", root->c);  
  36.         midSearch(root->rchild);  
  37.     }  
  38.     return ;  
  39. }  
  40.   
  41. void postSearch(BinaryTreeNode *root)  //后序遍历树  
  42. {  
  43.     if(root != NULL)  
  44.     {  
  45.         postSearch(root->lchild);  
  46.         postSearch(root->rchild);  
  47.         printf("%c", root->c);  
  48.     }  
  49.     return ;  
  50. }  
  51.   
  52. void BuildTreeFromPreAndMid(BinaryTreeNode * &root, int ll, int lr, int len, int &now)//根据中序和先序求树  
  53. {  
  54.     root = new BinaryTreeNode();  
  55.     root->c = *(preorder + now);  
  56.     int pos = (int)(strchr(inorder, *(preorder + now)) - inorder); //查找字符串中首次出现某个字符的位置   
  57.     now++;  
  58.     if(now >= len)  
  59.         return ;  
  60.     if(pos - 1 >= ll)  
  61.     {  
  62.         BinaryTreeNode *t = new BinaryTreeNode();  
  63.         root->lchild = t;  
  64.         BuildTreeFromPreAndMid(root->lchild, ll, pos - 1, len, now);  
  65.     }  
  66.     if(pos + 1 <= lr)  
  67.     {  
  68.         BinaryTreeNode *t = new BinaryTreeNode();  
  69.         root->rchild = t;  
  70.         BuildTreeFromPreAndMid(root->rchild, pos + 1, lr, len, now);  
  71.     }  
  72. }  
  73.   
  74. void BuildTreeFromPostAndMid(BinaryTreeNode * &root, int ll, int lr, int len, int &now)//根据中序和后序求树  
  75. {  
  76.     root = new BinaryTreeNode();  
  77.     root->c = *(postorder + now);  
  78.     int pos = (int)(strchr(inorder, *(postorder + now)) - inorder);  
  79.     now--;  
  80.     if(now < 0)  
  81.         return ;  
  82.     if(pos + 1 <= lr)  
  83.     {  
  84.         BinaryTreeNode *t = new BinaryTreeNode();  
  85.         root->rchild = t;  
  86.         BuildTreeFromPostAndMid(root->rchild, pos + 1, lr, len, now);  
  87.     }  
  88.     if(pos - 1 >= ll)  
  89.     {  
  90.         BinaryTreeNode *t = new BinaryTreeNode();  
  91.         root->lchild = t;  
  92.         BuildTreeFromPostAndMid(root->lchild, ll, pos - 1, len, now);  
  93.     }  
  94. }  
  95.   
  96. //释放二叉树  
  97. inline void DeleteBinaryTree(BinaryTreeNode * &root)  
  98. {  
  99.     if(root)  
  100.     {  
  101.         DeleteBinaryTree(root->lchild);    //释放左子树  
  102.         DeleteBinaryTree(root->rchild);    //释放右子树  
  103.         delete root;          //释放根结点  
  104.     }  
  105. }  
  106.   
  107. int main(void)  
  108. {  
  109.     gets(preorder);  
  110.     gets(inorder);  
  111.     //gets(postorder);  
  112.     int now = 0;  
  113.     BuildTreeFromPreAndMid(root1, 0, strlen(preorder) - 1, strlen(preorder), now);  
  114.   
  115.     //int now2 = strlen(postorder)-1;  
  116.     //BuildTreeFromPostAndMid(root2, 0, strlen(postorder) - 1, strlen(postorder), now2);  
  117.   
  118.     postSearch(root1);  
  119.     puts("");  
  120.     DeleteBinaryTree(root1);  
  121.     /*preSearch(root2); 
  122.     puts(""); 
  123.     DeleteBinaryTree(root2);*/  
  124.     return 0;  
  125. }  
原文地址:https://www.cnblogs.com/zhanglanyun/p/2636246.html