Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathAVLTree.java
More file actions
Latest commit
269 lines (232 loc) · 6.5 KB
/
Copy pathAVLTree.java
File metadata and controls
269 lines (232 loc) · 6.5 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
packagecom.thealgorithms.datastructures.trees;
importjava.util.ArrayList;
importjava.util.List;
/**
* Represents an AVL Tree, 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 they differ by more than one at any time,
* rebalancing is performed to restore this property.
*/
publicclassAVLTree {
privateNoderoot;
privatestaticclassNode {
privateintkey;
privateintbalance;
privateintheight;
privateNodeleft;
privateNoderight;
privateNodeparent;
Node(intk, Nodep) {
key = k;
parent = p;
}
publicIntegergetBalance() {
returnbalance;
}
}
/**
* Inserts a new key into the AVL tree.
*
* @param key the key to be inserted
* @return {@code true} if the key was inserted, {@code false} if the key already exists
*/
publicbooleaninsert(intkey) {
if (root == null) {
root = newNode(key, null);
} else {
Noden = root;
Nodeparent;
while (true) {
if (n.key == key) {
returnfalse;
}
parent = n;
booleangoLeft = n.key > key;
n = goLeft ? n.left : n.right;
if (n == null) {
if (goLeft) {
parent.left = newNode(key, parent);
} else {
parent.right = newNode(key, parent);
}
rebalance(parent);
break;
}
}
}
returntrue;
}
/**
* Deletes a key from the AVL tree.
*
* @param delKey the key to be deleted
*/
publicvoiddelete(intdelKey) {
if (root == null) {
return;
}
// Find the node to be deleted
Nodenode = root;
Nodechild = root;
while (child != null) {
node = child;
child = delKey >= node.key ? node.right : node.left;
if (delKey == node.key) {
delete(node);
return;
}
}
}
privatevoiddelete(Nodenode) {
if (node.left == null && node.right == null) {
// Leaf node
if (node.parent == null) {
root = null;
} else {
Nodeparent = node.parent;
if (parent.left == node) {
parent.left = null;
} else {
parent.right = null;
}
rebalance(parent);
}
return;
}
// Node has one or two children
Nodechild;
if (node.left != null) {
child = node.left;
while (child.right != null) {
child = child.right;
}
} else {
child = node.right;
while (child.left != null) {
child = child.left;
}
}
node.key = child.key;
delete(child);
}
/**
* Returns a list of balance factors for each node in the tree.
*
* @return a list of integers representing the balance factors of the nodes
*/
publicList<Integer> returnBalance() {
List<Integer> balances = newArrayList<>();
returnBalance(root, balances);
returnbalances;
}
privatevoidreturnBalance(Noden, List<Integer> balances) {
if (n != null) {
returnBalance(n.left, balances);
balances.add(n.getBalance());
returnBalance(n.right, balances);
}
}
/**
* Searches for a key in the AVL tree.
*
* @param key the key to be searched
* @return true if the key is found, false otherwise
*/
publicbooleansearch(intkey) {
Noderesult = searchHelper(this.root, key);
returnresult != null;
}
privateNodesearchHelper(Noderoot, intkey) {
if (root == null || root.key == key) {
returnroot;
}
if (root.key > key) {
returnsearchHelper(root.left, key);
}
returnsearchHelper(root.right, key);
}
privatevoidrebalance(Noden) {
setBalance(n);
if (n.balance == -2) {
if (height(n.left.left) >= height(n.left.right)) {
n = rotateRight(n);
} else {
n = rotateLeftThenRight(n);
}
} elseif (n.balance == 2) {
if (height(n.right.right) >= height(n.right.left)) {
n = rotateLeft(n);
} else {
n = rotateRightThenLeft(n);
}
}
if (n.parent != null) {
rebalance(n.parent);
} else {
root = n;
}
}
privateNoderotateLeft(Nodea) {
Nodeb = a.right;
b.parent = a.parent;
a.right = b.left;
if (a.right != null) {
a.right.parent = a;
}
b.left = a;
a.parent = b;
if (b.parent != null) {
if (b.parent.right == a) {
b.parent.right = b;
} else {
b.parent.left = b;
}
}
setBalance(a, b);
returnb;
}
privateNoderotateRight(Nodea) {
Nodeb = a.left;
b.parent = a.parent;
a.left = b.right;
if (a.left != null) {
a.left.parent = a;
}
b.right = a;
a.parent = b;
if (b.parent != null) {
if (b.parent.right == a) {
b.parent.right = b;
} else {
b.parent.left = b;
}
}
setBalance(a, b);
returnb;
}
privateNoderotateLeftThenRight(Noden) {
n.left = rotateLeft(n.left);
returnrotateRight(n);
}
privateNoderotateRightThenLeft(Noden) {
n.right = rotateRight(n.right);
returnrotateLeft(n);
}
privateintheight(Noden) {
if (n == null) {
return -1;
}
returnn.height;
}
privatevoidsetBalance(Node... nodes) {
for (Noden : nodes) {
reheight(n);
n.balance = height(n.right) - height(n.left);
}
}
privatevoidreheight(Nodenode) {
if (node != null) {
node.height = 1 + Math.max(height(node.left), height(node.right));
}
}
}