Binary Tree reconstruction

Source: Internet
Author: User

To enter the results of the pre-order traversal and the middle sequence traversal of a binary tree, rebuild the two-fork tree. Assume that no duplicate numbers are included in the result of the input's pre-order traversal and the middle-order traversal. For example, enter the pre-sequence traversal sequence {1,2,4,7,3,5,6,8} and the middle sequence traversal sequence {4,7,2,1,5,3,8,6}, then rebuild the binary tree and output its post-order traversal sequence.

Code:

Package Com.huawei;

Import Java.util.Scanner;

Class Node {
Node left = null;
Node right = null;
int value;
int size;
}
public class Binarytreebuilder {
/**
* Reconstruction of Binary tree subtree based on pre-sequence traversal and middle sequence traversal
* @param preorder Sequence traversal array
* @param start sub-tree start position
* @param inorder sequence Traversal array
* @param end Middle-order traversal ending position
* @param length subtree node tree
* @return The root node of the subtree
*/
public static Node Buildtree (char[] preorder, int start,
Char[] inorder, int end, int length) {
Parameter Validation
if (preorder = = NULL | | preorder.length = = 0 | | inorder = = NULL
|| Inorder.length = = 0 | | Length <= 0) {
return null;
}

Establishing a sub-root node
Char value = Preorder[start];
Node root = new node ();
Root.value = value;

recursive termination condition: Subtree has only one node
if (length = = 1)
return root;

Saozi Right sub-tree of sub-tree splitting
int i = 0;
while (I < length) {
if (value = = Inorder[end-i]) {
Break
}
i++;
}

Establishing the Zuozi of subtrees
Root.left = Buildtree (preorder, start + 1, inorder, End-i-1, length-1-i);
Establish the right subtree of a subtree
Root.right = Buildtree (preorder, start + length-i, inorder, end, i);

return root;
}
public static void Gettree (Node root) {
if (root = = null) {
System.out.println ("No");
Return
}
if (root.left! = null) {
Gettree (Root.left);
}
if (root.right! = null) {
Gettree (Root.right);
}
System.out.print (Root.value + "");
}

public static Node rebuild (int[] preorder, int startpre, int endpre, int[] inorder, int startin, int endin) {

if (endpre-startpre! = endin-startin) {
return null;
}

if (Startpre > Endpre) {
return null;
}

Node root = new node ();
Root.value = Preorder[startpre];
Root.size = 1;
Root.left = null;
Root.right = null;

if (Startpre = = Endpre) {
return root;
}

int index, length;
for (Index=startin; index<=endin; index++) {
if (inorder[index] = = Preorder[startpre]) {
Break
}
}

if (Index > Endin) {
return null;
}

if (Index > Startin) {
length = Index-startin;
Root.left = Rebuild (preorder, startpre+1, Startpre+length, Inorder, Startin, startin+length-1);
if (root.left! = null)
Root.size + = Root.left.size;
}
if (Index < Endin) {
length = Endin-index;
Root.right = Rebuild (preorder, endpre-length+1, Endpre, Inorder, endin-length+1, Endin);
if (root.right! = null)
Root.size + = Root.right.size;
}
return root;
}
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
while (Sc.hasnext ()) {
int n = sc.nextint ();
int[] Preorder = new Int[n]; {' 1 ', ' 2 ', ' 4 ', ' 7 ', ' 3 ', ' 5 ', ' 6 ', ' 8 '};
int[] inorder = new Int[n]; {' 4 ', ' 7 ', ' 2 ', ' 1 ', ' 5 ', ' 3 ', ' 8 ', ' 6 '};
String prestr = Sc.next ();
System.out.println (Prestr.length ());
String inStr = Sc.nextline ();
for (int i=0; i<n; i++) {
Preorder[i] = Sc.nextint ();//.charat (0);
Inorder[i] = Instr.charat (i);
}
for (int i=0; i<n; i++) {
Preorder[i] = Sc.next (). charAt (0);
Inorder[i] = Sc.nextint ();//.charat (0);
}
Node root = Rebuild (preorder,0,n-1,inorder,0,n-1);//buildtree (preorder, 0, inorder, inorder.length-1, Inorder.length) ;
if (root.size = = N)
Gettree (root);
else{
System.out.println ("No");
}
System.out.println ();
}
Sc.close ();
}
}

Binary Tree reconstruction

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.