Java based on binary tree pre-order, middle order for follow-up

Source: Internet
Author: User

In a binary tree total, the previous sequence traversal result is: ABDGCEFH, the middle sequence traversal result is: DGBAECHF, asks the post-order traversal result.

We know:

The pre-order traversal method is: root node, left dial hand tree, right subtree

The middle sequence traversal is: Left dial hand Tree-------right subtree

Post-order traversal mode: Left dial hand tree---right subtree--root node

It can be seen from here that the first value of the pre-sequence traversal is the root node, and then the middle sequence traversal to find this value, then the left part of the value is the current binary tree part of the left sub-tree of the pre-order traversal results, the right part of the value of the current binary tree is the right sub-tree part of the pre So, with this analysis, you can restore the binary tree and get a pseudo-code like this:

Node Getroot (pre-order, middle order)

C= first character of pre-order

Position of the pos=c in the middle order

Len1= the left half length of the middle order POS

len2= Middle order pos right half length

New node R, so that the elements of r are equal to C

R's left son =getroot (pre-order position 1 begins with the LEN1 length section, the left half of the POS position)

R's right son =getroot (pre-order position len1 start right half, middle order POS position right half part)

Return r

1 Example:

Figure 1

Input pre-order ABDGCEFH, middle order DGBAECHF, can be derived

A is the root node of the two fork tree

1:BDG for the pre-order of the left subtree of the two-fork tree

2:DGB is the middle order of the left subtree of the two-fork tree

A Zuozi can be built according to 1 and 2

3:CEFH for the first order of the right sub-tree of the two-fork tree

4:ECHF for the middle order of the right subtree of the two-fork tree

You can build a right subtree based on 3 and 4

The cloud structure of the two fork tree is obtained at the time of this step, and 2 shows that a is the root node, BDG on its left subtree, and CEFG on its right sub-tree.

So recursion to build a complete two-fork tree

Java code

classdatanode{intdata; DataNode Leftchild=NULL; DataNode Rightchild=NULL;} Public classNodetree {DataNode rootNode;    DataNode Tempnode; //int index_root;DataNode Left_childdatanode;        DataNode Right_childdatanode;  PublicDataNode Initrootnode (int[] prearray) {RootNode=NewDataNode (); Rootnode.data= Prearray[0]; returnRootNode; }         Public  voidBuildtree (int[] Prearray,int[] Midarray,datanode RootNode) {        intIndex_root =GetIndex (Midarray, rootnode.data); intLengthofrighttree = prearray.length-index_root-1; int[] prearray_left; int[] prearray_right; int[] midarray_left; int[] midarray_right; if(index_root>0) {Left_childdatanode=NewDataNode (); if(index_root==1) {Left_childdatanode.data= Midarray[0]; Rootnode.leftchild=Left_childdatanode; }Else{prearray_left=New int[Index_root]; Midarray_left=New int[Index_root]; System.arraycopy (Prearray,1, prearray_left, 0, Index_root); System.arraycopy (Midarray,0, Midarray_left, 0, Index_root); Left_childdatanode.data= Prearray_left[0]; Rootnode.leftchild=Left_childdatanode;            Buildtree (Prearray_left, Midarray_left, Left_childdatanode); }            }                if(lengthofrighttree>0) {Right_childdatanode=NewDataNode (); if(lengthofrighttree==1) {Right_childdatanode.data= Midarray[index_root+1]; Rootnode.rightchild=Right_childdatanode; return; }Else{prearray_right=New int[Lengthofrighttree]; Midarray_right=New int[Lengthofrighttree]; System.arraycopy (Prearray, Index_root+1, Prearray_right, 0, Lengthofrighttree); System.arraycopy (Midarray, Index_root+1, Midarray_right, 0, Lengthofrighttree); Right_childdatanode.data= Prearray_right[0]; Rootnode.rightchild=Right_childdatanode;            Buildtree (Prearray_right, Midarray_right,right_childdatanode); }        }    }         Public intGetIndex (int[] Array,inttemp) {        intindex =-1;  for(inti = 0; i < Array.Length; i++) {            if(array[i]==temp) {Index=i; returnindex; }        }        returnindex; }// post-traversal Public voidPostordertraverse (DataNode node) {if(node==NULL) {            return;        } postordertraverse (Node.leftchild);        Postordertraverse (Node.rightchild);    System.out.print (Node.data); }//Pre-order traversal Public voidPreordertraverse (DataNode node) {if(node==NULL) {            return;        } System.out.print (Node.data);        Preordertraverse (Node.leftchild);    Preordertraverse (Node.rightchild); }//middle sequence traversal Public voidInordertraverse (DataNode node) {if(node==NULL) {            return;        } inordertraverse (Node.leftchild);        System.out.print (Node.data);    Inordertraverse (Node.rightchild); }         Public Static voidMain (String args[]) {int[] Prearray = {}; int[] Midarray = {}; Nodetree Tree=NewNodetree (); DataNode Headnode=Tree.initrootnode (Prearray); Tree.        Buildtree (Prearray, Midarray, Headnode);    Tree.postordertraverse (Headnode); }    }

Java based on binary tree pre-order, middle order for follow-up

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.