-
Notifications
You must be signed in to change notification settings - Fork 21k
Expand file tree
/
Copy pathAVLTree.java
More file actions
259 lines (217 loc) · 6.03 KB
/
AVLTree.java
File metadata and controls
259 lines (217 loc) · 6.03 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
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
// File 3: AVLTree.java
package trees;
/**
* AVL Tree Implementation (Self-Balancing Binary Search Tree)
*
* An AVL Tree is a self-balancing BST where the heights of the two child
* subtrees of any node differ by at most one. This guarantees O(log n)
* complexity for all operations by performing rotations after each
* insertion/deletion.
*
* Reference: https://en.wikipedia.org/wiki/AVL_tree
*
* Rotations Performed: - LL Rotation (Left-Left case): Right rotation - RR
* Rotation (Right-Right case): Left rotation - LR Rotation (Left-Right case):
* Left rotation followed by right rotation - RL Rotation (Right-Left case):
* Right rotation followed by left rotation
*/
public class AVLTree {
private TreeNode root;
private static final String EMPTY_TREE_ERROR = "Tree is empty";
public AVLTree() {
this.root = null;
}
// ============= HEIGHT MANAGEMENT =============
private int getHeight(TreeNode node) {
return node == null ? 0 : node.height;
}
private int getBalance(TreeNode node) {
return node == null ? 0 : getHeight(node.left) - getHeight(node.right);
}
private void updateHeight(TreeNode node) {
if (node != null) {
node.height =
1 + Math.max(getHeight(node.left), getHeight(node.right));
}
}
// ============= ROTATION OPERATIONS =============
// Right Rotation (LL case)
private TreeNode rotateRight(TreeNode y) {
TreeNode x = y.left;
TreeNode t2 = x.right;
x.right = y;
y.left = t2;
updateHeight(y);
updateHeight(x);
return x;
}
// Left Rotation (RR case)
private TreeNode rotateLeft(TreeNode x) {
TreeNode y = x.right;
TreeNode t2 = y.left;
y.left = x;
x.right = t2;
updateHeight(x);
updateHeight(y);
return y;
}
// ============= INSERT OPERATION =============
public void insert(int value) {
root = insertRecursive(root, value);
}
private TreeNode insertRecursive(TreeNode node, int value) {
if (node == null) {
return new TreeNode(value);
}
if (value < node.value) {
node.left = insertRecursive(node.left, value);
} else if (value > node.value) {
node.right = insertRecursive(node.right, value);
} else {
return node; // Duplicate ignored
}
updateHeight(node);
return balance(node);
}
// ============= BALANCE OPERATION =============
private TreeNode balance(TreeNode node) {
int balanceFactor = getBalance(node);
// Left Heavy Cases
if (balanceFactor > 1) {
if (getBalance(node.left) < 0) {
// LR case: Left-Right
node.left = rotateLeft(node.left);
}
// LL case: Left-Left
return rotateRight(node);
}
// Right Heavy Cases
if (balanceFactor < -1) {
if (getBalance(node.right) > 0) {
// RL case: Right-Left
node.right = rotateRight(node.right);
}
// RR case: Right-Right
return rotateLeft(node);
}
return node;
}
// ============= DELETE OPERATION =============
public void delete(int value) {
root = deleteRecursive(root, value);
}
private TreeNode deleteRecursive(TreeNode node, int value) {
if (node == null) {
return null;
}
if (value < node.value) {
node.left = deleteRecursive(node.left, value);
} else if (value > node.value) {
node.right = deleteRecursive(node.right, value);
} else {
// Node to delete found
if (node.left == null && node.right == null) {
return null;
}
if (node.left == null) {
return node.right;
}
if (node.right == null) {
return node.left;
}
TreeNode minRight = findMinNode(node.right);
node.value = minRight.value;
node.right = deleteRecursive(node.right, minRight.value);
}
updateHeight(node);
return balance(node);
}
// ============= SEARCH OPERATION =============
public boolean search(int value) {
return searchRecursive(root, value);
}
private boolean searchRecursive(TreeNode node, int value) {
if (node == null) {
return false;
}
if (value == node.value) {
return true;
} else if (value < node.value) {
return searchRecursive(node.left, value);
} else {
return searchRecursive(node.right, value);
}
}
// ============= UTILITY METHODS =============
public int findMin() {
if (root == null) {
throw new IllegalStateException(EMPTY_TREE_ERROR);
}
return findMinNode(root).value;
}
private TreeNode findMinNode(TreeNode node) {
while (node.left != null) {
node = node.left;
}
return node;
}
public int findMax() {
if (root == null) {
throw new IllegalStateException(EMPTY_TREE_ERROR);
}
return findMaxNode(root).value;
}
private TreeNode findMaxNode(TreeNode node) {
while (node.right != null) {
node = node.right;
}
return node;
}
// ============= TREE TRAVERSALS =============
public void inorder() {
System.out.print("Inorder: ");
inorderRecursive(root);
System.out.println();
}
private void inorderRecursive(TreeNode node) {
if (node != null) {
inorderRecursive(node.left);
System.out.print(node.value + " ");
inorderRecursive(node.right);
}
}
public void preorder() {
System.out.print("Preorder: ");
preorderRecursive(root);
System.out.println();
}
private void preorderRecursive(TreeNode node) {
if (node != null) {
System.out.print(node.value + " ");
preorderRecursive(node.left);
preorderRecursive(node.right);
}
}
public void postorder() {
System.out.print("Postorder: ");
postorderRecursive(root);
System.out.println();
}
private void postorderRecursive(TreeNode node) {
if (node != null) {
postorderRecursive(node.left);
postorderRecursive(node.right);
System.out.print(node.value + " ");
}
}
// ============= HELPER METHODS =============
public int getHeight() {
return getHeight(root);
}
public boolean isEmpty() {
return root == null;
}
public void clear() {
root = null;
}
}