Data Structure Foundation post-order traversal and middle sequence traversal restore binary tree

Source: Internet
Author: User

"Problem description"

Two-fork Tree
A
/       /
B C
/   /   /   /
D E F G
/ / / / / / / /
H I J K M N O P
The result of the post-sequential traversal is: HIDJKEBMNFOPGCA, we call
The result of the middle sequence traversal is: HDIBJEKAMFNCOGP, which we call mid

"Algorithmic thinking"
(1) Pi points to the last character of Post
(2) Use PI to take a single character from post to PC
(3) Find the location of the PC in mid mi
(4) Determine the relationship between PC and previous character according to MI (left child/right child/no relationship)
(5) Pi-1
(6) Repeat (2) ~ (5) steps until Pi < 0

As you can see, the key to the problem is step (4), that is, how to determine the relationship between the PC and the previous character.
Here we need to use two auxiliary structures:
(1) A linked list that stores the helper structure
(2) A helper structure that records the subscripts of each node in mid
List of the list,helper we can use STL's structure is as follows

struct Helper {
treenode* node;
int index;
Public:
Helper (treenode* pnode, int idx)
: node (pnode), index (IDX) {}
};
Of course, the nodes of a binary tree should also have:

struct TreeNode {
 char    data;
 treenode*  lchild;
 treenode*  rchild;
Public:
 TreeNode (char c): Data (c), Lchild (0), rchild (0) {}
};

Well, let's take a step-by-step look at how to solve the problem of restoring a binary tree.
(1) (A, 7)
Take the first character of the post, then put the helper into the list and construct a list of the initial environment
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Post:h I D J K E B M N F O P G C A
Mid:h D I B J E K A M F N C O G P
"List"
Nod 0 A 0
Idx-1 7 15
Cur ^
Nxt
Cur, NXT is a pointer to an element in list, with two elements representing a boundary
(2) (C, 11)
Take post 13th character, according to List to determine who left child/right child
As you can see, Pc=c, whose subscript mi is 11 in mid, is abbreviated (C, 11) and is so brief later
(c, 11) on the right side (a, 7), because one > 7, so C is the right child of a, and inserted into the list, note
Cur the pointer changes.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Post:h I D J K E B M N F O P G C A
Mid:h D I B J E K A M F N C O G P
"List"
Nod 0 A C 0
Idx-1 7 11 15
Cur ^
Nxt
(3) (G, 13)
(g, 13) to the right of (c, 11), since the > 11, so G is the right child of C, inserted in list
"List"
Nod 0 A C G 0
Idx-1 7 11 13 15
Cur ^
Nxt
Omitted below, because this and my other article "pre-order-middle order binary tree restore" very much like, but a former
One from the back forward, there is a first to determine the left child, a first to determine the right child
Let's talk about how to restore B when the right subtree of a is restored. It also explains how to restore the left child's method.

(12) (B, 3)
This time, the list should be like this.
"List"
Nod 0 A M 0
Idx-1 7 8 15
Cur ^
Nxt
(B, 3) not to the right of (M, 8), NXT = cur, cur--
"List"
Nod 0 A M 0
Idx-1 7 8 15
Cur ^
NXT ^
(B, 3) not in the middle of (A, 7), (M, 8), delete (M, 8)
"List"
Nod 0 A 0
Idx-1 7 15
Cur ^
Nxt
(13) (B, 3)
At this point (B, 3) is not on (A, 7) to the right, NXT = cur, cur--
"List"
Nod 0 A 0
Idx-1 7 15
Cur ^
NXT ^
(b, 3) in the middle of (0,-1), (A, 7), so B is the left child of a, remove a, insert B,cur point B
"List"
Nod 0 B 0
Idx-1 3 15
Cur ^
Nxt

By the way, do not forget to do the corresponding link operation after making sure that the X-node is the Y-node of the left/right child.
The following shows the C + + representation of the algorithm, where we use iterator to represent the cur, nxt pointer. The reason we want to use list is
Because the list does not expire after inserting/deleting elements iterator. And one more thing, because List<>::iterator doesn't support
Random access, so we're going to use NXT, cur two iterator to represent one after the other, otherwise directly with Cur and
Cur-1 on the line, so it's a lot easier.
"Source Code Implementation"
#include <iostream> #include <stack> #include <string> #include <list> using namespace std;
    struct TreeNode {char data;
    treenode* Lchild;
treenode* Rchild;
Public:treenode (char c): Data (c), Lchild (0), rchild (0) {}};
     struct Helper {treenode* node;
int index;
Public:helper (treenode* pnode, int idx): node (pnode), index (IDX) {}};
     int main () {void inordertraversal (treenode* pTree);
     void Postordertraversal (treenode* pTree);
     void Post_mid_restore (String Post, String Mid, treenode*& result);
                /* A//B C//// D E F G///////H I J KM N O P */String Postord
    Er1 = "HIDJKEBMNFOPGCA";
        
   String Midorder1 = "Hdibjekamfncogp";
   String Postorder2 = "Dbfgeca"; String MidOrder2 = "BDAFEGC";
 
   treenode* res = 0;
   Post_mid_restore (Postorder1, Midorder1, res);
   Inordertraversal (RES);
 
   Postordertraversal (RES);
   Post_mid_restore (Postorder2, Midorder2, res);
   Inordertraversal (RES);
   
    Postordertraversal (RES);
Cin.get ();
     } void Print (list 




<span style= "Font-family:times New roman;font-size:18px;" >struct Helper {
 treenode* node;
 int   index;
Public:
 Helper (treenode* pnode, int idx) 
  : node (pnode), index (IDX) {}
};</span>
Of course

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.