已知二叉树先序遍历中序遍历求其后序遍历、重建二叉树

水深无声 2022-08-09 01:53 294阅读 0赞

已知二叉树先序遍历中序遍历求其后序遍历

  1. (注:已知中序遍历序列和剩下两种遍历序列中的一种都可以确定二叉树,即可得到另一种遍历序列, 但是已知前序遍历和后序遍历序列并不能唯一确定二叉树,例如:preorderAB postorderBA,我们不能确定BA的左子还是右子。)
  2. 二叉树的先序遍历的第一个元素总是树的根结点,而在中序遍历中,根结点在序列的中间,其左右两边分别是根结点的左右子树。
  3. 因此我们可以根据先序遍历确定根结点,然后在中序遍历中扫描根结点,同时可得到左右子树的中序遍历和先序遍历,递归此过程,即可获得二叉树的所有结点。
  4. 而后序遍历即为根结点左子树的后序遍历+右子树的后序遍历+根结点。
  5. 递归实现思路非常简单,C++代码:
  6. string getpostorder(char* preorder, char* inorder, int length){
  7. string str = "";
  8. char* rootInorder; //根结点在中序遍历中的位置
  9. int leftlength; //左子树长度
  10. if (length <= 0||preorder==NULL||inorder==NULL)//递归边界条件
  11. return "";
  12. else{
  13. leftlength = 0;
  14. rootInorder = inorder;
  15. //扫描根结点
  16. while (leftlength < length&& *rootInorder != preorder[0]){
  17. leftlength++;
  18. rootInorder++;
  19. }
  20. if (rootInorder == inorder+length-1 && *rootInorder != preorder[0])
  21. throw exception("Invalid input.");
  22. //递归
  23. str = getpostorder(preorder + 1,inorder,leftlength)+getpostorder(preorder+leftlength+1,inorder+leftlength+1,length-leftlength-1);
  24. str.push_back(preorder[0]);
  25. return str;
  26. }
  27. }

已知二叉树先序遍历中序遍历重建二叉树

  1. 二叉树的重建,也是运用递归方法思想,首先建立根结点,然后递归构建出其左右子树,递归边界条件为先序遍历和中序遍历只有一个元素的时候。
  2. 二叉树结点一般结构为:
  3. typedef struct BinaryTreeNode{
  4. char data;
  5. struct BinaryTreeNode *left;
  6. struct BinaryTreeNode *right;
  7. }BiTreeNode;

重建代码:

  1. BinaryTreeNode *Construct(char* preorder,char* inorder, int length){
  2. if (preorder == NULL || inorder == NULL || length <= 0)
  3. return NULL;
  4. return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);
  5. }
  6. //递归函数 前两个参数和后两个参数分别确定了某子树先序遍历和中序遍历序列
  7. //参数为指针,操作方便
  8. BinaryTreeNode *ConstructCore(char* startPreorder, char* endPreorder, char* startInorder, char* endInorder){
  9. //构建根节点
  10. char rootValue = startPreorder[0];
  11. BinaryTreeNode* root = new BinaryTreeNode();
  12. root->left = root->right = NULL;
  13. root->data = rootValue;
  14. if (startPreorder == endPreorder){
  15. if (startInorder == endInorder && *startPreorder == *startInorder){
  16. return root;
  17. }
  18. else
  19. throw exception("Invalid input.");
  20. }
  21. char* rootInorder = startInorder;
  22. //扫描中序遍历寻找根结点
  23. while (rootInorder <= endInorder && *rootInorder != *startPreorder)
  24. ++rootInorder;
  25. if (rootInorder==endInorder && *rootInorder!=rootValue)
  26. throw exception("Invalid input.");
  27. //这两个变量用于确定左右子树
  28. int leftLength = rootInorder - startInorder;
  29. char* leftPreorderEnd = startPreorder + leftLength;
  30. if (leftLength > 0){
  31. //构建左子树
  32. root->left = ConstructCore(startPreorder+1,leftPreorderEnd,startInorder,rootInorder-1);
  33. }
  34. if (leftLength < endPreorder - startPreorder){//右子树
  35. root->right = ConstructCore(leftPreorderEnd+1,endPreorder,rootInorder+1,endInorder);
  36. }
  37. return root;
  38. }

