Build a binary tree by means of central traversal and first/second Traversal

Source: Internet
Author: User
1. Problems

Can a binary tree be uniquely identified based on the two traversal sequences of a given binary tree (such as the first, middle, and last?

2. Theoretical Analysis

An important basic knowledge of data structures is the ability to combine two different traversal sequences (three types: First Order + middle order, first order + back order, middle order + back order ), uniquely identifies a binary tree. Then, the binary tree is reconstructed based on different traversal sequences (first, middle, and last) of the binary tree.Obviously, none of these three combinations can uniquely determine a binary tree. The first and second orders cannot uniquely determine a binary tree. Below are the proofs and conclusions about this problem.

① Given the forward and middle sequence of a binary tree node, the binary tree can be uniquely identified.
Proof: Because the first element of the first sequence is the root node, this element divides the sequence in the binary tree into two parts., Left (assuming there are l elements) indicates the left subtree. If there are no elements on the left, the left subtree is empty. Right (assuming there are R elements) indicates the right subtree. If it is null, the right subtree is empty.Traverse the "Root-left subtree-right subtree" Order in the forward order, then, the left subtree is constructed from the L node sequence starting from the second element of the first sequence and the L node sequence on the left of the root of the middle sequence, the right subtree is constructed from the last r element sequence of the first sequence and the r element sequence on the right of the root of the middle sequence.
That is, the logic order is: 1. The root node of the tree can be uniquely identified through the first sequence; 2. Combine the central sequence with the root node to determine the left and right subtree; 3. The sequence of elements of the left and right Subtrees can be uniquely determined based on the sequence of elements in the first and middle orders; 4. Construct the tree in sequence based on the identified root node and left/right subtree.
② A binary tree can be uniquely identified by the central sequence and the first sequence, but a binary tree cannot be uniquely identified by the first sequence and the latter sequence, because the left and right Subtrees cannot be determined..
Inverse example: Any node only has a binary tree of the Left subtree and any node only has a binary tree of the right subtree. The former sequence is the same, and the latter sequence is the same, but it is two different binary trees.
Example: 2 2
/\
1 1
/\
3 3
The first traversal sequence of the Two Binary Trees is 2-1-3, and the latter is 3-1-2. But obviously they are different binary trees, so the binary tree cannot be uniquely identified based on the first sequence and the latter sequence.

③ It has been suggested that a binary tree can be identified by the first and middle sequence of the binary tree. Now it is proved that the middle sequence and the latter sequence of the binary tree can also be uniquely identified by a binary tree.

Proof:
When n = 1, there is only one root node, and the binary tree can be determined by the Central sequence and the post sequence.
When N = m-1, the conclusion is true. When the number of nodes is m-1, the middle sequence and the back sequence can uniquely determine the binary tree. It is proved that when n = m, the conclusion is true.
Set the central sequence to S1, S2 ,..., SM, followed by P1, P2 ,..., PM. Because the last element PM in the Post-order sequence is the root, the nodes equal to the PM can be found in the middle sequence (where the nodes in the binary tree are different) Si (1 ≤ I ≤ m ), because the central sequence is obtained by traversing the middle sequence, Si is the root node, S1, S2 ,..., Si-1 is the central sequence of the Left subtree, while Si + 1, Si + 2 ,..., SM is the middle sequence of the right subtree.
If I = 1, S1 is the root, then the left subtree of the binary tree is empty, and the knots of the right subtree are m-1, then {S2, S3 ,..., SM} and {p1, p2 ,..., Pm-1} can uniquely determine the right subtree, which also determines the binary tree.
If I = m, SM is the root, then the right subtree of the binary tree is empty, and the number of knots in the left subtree is s-1, then {S1, S2 ,..., Sm-1} and {p1, p2 ,..., Pm-1} uniquely identified the left subtree, which also identified the binary tree.
Finally, when 1 <I <m, Si divides the central sequence into {S1, S2 ,..., Si-1} and {Si + 1, Si + 2 ,..., SM }. Since the post-order traversal is "Left subtree-right subtree-root node", {p1, p2 ,..., Pi-1} and {Pi, PI + 1 ,... Pm-1} is the post-sequential traversal sequence of left and right subtree of binary tree. Therefore, {S1, S2 ,..., Si-1} and {p1, p2 ,..., Pi-1} uniquely identifies the left subtree of a binary tree, from {Si + 1, Si + 2 ,..., SM} and {Pi, PI + 1 ,..., The Pm-1} uniquely identifies the right subtree of a binary tree.

That is, the logic order is: 1. The root node of the tree can be uniquely identified through the first sequence; 2. Combine the central sequence with the root node to determine the left and right subtree; 3. The sequence of elements of the left and right Subtrees can be uniquely determined based on the sequence of elements in the first and middle orders; 4. Construct the tree in sequence based on the identified root node and left/right subtree.

3. Build a binary tree based on the first and middle order traversal sequences

Assume that the known binary tree is as follows:

        ___7___       /     \    10        2   /   \      /  4    3      8        \    /         1  11

The result of its first and middle traversal is as follows:

preorder = {7,10,4,3,1,2,8,11}inorder = {4,10,3,1,7,11,8,2}

You need to pay attention to the following important points:

1) The first node that is first traversed is always the root node. For example, if the root node of the Binary Tree in is 7, it is also the first value for sequential traversal.The parent node is always traversed before the child node.

