Constructing binary tree based on the sequence of middle order and sequential sequence

Source: Internet
Author: User

The algorithm is as follows:
1) First find the root node in the sequence of order,
2) Find the root node position in the sequence, (you can divide the two fork tree into the Saozi right subtree).
3) construct the left subtree in the same way.
4) Construct the right sub-tree in the same way.

The algorithm is as follows:

binarytree* creat_node (char ch) {binarytree* root;
    root = new BinaryTree;
    Root->data = ch;
    Root->lchild = Root->rchild = NULL;
return root; } binarytree* Mid_post_create_pre (char* mid,char*post,int N) {if (!mid| |! post| | n<0) {cout<< "input error.
        "<<endl;                
    return NULL;
    } int root_index = 0;
        for (root_index = 0; Root_index < N, root_index++) {if (mid[root_index] = = Post[n-1]) {break; }} if (Root_index = = N) {cout<< "The sequence of sequences does not match the order sequence.
        "<<endl;
    return NULL;
    } binarytree* root = This->creat_node (post[n-1]); 
    if (Root_index > 0) {root->lchild = Root->mid_post_create_pre (Mid,post,root_index); } if (N-root_index-1 > 0) {root->rchild = Root->mid_post_create_pre (mid+root_index+1,post+root_index,n
    -ROOT_INDEX-1); 
} return root; } 

About binary tree construction and traversal please see my previous blog: Two construction of fork tree and its traversal algorithm

For the algorithm of constructing binary tree based on ordinal sequence and ordinal sequence, please refer to my previous blog:
Constructing binary tree based on ordinal sequence and middle sequence sequence

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.