-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.java
More file actions
333 lines (278 loc) · 8.96 KB
/
AVLTree.java
File metadata and controls
333 lines (278 loc) · 8.96 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
322
323
324
325
326
327
328
329
330
331
332
333
package org.gra4j.trochilus;
import java.util.LinkedList;
import java.util.Queue;
public class AVLTree<E extends Comparable<? super E>> {
private AVLNode<E> root;
private static final int ALLOWED_IMBALANCE = 1;
private static class AVLNode<E extends Comparable<? super E>> {
E element;
AVLNode<E> left;
AVLNode<E> right;
AVLNode<E> parent;
int height;
AVLNode (E e) {
this(e, null, null, null);
}
AVLNode (E e, AVLNode<E> parent) {
this(e, null, null, parent);
}
AVLNode (E e, AVLNode<E> lt, AVLNode<E> rt, AVLNode<E> pt) {
element = e;
left = lt;
right = rt;
parent = pt;
height = 0;
}
}
public AVLTree (E element) {
root = new AVLNode<>(element);
}
public AVLNode<E> getRoot() {
return root;
}
public void setRoot(AVLNode<E> root) {
this.root = root;
}
public void makeEmpty () {
root = null;
}
public boolean isEmpty () {
return root == null;
}
public int height () {
return height(root);
}
public int height (AVLNode<E> t) {
return t == null ? -1 : t.height;
}
public AVLNode<E> insert (E element) {
root = insert(element, root);
return root;
}
private AVLNode<E> insert (E element, AVLNode<E> t) {
if (t == null)
return new AVLNode<>(element);
int compareResult = element.compareTo(t.element);
if (compareResult < 0) {
t.left = insert(element, t.left);
t.left.parent = t;
} else if (compareResult > 0) {
t.right = insert(element, t.right);
t.right.parent = t;
} else
;
return balance(t);
}
public AVLNode<E> insertN (E element) {
if (root == null)
return root = new AVLNode<>(element);
AVLNode<E> node = root;
while (node != null) {
int compareResult = element.compareTo(node.element);
if (compareResult < 0) {
if (node.left == null) {
AVLNode<E> left = node.left = new AVLNode<>(element, node);
root = balanceN(left);
break;
} else
node = node.left;
} else if (compareResult > 0) {
if (node.right == null) {
AVLNode<E> right = node.right = new AVLNode<>(element, node);
root = balanceN(right);
break;
} else
node = node.right;
} else
;
}
return node;
}
private AVLNode<E> balanceN (AVLNode<E> node) {
AVLNode<E> eavlNode = null, preNode = null;
while (node != null) {
if (eavlNode != null && preNode != eavlNode) {
if (node.left == preNode)
node.left = eavlNode;
else if (node.right == preNode)
node.right = eavlNode;
}
AVLNode<E> parent = node.parent;
preNode = node;
eavlNode = balance(node);
node = parent;
}
return eavlNode;
}
public AVLNode<E> remove (E element) {
root = remove(element, root);
return root;
}
private AVLNode<E> remove (E element, AVLNode<E> t) {
if (t == null)
return t;
int compareResult = element.compareTo(t.element);
if (compareResult < 0)
t.left = remove(element, t.left);
else if (compareResult > 0)
t.right = remove(element, t.right);
else if (t.left != null && t.right != null){
t.element = findMin(t.right).element;
t.right = remove(t.element, t.right);
} else {
AVLNode<E> parent = t.parent;
t = (t.left != null) ? t.left : t.right;
if (t != null)
t.parent = parent;
}
return balance(t);
}
public AVLNode<E> removeN (E element) {
if (root == null)
return root;
AVLNode<E> node = root;
while (node != null) {
int compareResult = element.compareTo(node.element);
if (compareResult < 0)
node = node.left;
else if (compareResult > 0)
node = node.right;
else if (node.left != null && node.right != null) {
element = node.element = findMin(node.right).element;
node = node.right;
} else {
AVLNode<E> parent = node.parent;
if (parent.left == node)
parent.left = node = (node.left != null) ? node.left : node.right;
else if (parent.right == node)
parent.right = node = (node.left != null) ? node.left : node.right;
if (node != null)
node.parent = parent;
balanceN(parent);
break;
}
}
return node;
}
public E findMin () {
AVLNode<E> min = findMin(root);
return min == null ? null : min.element;
}
public AVLNode<E> findMin (AVLNode<E> t) {
if (t == null || t.left == null)
return t;
return findMin(t.left);
}
public E findMax () {
AVLNode<E> max = findMax(root);
return max == null ? null : max.element;
}
public AVLNode<E> findMax (AVLNode<E> t) {
if (t == null || t.right == null)
return t;
return findMax(t.right);
}
public boolean contains (E element) {
return contains(element, root);
}
public boolean contains (E element, AVLNode<E> t) {
if (t == null)
return false;
int compareResult = element.compareTo(t.element);
if (compareResult < 0)
return contains(element, t.left);
else if (compareResult > 0)
return contains(element, t.right);
else
return true;
}
public void printTree () {
if (isEmpty())
System.out.println("Empty tree");
else
printTree(root);
}
private void printTree (AVLNode<E> t) {
if (t == null)
return;
printTree(t.left);
System.out.println(t.element);
printTree(t.right);
}
public void levelOrder () {
if (isEmpty())
System.out.println("Empty tree");
else
levelOrder(root);
}
private void levelOrder (AVLNode<E> t) {
if (t == null)
return;
Queue<AVLNode<E>> q = new LinkedList<>();
q.add(t);
while (!q.isEmpty()) {
AVLNode<E> node = q.poll();
if (node != null) {
System.out.println(node.element);
q.add(node.left);
q.add(node.right);
}
}
}
private AVLNode<E> balance (AVLNode<E> t) {
if (t == null)
return t;
if (height(t.left) - height(t.right) > ALLOWED_IMBALANCE)
if (height(t.left.left) >= height(t.left.right))
t = rotateWithLeftChild(t);
else
t = doubleWithLeftChild(t);
else if (height(t.right) - height(t.left) > ALLOWED_IMBALANCE)
if (height(t.right.right) >= height(t.right.left))
t = rotateWithRightChild(t);
else
t = doubleWithRightChild(t);
t.height = Math.max(height(t.left), height(t.right)) + 1;
return t;
}
private AVLNode<E> rotateWithLeftChild (AVLNode<E> t) {
AVLNode<E> lt = t.left;
t.left = lt.right;
if (t.left != null)
t.left.parent = t;
lt.right = t;
lt.parent = t.parent;
t.parent = lt;
t.height = Math.max(height(t.left), height(t.right)) + 1;
lt.height = Math.max(height(lt.left), height(lt.right)) + 1;
return lt;
}
private AVLNode<E> rotateWithRightChild (AVLNode<E> t) {
AVLNode<E> rt = t.right;
t.right = rt.left;
if (t.right != null)
t.right.parent = t;
rt.left = t;
rt.parent = t.parent;
t.parent = rt;
t.height = Math.max(height(t.left), height(t.right)) + 1;
rt.height = Math.max(height(rt.left), height(rt.right)) + 1;
return rt;
}
private AVLNode<E> doubleWithLeftChild(AVLNode<E> t) {
t.left = rotateWithRightChild(t.left);
return rotateWithLeftChild(t);
}
private AVLNode<E> doubleWithRightChild(AVLNode<E> t) {
t.right = rotateWithLeftChild(t.right);
return rotateWithRightChild(t);
}
public AVLNode<E> changeLeftRight (AVLNode<E> node) {
if (node == null)
return null;
AVLNode<E> left = node.left;
node.left = changeLeftRight(node.right);
node.right = changeLeftRight(left);
return node;
}
}