-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareBTrees.java
More file actions
55 lines (54 loc) · 1.63 KB
/
CompareBTrees.java
File metadata and controls
55 lines (54 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Arrays;
import java.util.HashMap;
public class CompareBTrees{
public static void main(String[] args) {
int[] aTree={8, 3, 6, 1, 4, 7, 10, 14, 13};
int[] bTree={8, 10, 14, 3, 6, 4, 1, 7, 13};
System.out.println(Arrays.toString(aTree));
System.out.println(Arrays.toString(bTree));
System.out.println(compare(aTree,bTree));
}
static boolean compare(int[] aTree, int[] bTree){
int[] aInOrder=getInOrderTraversal(aTree);
int[] bInOrder=getInOrderTraversal(bTree);
System.out.println(Arrays.toString(aInOrder));
System.out.println(Arrays.toString(bInOrder));
return true;
}
static int[] getInOrderTraversal(int[] tree){
int n=tree.length;
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<n;i++){
map.put(tree[i],i);
}
int[] temp=new int[n];
temp[0]=tree[0];
tree[0]=-1;
int i=1;
int count=0;
while(count<n){
int j=map.get(temp[count])+1;
while(j<n){
if(tree[j]<=temp[count] && tree[j]!=-1){
temp[i]=tree[j];
tree[j]=-1;
++i;
break;
}
++j;
}
j=map.get(temp[count])+1;
while(j<n){
if(tree[j]>temp[count] && tree[j]!=-1){
temp[i]=tree[j];
tree[j]=-1;
++i;
break;
}
++j;
}
++count;
}
return temp;
}
}