Binary Tree pre-order, middle-order, and post-order traversal and Creation

Source: Internet
Author: User

# Include <iostream>
# Include <string>
# Include <stack>
Using namespace std;
Struct node;
Typedef node * pNode;
Struct node {
Char data;
PNode left, right;
};
String line;
String: iterator it;
// Create a binary tree using the extension sequence in the forward Sequence
Void plant (pNode & root)
{
If (it = line. end ())
Return;
Char data = * it ++;
If (data = '/')
Root = NULL;
Else {
Root = new node ();
Root-> data = data;
Plant (root-> left );
Plant (root-> right );
}
}
// First-order traversal
Void pre_order (pNode root)
{
Stack <pNode> s;
While (root! = NULL |! S. empty ())
{
If (root! = NULL ){
Cout <root-> data <"";
S. push (root );
Root = root-> left;
} Else {
Root = s. top ();
S. pop ();
Root = root-> right;
}
}
}
// Sequential Traversal
Void in_order (pNode root)
{
Stack <pNode> s;
While (root! = NULL |! S. empty ())
{
If (root! = NULL ){
S. push (root );
Root = root-> left;
} Else {
Root = s. top ();
S. pop ();
Cout <root-> data <"";
Root = root-> right;
}
}
}
// Type of the stack pressure parameter, including local variables and row numbers
Struct stack_arg {
Stack_arg (pNode _ root, int _ line)
: Root (_ root), line (_ line ){}
PNode root;
Int line;
};
// Post-order traversal
Void post_order (pNode root)
{
Stack <stack_arg> s;
S. push (stack_arg (root, 1 ));
While (! S. empty ())
{
Switch (s. top (). line ){
Case 1:
If (s. top (). root = NULL)
S. pop ();
Else
S. top (). line = 2;
Break;
Case 2:
S. top (). line = 3;
S. push (stack_arg (s. top (). root-> left, 1 ));
Break;
Case 3:
S. top (). line = 4;
S. push (stack_arg (s. top (). root-> right, 1 ));
Break;
Case 4:
Cout <s. top (). root-> data <"";
S. pop ();
Break;
}
}
}
Int main ()
{
Cin> line;
It = line. begin ();
PNode root = NULL;
Plant (root );
Cout <"sequential traversal" <endl;
Pre_order (root );
Cout <endl;
Cout <"sequential traversal" <endl;
In_order (root );
Cout <endl;
Cout <"post-order traversal" <endl;
Post_order (root );
Cout <endl;
System ("pause ");
Return 0;
}
/*
Input
ABDI // J // EK // SCSI // CFM // N // GO // P //
Output
Sequential traversal:
A B D I J E K L Q C F M N G O P
Sequential Traversal
I D J B K E Q L A M F N C O G P
Post-order traversal
I J D K Q L E B M N F O P G C

 

//////////////////////////////////////// //////////////////////////////
How do I find the middle-order traversal?
By analyzing the three traversal methods of the same binary tree, we can summarize the methods to quickly restore the binary tree from the pre-order, middle-order, or middle-order and post-order traversal results.
Binary Tree is the most commonly used data structure, which is widely used. There are three ways to traverse a binary tree: Forward traversal, central traversal, and backward traversal. The first traversal order is NLR, that is, the first root node, then the left and right subtree; the middle traversal order is: LNR first the left subtree, then the root node and the right subtree; the post-order traversal order is: LRN first left subtree, then right subtree, and root node. A binary tree can be uniquely identified by pre-and mid-order traversal, and by the middle-and post-order traversal sequences. However, a binary tree cannot be uniquely identified by the pre-and post-order traversal sequences.
The binary tree further limits the binary tree: the root node's weight is greater than (or less than) the weight of all nodes in the left subtree; the root node's weight is less than (or greater) the weights of all nodes in the right subtree.
So how can we quickly restore a binary tree based on the relationship between the three traversal sequences and the binary sorting tree? The following describes how to quickly restore a binary tree based on the pre-and mid-order traversal sequences of Binary Trees.
1. Restore a binary tree from a given pre-order or mid-order or post-Order Sequence
Example: pre-order sequence: ABDECFGH middle-order sequence: DEBACGFH (post-order sequence: EDBGHFCA)
(1) assign weights to each node in the central sequence from small to large from left to right, as shown below:
D (1) E (2) B (3) A (4) C (5) G (6) F (7) H (8)
(2) The sequence read during restoration is a forward sequence. Each node value and the corresponding weight value are read from left to right;
(3) construct a binary sorting tree based on the construction rules of the binary sorting tree based on the weights given in step 1st of the read sequence. The first read node is the root node, and the other nodes are the nodes in the left and right subtree respectively. Set the root node to TT and the weight to NN. The read node is SS and the weight is MM.
(4) When the SS is inserted into the left or right subtree of TT, the rule is still followed. (3) until the left or right subtree is empty.
(5) When the read sequence ends, the binary tree is restored successfully.
6) for the binary tree restored from the central sequence and the post sequence, the read sequence is the post sequence, which is read from the right to the left. The construction rules are the same as above. The restoration result is exactly the same as the preceding result.

2. basis for determining the restoration method
During binary tree traversal, In the middle sequence, all the nodes in the left subtree of the root node are on the left of the root node, all nodes in the right subtree of the root node are on the right of the root node. This feature is exactly the same as that of the binary sorting tree, the pre-order sequence reads from left to right, which is exactly the same as the order for traversing the binary tree; the post-order sequence reads from right to left, it is restored in the order of the root node, right subtree, and left subtree.
(1) If a binary tree has N nodes (N is a positive integer greater than 1), we use the restoration method to assign the weights to the N nodes in the sequence 1, 2... N, set the root node's weight to M (1
(2) According to the binary tree traversal rules, the weight is 1, 2... The M-1 node is the node in the left subtree of the root node, and the weight is M + 1 ,... N is the node in the right subtree of the root node.
(3) divide the N nodes into three subsets AA = (1, 2... M-1) BB = (M) CC = (M + 1 ,... N), because the first read node of the first sequence must be the root node of the binary root, so BB is the root node, the AA set is the left subtree, and the CC set is the right subtree.
(4) read the nodes in the pre-ordered sequence and recursively restore the left subtree corresponding to BB and the right subtree corresponding to CC, finally, the three Subtrees are merged into a binary sorting tree of the right subtree that uses BB as the root node, AA as the root node as the BB, and CC as the root node as the BB.
(5) Likewise, it can be concluded that the rules for restoring binary trees from the central sequence and the backward sequence are also established.
(6) In the restoration process, the sequence of reading sequences also follows the rule of first root node and then subtree.
3. Conclusion
In some applications of Binary Trees, such as balanced binary trees and red/black trees, we often need to observe the form of Binary Trees, determine and adjust them. Quickly restoring a binary tree based on the nature of the traversal sequence and binary sorting tree is of great help to the research.

 

We can obtain the extension sequence ABD/E ///C // FG/H.

Followed by ED ## B ### G ## HFCA

 

Void PreCreate (Node * p)

{

If (line = end)

Return;

If (line = "/")

P = NULL;

Else {

P = new NOde ();

P-> data = line;

PreCreate (p-> Left );

PreCreate (p-> Right );

}

}

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.