加上main()函数之后的完整代码:

  1. #include<iostream>
  2. #include<string>
  3. #include<cstring>
  4. #include<stack>
  5. using namespace std;
  6. typedef struct BinaryTreeNode{
  7. char data;
  8. struct BinaryTreeNode *left;
  9. struct BinaryTreeNode *right;
  10. }BiTreeNode;
  11. BinaryTreeNode *ConstructCore(char* startPreorder, char* endPreorder, char* startInorder, char* endInorder){
  12. char rootValue = startPreorder[0];
  13. BinaryTreeNode* root = new BinaryTreeNode();
  14. root->left = root->right = NULL;
  15. root->data = rootValue;
  16. if (startPreorder == endPreorder){
  17. if (startInorder == endInorder && *startPreorder == *startInorder){
  18. return root;
  19. }
  20. else
  21. throw exception("Invalid input.");
  22. }
  23. char* rootInorder = startInorder;
  24. while (rootInorder <= endInorder && *rootInorder != *startPreorder)
  25. ++rootInorder;
  26. if (rootInorder==endInorder && *rootInorder!=rootValue)
  27. throw exception("Invalid input.");
  28. int leftLength = rootInorder - startInorder;
  29. char* leftPreorderEnd = startPreorder + leftLength;
  30. if (leftLength > 0){
  31. root->left = ConstructCore(startPreorder+1,leftPreorderEnd,startInorder,rootInorder-1);
  32. }
  33. if (leftLength < endPreorder - startPreorder){
  34. root->right = ConstructCore(leftPreorderEnd+1,endPreorder,rootInorder+1,endInorder);
  35. }
  36. return root;
  37. }
  38. BinaryTreeNode *Construct(char* preorder,char* inorder, int length){
  39. if (preorder == NULL || inorder == NULL || length <= 0)
  40. return NULL;
  41. return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);
  42. }
  43. //递归遍历重建的二叉树
  44. string posttravel(BinaryTreeNode* root){
  45. string str="";
  46. if (root != NULL){
  47. str = posttravel(root->left) + posttravel(root->right);
  48. str.push_back(root->data);
  49. return str;
  50. }
  51. else
  52. return "";
  53. }
  54. string getpostorder(char* preorder, char* inorder, int length){
  55. string str = "";
  56. char* rootInorder;
  57. int leftlength;
  58. if (length <= 0||preorder==NULL||inorder==NULL)
  59. return "";
  60. else{
  61. leftlength = 0;
  62. rootInorder = inorder;
  63. while (leftlength < length&& *rootInorder != preorder[0]){
  64. leftlength++;
  65. rootInorder++;
  66. }
  67. if (rootInorder == inorder+length-1 && *rootInorder != preorder[0])
  68. throw exception("Invalid input.");
  69. str = getpostorder(preorder + 1,inorder,leftlength)+getpostorder(preorder+leftlength+1,inorder+leftlength+1,length-leftlength-1);
  70. str.push_back(preorder[0]);
  71. return str;
  72. }
  73. }
  74. int main(){
  75. string postorder;
  76. char preorder[100], inorder[100];
  77. cout << "Input preoder & inorder of the binary tree:";
  78. cin >> preorder >> inorder;
  79. BinaryTreeNode* root=Construct(preorder,inorder,strlen(preorder));
  80. //postorder = posttravel(root);//此函数为递归后序遍历重建的二叉树
  81. postorder = getpostorder(preorder, inorder, strlen(preorder));
  82. cout << postorder << endl;
  83. system("pause");
  84. return 0;
  85. }

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读