Construct Tree from given Inorder and Preorder traversals

Go down

Construct Tree from given Inorder and Preorder traversals

Post  skyboard on Tue Sep 13, 2011 3:59 pm

public class Node{
private int value;
private Node left;
private Node right;

public Node(int v, Node l, Node r){
value=v;
left=l;
right=r;
}
}

public class Build{

public Node buildTree(int in[], int pre[], int start, int end){
static int preIndex=0;
if(start>end)
return null;
Node temp=new Node(pre[preIndex++],null,null);
if(start==end)
return temp;
int inIndex=search(in, start,end, temp.value);
temp.left=buildTree(in, pre, start, inIndex-1);
temp.right=buildTree(int,pre,inIndex+1,end);
return temp;
}

public int search(int[] in, int start, int end, int data){
for(int i=start;i<=end;i++){
if(in[i]==data)
return i;
}
}
}

skyboard

Posts : 31
Join date : 2011-09-03

View user profile

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum