Create a tree based on the first and middle order of the tree

Source: Internet
Author: User

We only know that the first sequence and the latter sequence cannot find the unique tree, so we will not discuss it.

 

[CPP]View plaincopy

  1. # Include <iostream>
  2. # Include <cstdio>
  3. # Include <cstring>
  4. Using namespace STD;
  5. Struct binarytreenode
  6. {
  7. Char C;
  8. Binarytreenode * lchild, * rchild;
  9. Binarytreenode ()
  10. {
  11. Lchild = NULL, rchild = NULL;
  12. }
  13. };
  14. Struct binarytreenode * root1, * root2;
  15. Char preorder [100], inorder [100], postorder [100];
  16. Void presearch (binarytreenode * root) // first traverse the tree
  17. {
  18. If (root! = NULL)
  19. {
  20. Printf ("% C", root-> C );
  21. Presearch (root-> lchild );
  22. Presearch (root-> rchild );
  23. }
  24. Return;
  25. }
  26. Void midsearch (binarytreenode * root) // The middle-order traversal tree.
  27. {
  28. If (root! = NULL)
  29. {
  30. Midsearch (root-> lchild );
  31. Printf ("% C", root-> C );
  32. Midsearch (root-> rchild );
  33. }
  34. Return;
  35. }
  36. Void postsearch (binarytreenode * root) // traverse the tree in descending order
  37. {
  38. If (root! = NULL)
  39. {
  40. Postsearch (root-> lchild );
  41. Postsearch (root-> rchild );
  42. Printf ("% C", root-> C );
  43. }
  44. Return;
  45. }
  46. Void buildtreefrompreandmid (binarytreenode * & root, int LL, int LR, int Len, Int & now) // calculate the tree in the middle and first order
  47. {
  48. Root = new binarytreenode ();
  49. Root-> C = * (preorder + now );
  50. Int Pos = (INT) (strchr (inorder, * (preorder + now)-inorder); // you can specify the position where a character first appears in a string.
  51. Now ++;
  52. If (now> = Len)
  53. Return;
  54. If (POS-1> = LL)
  55. {
  56. Binarytreenode * t = new binarytreenode ();
  57. Root-> lchild = T;
  58. Buildtreefrompreandmid (root-> lchild, ll, pos-1, Len, now );
  59. }
  60. If (Pos + 1 <= LR)
  61. {
  62. Binarytreenode * t = new binarytreenode ();
  63. Root-> rchild = T;
  64. Buildtreefrompreandmid (root-> rchild, POS + 1, LR, Len, now );
  65. }
  66. }
  67. Void buildtreefrompostandmid (binarytreenode * & root, int LL, int LR, int Len, Int & now) // calculate the tree in the middle and back order
  68. {
  69. Root = new binarytreenode ();
  70. Root-> C = * (postorder + now );
  71. Int Pos = (INT) (strchr (inorder, * (postorder + now)-inorder );
  72. Now --;
  73. If (now <0)
  74. Return;
  75. If (Pos + 1 <= LR)
  76. {
  77. Binarytreenode * t = new binarytreenode ();
  78. Root-> rchild = T;
  79. Buildtreefrompostandmid (root-> rchild, POS + 1, LR, Len, now );
  80. }
  81. If (POS-1> = LL)
  82. {
  83. Binarytreenode * t = new binarytreenode ();
  84. Root-> lchild = T;
  85. Buildtreefrompostandmid (root-> lchild, ll, pos-1, Len, now );
  86. }
  87. }
  88. // Release a binary tree
  89. Inline void deletebinarytree (binarytreenode * & root)
  90. {
  91. If (Root)
  92. {
  93. Deletebinarytree (root-> lchild); // release the left subtree
  94. Deletebinarytree (root-> rchild); // release the right subtree
  95. Delete root; // release the root node
  96. }
  97. }
  98. Int main (void)
  99. {
  100. Gets (preorder );
  101. Gets (inorder );
  102. // Gets (postorder );
  103. Int now = 0;
  104. Buildtreefrompreandmid (root1, 0, strlen (preorder)-1, strlen (preorder), now );
  105. // Int now2 = strlen (postorder)-1;
  106. // Buildtreefrompostandmid (root2, 0, strlen (postorder)-1, strlen (postorder), now2 );
  107. Postsearch (root1 );
  108. Puts ("");
  109. Deletebinarytree (root1 );
  110. /* Presearch (root2 );
  111. Puts ("");
  112. Deletebinarytree (root2 );*/
  113. Return 0;
  114. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.