2) We can see that 7 is 4th values (starting from 0) in the middle-order traversal ). The central traversal order is: Left subtree, root node, and right subtree.Therefore, the four knots {,} on the left of 7 belong to the left subtree, while the {, 2} on the right of the root node 7 belongs to the right subtree.

3) the recursive formula can be easily obtained from the above conclusion. After building the root node 7, we can traverse {4,
10, 3, 1} and {11, 8, 2} construct the left and right subtree respectively. At the same time, we need the corresponding first-order traversal results for discovering rules. We can know from the first-order traversal that the first-order traversal of left and right subtree is {10, 4, 3, 1} and {2, respectively,
8, 11 }. The left and right Subtrees are also Binary Trees.RecursionTo solve the problem.

4) The problem about how to obtain the position of the root node in the middle-order traversal is not detailed yet. If a linear scan is used to find the position, it takes O (n) Time for each query. If a binary tree is balanced, the entire algorithm requires O (nlgn) time. If the binary tree is not balanced, O (N ^ 2) is required in the worst case. To improve efficiency, we can considerThe hash table is used to store and find the position of the root node in the middle-order traversal. It only takes O (1) Time for each query. In this way, it takes O (n) Time to build the entire tree.For convenience, an array is used to mark positions. It is also convenient to use a hash table.Note that the Binary Tree node values cannot have the same values.

[CPP]
View plaincopy

  1. Int mapindex [256];
  2. Void maptoindices (INT inorder [], int N)
  3. {
  4. Int I;
  5. For (I = 0; I <n; I ++ ){
  6. Mapindex [inorder [I] = I;
  7. }
  8. }
  9. // Call the maptoindices method before this operation. Pre array first traverses the sequence. Note that the starting position of pre changes during recursion. N indicates the number of nodes, and offset indicates the start position of the subtree.
  10. Struct node * buildinorderpreorder (INT pre [],
  11. Int N, int offset)
  12. {
  13. If (n = 0) return NULL;
  14. Int rootval = pre [0];
  15. Int I = mapindex [rootval]-offset;
  16. Struct node * root = newnode (rootval );
  17. Root-> left = buildinorderpreorder (pre + 1,
  18. I, offset );
  19. Root-> right = buildinorderpreorder (pre + I + 1,
  20. N-i-1, offset + I + 1 );
  21. Return root;
  22. }
  23. // Test code
  24. Void buildinorderpreordertest ()
  25. {
  26. Int pre [] = {7, 10, 4, 3, 1, 2, 8, 11 };
  27. Int in [] = {4, 10, 3, 1, 7, 11, 8, 2 };
  28. Int n = sizeof (in)/sizeof (in [0]);
  29. Int offset = 0;
  30. Maptoindices (in, N );
  31. Struct node * root = buildinorderpreorder (PRE, N, offset );
  32. Traverse (Root );
  33. Putchar ('\ n ');
  34. }


2) Build a binary tree based on post-order traversal and Middle-order traversal

Similar to the preceding principle, the Code is as follows:

[CPP]
View plaincopy

  1. Struct node * buildinorderpostorder (INT post [], int N, int offset)
  2. {
  3. Assert (n> = 0 );
  4. If (n = 0) return NULL;
  5. Int rootval = post [n-1];
  6. Int I = mapindex [rootval]-offset; // the divider's index
  7. Struct node * root = newnode (rootval );
  8. Root-> left = buildinorderpostorder (post, I, offset );
  9. Root-> right = buildinorderpostorder (post + I, n-i-1, offset + I + 1 );
  10. Return root;
  11. }

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.