-
Notifications
You must be signed in to change notification settings - Fork 151
/
AVLTree.java
339 lines (290 loc) · 6.77 KB
/
AVLTree.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// AVL Binary search tree implementation in Java
// Author: AlgorithmTutor
// data structure that represents a node in the tree
class Node {
int data; // holds the key
Node parent; // pointer to the parent
Node left; // pointer to left child
Node right; // pointer to right child
int bf; // balance factor of the node
public Node(int data) {
this.data = data;
this.parent = null;
this.left = null;
this.right = null;
this.bf = 0;
}
}
public class AVLTree {
private Node root;
public AVLTree() {
root = null;
}
private void printHelper(Node currPtr, String indent, boolean last) {
// print the tree structure on the screen
if (currPtr != null) {
System.out.print(indent);
if (last) {
System.out.print("R----");
indent += " ";
} else {
System.out.print("L----");
indent += "| ";
}
System.out.println(currPtr.data + "(BF = " + currPtr.bf + ")");
printHelper(currPtr.left, indent, false);
printHelper(currPtr.right, indent, true);
}
}
private Node searchTreeHelper(Node node, int key) {
if (node == null || key == node.data) {
return node;
}
if (key < node.data) {
return searchTreeHelper(node.left, key);
}
return searchTreeHelper(node.right, key);
}
private Node deleteNodeHelper(Node node, int key) {
// search the key
if (node == null) return node;
else if (key < node.data) node.left = deleteNodeHelper(node.left, key);
else if (key > node.data) node.right = deleteNodeHelper(node.right, key);
else {
// the key has been found, now delete it
// case 1: node is a leaf node
if (node.left == null && node.right == null) {
node = null;
}
// case 2: node has only one child
else if (node.left == null) {
Node temp = node;
node = node.right;
}
else if (node.right == null) {
Node temp = node;
node = node.left;
}
// case 3: has both children
else {
Node temp = minimum(node.right);
node.data = temp.data;
node.right = deleteNodeHelper(node.right, temp.data);
}
}
// Write the update balance logic here
// YOUR CODE HERE
return node;
}
// update the balance factor the node
private void updateBalance(Node node) {
if (node.bf < -1 || node.bf > 1) {
rebalance(node);
return;
}
if (node.parent != null) {
if (node == node.parent.left) {
node.parent.bf -= 1;
}
if (node == node.parent.right) {
node.parent.bf += 1;
}
if (node.parent.bf != 0) {
updateBalance(node.parent);
}
}
}
// rebalance the tree
void rebalance(Node node) {
if (node.bf > 0) {
if (node.right.bf < 0) {
rightRotate(node.right);
leftRotate(node);
} else {
leftRotate(node);
}
} else if (node.bf < 0) {
if (node.left.bf > 0) {
leftRotate(node.left);
rightRotate(node);
} else {
rightRotate(node);
}
}
}
private void preOrderHelper(Node node) {
if (node != null) {
System.out.print(node.data + " ");
preOrderHelper(node.left);
preOrderHelper(node.right);
}
}
private void inOrderHelper(Node node) {
if (node != null) {
inOrderHelper(node.left);
System.out.print(node.data + " ");
inOrderHelper(node.right);
}
}
private void postOrderHelper(Node node) {
if (node != null) {
postOrderHelper(node.left);
postOrderHelper(node.right);
System.out.print(node.data + " ");
}
}
// Pre-Order traversal
// Node.Left Subtree.Right Subtree
public void preorder() {
preOrderHelper(this.root);
}
// In-Order traversal
// Left Subtree . Node . Right Subtree
public void inorder() {
inOrderHelper(this.root);
}
// Post-Order traversal
// Left Subtree . Right Subtree . Node
public void postorder() {
postOrderHelper(this.root);
}
// search the tree for the key k
// and return the corresponding node
public Node searchTree(int k) {
return searchTreeHelper(this.root, k);
}
// find the node with the minimum key
public Node minimum(Node node) {
while (node.left != null) {
node = node.left;
}
return node;
}
// find the node with the maximum key
public Node maximum(Node node) {
while (node.right != null) {
node = node.right;
}
return node;
}
// find the successor of a given node
public Node successor(Node x) {
// if the right subtree is not null,
// the successor is the leftmost node in the
// right subtree
if (x.right != null) {
return minimum(x.right);
}
// else it is the lowest ancestor of x whose
// left child is also an ancestor of x.
Node y = x.parent;
while (y != null && x == y.right) {
x = y;
y = y.parent;
}
return y;
}
// find the predecessor of a given node
public Node predecessor(Node x) {
// if the left subtree is not null,
// the predecessor is the rightmost node in the
// left subtree
if (x.left != null) {
return maximum(x.left);
}
Node y = x.parent;
while (y != null && x == y.left) {
x = y;
y = y.parent;
}
return y;
}
// rotate left at node x
void leftRotate(Node x) {
Node y = x.right;
x.right = y.left;
if (y.left != null) {
y.left.parent = x;
}
y.parent = x.parent;
if (x.parent == null) {
this.root = y;
} else if (x == x.parent.left) {
x.parent.left = y;
} else {
x.parent.right = y;
}
y.left = x;
x.parent = y;
// update the balance factor
x.bf = x.bf - 1 - Math.max(0, y.bf);
y.bf = y.bf - 1 + Math.min(0, x.bf);
}
// rotate right at node x
void rightRotate(Node x) {
Node y = x.left;
x.left = y.right;
if (y.right != null) {
y.right.parent = x;
}
y.parent = x.parent;
if (x.parent == null) {
this.root = y;
} else if (x == x.parent.right) {
x.parent.right = y;
} else {
x.parent.left = y;
}
y.right = x;
x.parent = y;
// update the balance factor
x.bf = x.bf + 1 - Math.min(0, y.bf);
y.bf = y.bf + 1 + Math.max(0, x.bf);
}
// insert the key to the tree in its appropriate position
public void insert(int key) {
// PART 1: Ordinary BST insert
Node node = new Node(key);
Node y = null;
Node x = this.root;
while (x != null) {
y = x;
if (node.data < x.data) {
x = x.left;
} else {
x = x.right;
}
}
// y is parent of x
node.parent = y;
if (y == null) {
root = node;
} else if (node.data < y.data) {
y.left = node;
} else {
y.right = node;
}
// PART 2: re-balance the node if necessary
updateBalance(node);
}
// delete the node from the tree
Node deleteNode(int data) {
return deleteNodeHelper(this.root, data);
}
// print the tree structure on the screen
public void prettyPrint() {
printHelper(this.root, "", true);
}
public static void main(String [] args) {
AVLTree bst = new AVLTree();
bst.insert(1);
bst.insert(2);
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(6);
bst.insert(7);
bst.insert(8);
bst.prettyPrint();
}
}