First order, middle order, sequence, known for the third party

Source: Internet
Author: User

Today to summarize the next two fork tree pre-order, middle sequence, order traversal mutual seeking method, that is, if you know the two traversal, how to find a third traversal method, the more stupid method is to draw out the binary tree, and then according to a variety of different characteristics of the traversal, can also be programmed to find out, the following we separately explained.

First, we look at the characteristics of the pre-order, the middle order, and the sequential traversal:
Pre-order Traversal:
1. Access the root node
2. Pre-sequence traversal Zuozi
3. Pre-sequence traversal right subtree
Middle Sequence Traversal:
1. Middle Sequence Traversal Zuozi
2. Access the root node
3. The middle sequence traverses the right sub-tree
Post-post traversal:
1. Post-Zuozi traversal
2. Post-the-loop traversal of right sub-tree
3. Access the root node

First, known pre-order, middle sequence traversal

Cases:

Pre-sequence traversal: Gdafemhz

Middle Sequence Traversal: ADEFGHMZ

The drawing tree seeking method:
The first step, based on the characteristics of the pre-order traversal, we know that the root node is G.

The second step is to observe the sequence traversal adefghmz. The adef on the left side of the root node is necessarily the left subtree of root, and the hmz on the right of G must be the right subtree of root.

The third step is to observe the left subtree adef, and the root node of Zuozi must be the root of the tree leftchild. In the pre-sequence traversal, the root leftchild of the tree is located after root, so the root node of the Zuozi is D.

The fourth step, the same truth, root in the right subtree node HMZ root node can also be obtained through the pre-sequence traversal. In the pre-sequence traversal, it must be that all the left subtree nodes of root and root are traversed before the right subtree is traversed, and the first node of the left subtree that is traversed is the root node of Zuozi. Similarly, the first node of the right subtree that is traversed is the root node of the right child tree.

The fifth step, observed that the above process is recursive. First find the root node of the current tree, then divide it into left dial hand tree, right subtree, and then go to the left subtree to repeat the above procedure, and then go to the right subtree to repeat the above procedure. Finally, you can restore a tree. The process of this step recursion can be expressed concisely as follows:

1 Determine the root, determine the left subtree, and determine the right subtree.

2 recursion in record.

3 recursively in the right subtree.

4 Prints the current root.

So, we can draw the shape of this binary tree:

The original pictures of Asahi elder brother, very successful, classic classic

Then, according to the ergodic rules, we can know that the order of sequential traversal is: AEFDHZMG

Programming Method: (according to the above thinking, write recursive program)

 1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 5 struct TreeNode 6 {7 Struc T treenode* left; 8 struct treenode* right; 9 Char elem;10};11 binarytreefromorderings (char* inorder, char* preorder, int length) (length = = 0) 1 5 {//cout<< "invalid length"; return;18}19 treenode* node = new Treenode;//noice that [NE W] should is written out.20 Node->elem = *preorder;21 int rootindex = 0;22 for (; rootindex < length; rootindex+ +) (Inorder[rootindex] = = *preorder) break;26}27//left28 binarytreefromorderings (Inord Er, preorder +1, rootindex);//right30 binarytreefromorderings (inorder + rootindex + 1, preorder + Rootindex + 1, le Ngth-(Rootindex + 1)) cout<<node->elem<<endl;32 return;33}34 int main (int argc, char* argv[ ]) PNs ("Hello world!\n"), char* pr= "Gdafemhz", char* in= "Adefghmz"; 41 42    Binarytreefromorderings (in, PR, 8); ("\ n"); return 0;46} 

The result of the output is: AEFDHZMG

Second, the known sequence and post-order traversal, to seek pre-sequence traversal

is still the above problem, this time we only give the middle order and post-sequence traversal:

Middle Sequence Traversal: ADEFGHMZ

Post-post traversal: AEFDHZMG

The drawing tree seeking method:
The first step, according to the characteristics of the post-order traversal, we know that the next step to traverse the last node is the root node, that is, the root node is G.

The second step is to observe the sequence traversal adefghmz. The adef on the left side of the root node is necessarily the left subtree of root, and the hmz on the right of G must be the right subtree of root.

The third step is to observe the left subtree adef, and the root node of Zuozi must be the root of the tree leftchild. In the pre-sequence traversal, the root leftchild of the tree is located after root, so the root node of the Zuozi is D.

The fourth step, the same truth, root in the right subtree node HMZ root node can also be obtained through the pre-sequence traversal. In the pre-order traversal, it must be that all the left subtree nodes of root and root are traversed before the right subtree is traversed, and the first node of the left subtree that is traversed is the root node of Zuozi. Similarly, the first node of the right subtree that is traversed is the root node of the right child tree.

The fifth step, observed that the above process is recursive. First find the root node of the current tree, then divide it into left dial hand tree, right subtree, and then go to the left subtree to repeat the above procedure, and then go to the right subtree to repeat the above procedure. Finally, you can restore a tree. The process of this step recursion can be expressed concisely as follows:

1 Determine the root, determine the left subtree, and determine the right subtree.

2 recursion in record.

3 recursively in the right subtree.

4 Prints the current root.

In this way, we can draw the shape of a two-fork tree, as shown here, and not repeat it here.

So, the pre-sequence traversal: Gdafemhz

Programming: ( and verifying that our results are correct)

#include <iostream> #include <fstream> #include <string>struct treenode{struct treenode* left;    struct treenode* right; char Elem;};    treenode* binarytreefromorderings (char* inorder, char* aftorder, int length) {if (length = = 0) {return NULL;    } treenode* node = new Treenode;//noice that [new] should is written out.    Node->elem = * (aftorder+length-1);    std::cout<<node->elem<<std::endl;    int rootindex = 0; for (; rootindex < length; rootindex++)//a Variation of the loop {if (inorder[rootindex] = = * (aftorder+length-    1)) break;    } node->left = Binarytreefromorderings (inorder, Aftorder, Rootindex);        Node->right = binarytreefromorderings (inorder + rootindex + 1, Aftorder + rootindex, Length-(rootindex + 1)); return node;}        int main (int argc, char** argv) {char* af= "aefdhzmg";     char* in= "ADEFGHMZ";     Binarytreefromorderings (In, AF, 8);    printf ("\ n"); return 0;}

Output Result: Gdafemhz

First order, middle order, sequence, known for the third party

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.