-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST10.java
110 lines (88 loc) · 2.66 KB
/
BST10.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.ArrayList;
public class BST10 {
static class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
// static void Inorder1(Node root) {
// if (root == null) {
// return;
// }
// Inorder(root.left);
// System.out.print(root.data + " ");
// Inorder(root.right);
// }
// MERGE TWO BSTS
// BST1-> INORDER SEQ , BST 2 -> INORDER SEQ => NEW INORDER SEQ => FINAL BST
// INORDER SEQ OF BST'S
static void Inorder1(Node root) {
if (root == null) {
return;
}
Inorder1(root.left);
System.out.print(root.data + " ");
Inorder1(root.right);
}
static void Inorder(Node root, ArrayList<Integer> arr) {
if (root == null) {
return;
}
Inorder(root.left, arr);
arr.add(root.data);
Inorder(root.right, arr);
}
static Node ToBST(ArrayList<Integer> arr, int start, int end) { // SORTED ARRAY TO BST
if (start > end) {
return null;
}
int mid = (start + end) / 2;
Node root = new Node(arr.get(mid));
root.left = ToBST(arr, start, mid - 1);
root.right = ToBST(arr, mid + 1, end);
return root;
}
static Node MergeBST(Node root1, Node root2) {
ArrayList<Integer> arr1 = new ArrayList<>();
ArrayList<Integer> arr2 = new ArrayList<>();
Inorder(root1, arr1);
Inorder(root2, arr2);
ArrayList<Integer> finalarr = new ArrayList<>();
int i = 0;
int j = 0;
while (i < arr1.size() && j < arr2.size()) {
if (arr1.get(i) <= arr2.get(j)) {
finalarr.add(arr1.get(i));
i++;
} else {
finalarr.add(arr2.get(j));
j++;
}
while (i < arr1.size()) {
finalarr.add(arr1.get(i));
i++;
}
while (j < arr2.size()) {
finalarr.add(arr2.get(j));
j++;
}
}
return ToBST(finalarr, 0, finalarr.size() - 1);
}
public static void main(String[] args) {
Node root1 = new Node(2);
root1.left = new Node(1);
root1.right = new Node(4);
Node root2 = new Node(9);
root2.left = new Node(3);
root2.right = new Node(12); // THERE IS SOME ISSUE I DONOT KNOW WHY??
System.out.println();
Node root = MergeBST(root1, root2);
Inorder1(root);
}
}