-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.java
271 lines (207 loc) · 7.25 KB
/
BinarySearchTree.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
public class BinarySearchTree {
// Complete this class
BinaryTreeNode<Integer> root;
public boolean sHelp(int e, BinaryTreeNode<Integer> root) {
if (root == null) {
return false;
}
if (root.data == e) {
return true;
}
if (e < root.data) {
return sHelp(e, root.left);
} else {
return sHelp(e, root.right);
}
}
public BinaryTreeNode<Integer> iHelp(int e, BinaryTreeNode<Integer> root) {
if (root == null) {
return new BinaryTreeNode<Integer>(e);
}
if (e < root.data) {
root.left = iHelp(e, root.left);
return root;
} else {
root.right = iHelp(e, root.right);
return root;
}
}
public void printTree(BinaryTreeNode<Integer> root) {
if (root == null) {
return;
}
System.out.print(String.valueOf(root.data) + ':');
if (root.left != null) {
System.out.print("L:" + String.valueOf(root.left.data) + ",");
}
if (root.right != null) {
System.out.print("R:" + String.valueOf(root.right.data));
}
System.out.println();
printTree(root.left);
printTree(root.right);
}
public BinaryTreeNode<Integer> deleteData(int e, BinaryTreeNode<Integer> root) {
if (root == null) {
return root;
}
if (root.data == e) {
if (root.left == null && root.right == null) {
return null;
}
if (root.left == null) {
return root.right;
}
if (root.right == null) {
return root.left;
} else {
int a = Min(root.right);
root.right = deleteData(a, root.right);
root.data = a;
return root;
}
}
if (e < root.data) {
root.left = deleteData(e, root.left);
return root;
} else {
root.right = deleteData(e, root.right);
return root;
}
}
private Pair<Pair<Integer, Integer>, BinaryTreeNode<Integer>> findImbalanceNode(BinaryTreeNode<Integer> root,
int e) {
int l, r, bf;
if (root == null) {
Pair<Integer, Integer> pair = new Pair<Integer, Integer>();
pair.first = 0;
pair.second = 0;
Pair<Pair<Integer, Integer>, BinaryTreeNode<Integer>> ans = new Pair<Pair<Integer, Integer>, BinaryTreeNode<Integer>>();
ans.first = pair;
return ans;
}
Pair<Pair<Integer, Integer>, BinaryTreeNode<Integer>> left = findImbalanceNode(root.left, e);
if (left.second != null) {
l = left.first.first;
r = left.first.second;
bf = l - r;
root.left = balance(left.second, bf, e);
return left;
}
Pair<Pair<Integer, Integer>, BinaryTreeNode<Integer>> right = findImbalanceNode(root.right, e);
if (right.second != null) {
l = right.first.first;
r = right.first.second;
bf = l - r;
root.right = balance(right.second, bf, e);
return right;
}
Pair<Integer, Integer> ans1 = new Pair<Integer, Integer>();
Pair<Pair<Integer, Integer>, BinaryTreeNode<Integer>> ans = new Pair<Pair<Integer, Integer>, BinaryTreeNode<Integer>>();
ans1.first = 1 + Math.max(left.first.first, left.first.second);
ans1.second = 1 + Math.max(right.first.first, right.first.second);
bf = ans1.first - ans1.second;
if (Math.abs(bf) > 1) {
ans.first = ans1;
ans.second = root;
} else {
ans.first = ans1;
}
return ans;
}
public BinaryTreeNode<Integer> balance(BinaryTreeNode<Integer> node, int bf, int e) {
if (bf < 0) {
System.out.println("bf<0");
if (node.right.data > e) {
System.out.println("right-left");
BinaryTreeNode<Integer> nodeX = rightRotation(node.right);
node.right = nodeX;
BinaryTreeNode<Integer> nodeY = leftRotation(node);
return nodeY;
} else {
System.out.println("normal left");
return leftRotation(node);
}
} else if (bf > 0) {
System.out.println("bf>0");
if (node.left.data < e) {
System.out.println("left-right");
BinaryTreeNode<Integer> nodeX = leftRotation(node.left);
node.left = nodeX;
BinaryTreeNode<Integer> nodeY = rightRotation(node);
return nodeY;
} else {
System.out.println("normal right");
return rightRotation(node);
}
}
return null;
}
public static BinaryTreeNode<Integer> rightRotation(BinaryTreeNode<Integer> node) {
BinaryTreeNode<Integer> newRoot = node.left;
node.left = newRoot.right;
newRoot.right = node;
return newRoot;
}
public static BinaryTreeNode<Integer> leftRotation(BinaryTreeNode<Integer> node) {
BinaryTreeNode<Integer> newRoot = node.right;
node.right = newRoot.left;
newRoot.left = node;
return newRoot;
}
public boolean search(int e) {
return sHelp(e, root);
}
public void insertData(int e) {
this.root = iHelp(e, root);
Pair<Pair<Integer, Integer>, BinaryTreeNode<Integer>> pair = findImbalanceNode(root, e);
// System.out.println(pair.second.data);
if (pair.second != null && pair.second.data == root.data) {
System.out.println("inside if");
int bf = pair.first.first - pair.first.second;
root = balance(root, bf, e);
}
}
public void deleteData(int e) {
this.root = deleteData(e, root);
}
public void printTree() {
printTree(root);
}
public static int Max(BinaryTreeNode<Integer> root) {
if (root.left == null && root.right == null) {
return root.data;
}
if (root.left == null) {
if (root.data > Max(root.right)) {
return root.data;
} else
return Max(root.right);
} else if (root.right == null) {
if (root.data > Max(root.left)) {
return root.data;
} else
return Max(root.left);
} else {
return Math.max(root.data, Math.max(Max(root.left), Max(root.right)));
}
}
public static int Min(BinaryTreeNode<Integer> root) {
if (root.left == null && root.right == null) {
return root.data;
}
if (root.left == null) {
if (root.data < Min(root.right)) {
return root.data;
} else
return Min(root.right);
} else if (root.right == null) {
if (root.data < Max(root.left)) {
return root.data;
} else
return Min(root.left);
} else {
return Math.min(root.data, Math.min(Min(root.left), Min(root.right)));
}
}
}