Constructing two-fork tree from ordinal sequence/post sequence and middle sequence sequence

Source: Internet
Author: User

It is assumed that the first and middle sequence sequences of the tree are
Preorder = {7,10,4,3,1,2,8,11}
Inorder = {4,10,3,1,7,11,8,2}
1. Store the middle sequence sequence in HashMap, the value of the key storage node, and value stores the subscript of the node in the middle sequence sequence.

public void Map (int[] order) {
        int n = order.length;
        for (int i = 0;i<n;i++) {
            hashmap.put (order[i], i);
        }

    }

2. Using the first order sequence and the sequence structure, the code is as follows;

//index the root node of the tree corresponding to the recursive sequence of the subscript//n the number of nodes of the tree//off for the tree in the middle sequence of the left edge, through the off and root node in the middle sequence of the subscript can be obtained Zuozi node number/ /preorder,index corresponds to the ordinal sequence//n,off corresponds to the sequence public TreeNode buildpre (int[] preorder,int index,int n, int off) {if (0==
        N) {return null;
        } int rootval = Preorder[index];
        TreeNode root = new TreeNode (rootval); Hashmap.get (Rootval) obtains the subscript of the root node in the middle order sequence, minus the left boundary of the tree corresponding to the left//that is, the number of nodes for that root node corresponding to the right subtree int i = Hashmap.get (rootval)-O ff
        I is the number of nodes of the Zuozi//Zuozi recursion, the first order traversal format {root, {left subtree},{right subtree}}, if the root node is labeled index, then//its left subtree is labeled Index+1. Zuozi in the left border of the sequence
        Root.left = Buildpre (Preorder,index+1,i,off);
        In the sequence {root, {left subtree},{}}, because of the number of left child tree nodes I know, you can know the root of the right subtree//the corresponding subscript index+i+1//The tree node number n minus the left subtree node number I and 1 is the right subtree node number. Because the sequence is listed as {{left subtree}, root, {right subtree}},hashmap.get (Rootval) +1 is right subtree//left border in sequence of root.right = Buildpre (Preorder,inde
        X+i+1,n-i-1,hashmap.get (Rootval) +1);    
    return root; }

3. Using post-order sequences and sequence constructs:

Public TreeNode buildpost (int[] hostorder,int index,int n, int off) {

        if (n = = 0) {
            return null;
        }

        int rootval = hostorder[index];//Gets the root node
        TreeNode t = new TreeNode (rootval);

        int i = Off-hashmap.get (rootval);//Get the number of right subtree nodes
        t.right = Buildpost (Hostorder,index-1,i,off);
        T.left = Buildpost (Hostorder,index-i-1,n-i-1,hashmap.get (Rootval)-1);
        return t;



    }

Can find an example, the simulation program run, the process of running a more clear.
Reference: Http://blog.csdn.net/sgbfblog/article/details/7783935#comments

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.