-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAVLTree.java
More file actions
321 lines (268 loc) · 7.87 KB
/
Copy pathAVLTree.java
File metadata and controls
321 lines (268 loc) · 7.87 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
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
package algorithms.tree;
/**
* AVL Tree implementation - a self-balancing binary search tree.
*
* In an AVL tree, the heights of the two child subtrees of any node differ by at most one.
* If at any time they differ by more than one, rebalancing is done to restore this property.
*
* Operations (all O(log n)):
* - Insert: Add a new node and rebalance if necessary
* - Delete: Remove a node and rebalance if necessary
* - Search: Find a node by value
*
* Time Complexity: O(log n) for insert, delete, and search
* Space Complexity: O(n) for storing n nodes
*/
public class AVLTree {
private AVLNode root;
/**
* AVL tree node with height information.
*/
private static class AVLNode {
int val;
int height;
AVLNode left;
AVLNode right;
AVLNode(int val) {
this.val = val;
this.height = 1;
}
}
/**
* Get height of a node.
*/
private int height(AVLNode node) {
return node == null ? 0 : node.height;
}
/**
* Get balance factor of a node.
* Balance Factor = Height(Left) - Height(Right)
*/
private int getBalance(AVLNode node) {
return node == null ? 0 : height(node.left) - height(node.right);
}
/**
* Update height of a node based on its children.
*/
private void updateHeight(AVLNode node) {
if (node != null) {
node.height = Math.max(height(node.left), height(node.right)) + 1;
}
}
/**
* Right rotation.
*
* y x
* / \ / \
* x T3 ==> T1 y
* / \ / \
* T1 T2 T2 T3
*/
private AVLNode rotateRight(AVLNode y) {
AVLNode x = y.left;
AVLNode T2 = x.right;
// Perform rotation
x.right = y;
y.left = T2;
// Update heights
updateHeight(y);
updateHeight(x);
return x;
}
/**
* Left rotation.
*
* x y
* / \ / \
* T1 y ==> x T3
* / \ / \
* T2 T3 T1 T2
*/
private AVLNode rotateLeft(AVLNode x) {
AVLNode y = x.right;
AVLNode T2 = y.left;
// Perform rotation
y.left = x;
x.right = T2;
// Update heights
updateHeight(x);
updateHeight(y);
return y;
}
/**
* Insert a value into the AVL tree.
*/
public void insert(int val) {
root = insertNode(root, val);
}
/**
* Recursive insert helper with rebalancing.
*/
private AVLNode insertNode(AVLNode node, int val) {
// 1. Perform normal BST insertion
if (node == null) {
return new AVLNode(val);
}
if (val < node.val) {
node.left = insertNode(node.left, val);
} else if (val > node.val) {
node.right = insertNode(node.right, val);
} else {
// Duplicate values not allowed
return node;
}
// 2. Update height of this ancestor node
updateHeight(node);
// 3. Get the balance factor
int balance = getBalance(node);
// 4. If node becomes unbalanced, there are 4 cases:
// Left-Left Case
if (balance > 1 && val < node.left.val) {
return rotateRight(node);
}
// Right-Right Case
if (balance < -1 && val > node.right.val) {
return rotateLeft(node);
}
// Left-Right Case
if (balance > 1 && val > node.left.val) {
node.left = rotateLeft(node.left);
return rotateRight(node);
}
// Right-Left Case
if (balance < -1 && val < node.right.val) {
node.right = rotateRight(node.right);
return rotateLeft(node);
}
return node;
}
/**
* Search for a value in the AVL tree.
*/
public boolean search(int val) {
return searchNode(root, val);
}
private boolean searchNode(AVLNode node, int val) {
if (node == null) {
return false;
}
if (val == node.val) {
return true;
} else if (val < node.val) {
return searchNode(node.left, val);
} else {
return searchNode(node.right, val);
}
}
/**
* Delete a value from the AVL tree.
*/
public void delete(int val) {
root = deleteNode(root, val);
}
/**
* Recursive delete helper with rebalancing.
*/
private AVLNode deleteNode(AVLNode node, int val) {
// 1. Perform standard BST delete
if (node == null) {
return null;
}
if (val < node.val) {
node.left = deleteNode(node.left, val);
} else if (val > node.val) {
node.right = deleteNode(node.right, val);
} else {
// Node to be deleted found
// Node with only one child or no child
if (node.left == null || node.right == null) {
node = (node.left != null) ? node.left : node.right;
} else {
// Node with two children: Get inorder successor
AVLNode temp = getMinNode(node.right);
node.val = temp.val;
node.right = deleteNode(node.right, temp.val);
}
}
// If the tree had only one node then return
if (node == null) {
return null;
}
// 2. Update height
updateHeight(node);
// 3. Get balance factor
int balance = getBalance(node);
// 4. If node becomes unbalanced, there are 4 cases:
// Left-Left Case
if (balance > 1 && getBalance(node.left) >= 0) {
return rotateRight(node);
}
// Left-Right Case
if (balance > 1 && getBalance(node.left) < 0) {
node.left = rotateLeft(node.left);
return rotateRight(node);
}
// Right-Right Case
if (balance < -1 && getBalance(node.right) <= 0) {
return rotateLeft(node);
}
// Right-Left Case
if (balance < -1 && getBalance(node.right) > 0) {
node.right = rotateRight(node.right);
return rotateLeft(node);
}
return node;
}
/**
* Get the node with minimum value.
*/
private AVLNode getMinNode(AVLNode node) {
while (node.left != null) {
node = node.left;
}
return node;
}
/**
* In-order traversal (sorted order).
*/
public void inorder() {
inorderTraversal(root);
System.out.println();
}
private void inorderTraversal(AVLNode node) {
if (node != null) {
inorderTraversal(node.left);
System.out.print(node.val + " ");
inorderTraversal(node.right);
}
}
/**
* Get the height of the tree.
*/
public int getHeight() {
return height(root);
}
/**
* Example usage and testing.
*/
public static void main(String[] args) {
AVLTree tree = new AVLTree();
System.out.println("Inserting values: 10, 20, 30, 40, 50, 25");
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.insert(40);
tree.insert(50);
tree.insert(25);
System.out.print("In-order traversal: ");
tree.inorder();
System.out.println("Tree height: " + tree.getHeight());
System.out.println("\nSearching for 30: " + tree.search(30));
System.out.println("Searching for 100: " + tree.search(100));
System.out.println("\nDeleting 40");
tree.delete(40);
System.out.print("In-order traversal after deletion: ");
tree.inorder();
System.out.println("Tree height after deletion: " + tree.getHeight());
}
}