-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLNode.java
More file actions
341 lines (282 loc) · 6.93 KB
/
Copy pathAVLNode.java
File metadata and controls
341 lines (282 loc) · 6.93 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
334
335
336
337
338
339
340
341
// package tree;
/**
* The AVLNode class implements a node in a self-balancing AVL binary search tree.
*
* @author Kyle Sadler
* @version 1.0
* @since 2018-10-03
*/
public class AVLNode {
AVLNode left;
AVLNode right;
AVLNode parent;
int key;
Object object;
int depth;
int balanceFactor;
public AVLNode(int key, Object object){
/**
* Creates an AVLNode with key, object pair
*/
this.right = null;
this.left = null;
this.parent = null;
this.key = key;
this.object = object;
this.depth = 1;
this.balanceFactor = 0;
}
public void insert(int key, Object object){
/**
* Adds a key to the current node's subtree recursively.
* This function is initially called by AVLTree on the root node
*
* @param key the key to add to the tree
* @param object the object stored key
*/
if(this.key > key){
// if key is less than, add to the left
if(this.left != null){
this.left.insert(key, object);
} else {
this.left = new AVLNode(key, object);
this.left.parent=this;
}
} else {
// if key is greater than or equal to, add to the right
if(this.right != null){
this.right.insert(key, object);
} else {
this.right = new AVLNode(key, object);
this.right.parent=this;
}
}
this.update(); // O(1)
this.balance(); // O(1)
}
public Object get(int key){
/**
* Retrieve an object from the self-balancing tree in O(log n) time
* @param key the key of the object to retrieve
* @return the object with the given key. returns null if not found
*/
if(this.key == key){
return this.object;
}
if(this.key > key){
// if key is less than, get from left subtree
if(this.left != null){
return this.left.get(key);
} else {
return null;
}
} else {
// if key is greater than, get from right subtree
if(this.right != null){
return this.right.get(key);
} else {
return null;
}
}
}
public void delete(){
/**
* Removes the current node from the tree recursively while maintaining the
* AVL binary search tree structure
*/
if(this.left != null){
// if left-child exists, replace current node with next smallest key
AVLNode nextSmallest = this.left.getMax();
this.key = nextSmallest.key;
nextSmallest.delete();
} else if(this.right != null) {
// if right-child exists, replace current node with next largest key
AVLNode nextLargest = this.right.getMin();
this.key = nextLargest.key;
nextLargest.delete();
} else {
// if node has no children, remove node
if(this.equals(this.parent.right)){
this.parent.right = null;
}else if(this.equals(this.parent.left)){
this.parent.left = null;
}
this.parent.maintainTree(); // O(log n)
}
}
public void maintainTree(){
/**
* Maintains AVL binary search tree structure after a node is deleted
* by recursively updating and balancing each ancestor.
* One call to this method takes O(h) time where h is the height of the
* current node
*/
this.update();
this.balance();
if (this.parent != null){
this.parent.maintainTree();
}
}
public AVLNode getMin(){
/**
* Returns the minimum AVLNode node in the current node's subtree in O(log n) time
* @return node AVLNode with minimum key
*/
if(this.left != null){
return this.left.getMin();
}else{
return this;
}
}
public AVLNode getMax(){
/**
* Returns the maximum AVLNode node in the current node's subtree in O(log n) time
* @return node AVLNode with maximum key
*/
if(this.right != null){
return this.right.getMax();
}else{
return this;
}
}
public void update(){
/**
* Update the depth and balanceFactor for the current node in O(1) time
*/
int rightDepth, leftDepth;
if(this.right != null){
rightDepth = this.right.depth;
} else {
rightDepth = 0;
}
if(this.left != null){
leftDepth = this.left.depth;
} else {
leftDepth = 0;
}
this.depth = Math.max(rightDepth, leftDepth) + 1;
this.balanceFactor = rightDepth - leftDepth;
}
public void balance(){
/**
* Balances the current AVLNode in O(1) time according to the following rules
* 1. If the balanceFactor is greater than 1, rotate the current node to the left
* 2. If the balanceFactor is less than -1. rotate the current node to the right
*/
if(balanceFactor > 1){
this.rotateLeft();
}else if(balanceFactor < -1){
this.rotateRight();
}
}
public void rotateRight(){
/**
* Enacts a right rotation on the current node. Called by this.balance() when
* the current node is left heavy (balanceFactor < -1)
*
* The tree is rotated like so:
*
* p q
* / \ / \
* q c --> a p
* / \ / \
* a b b c
*
* where p is the current node.
*/
// guaranteed to not be null
AVLNode p = this;
AVLNode q = this.left;
// subtrees might be null
AVLNode a = q.left;
AVLNode b = q.right;
AVLNode c = p.right;
// swap p and q
int pKey = p.key;
Object pObject = p.object;
AVLNode pNode = p;
p.key = q.key;
q.key = pKey;
p.object = q.object;
q.object = pObject;
p = q;
q = pNode;
q.right = p;
p.parent = q;
// set subtrees
q.left = a;
p.left = b;
p.right = c;
if(a != null){
a.parent = q;
}
if(b != null){
b.parent = p;
}
if(c != null){
c.parent = p;
}
q.update();
p.update();
}
public void rotateLeft(){
/**
* Enacts a left rotation on the current node. Called by this.balance() when
* the current node is right heavy (balanceFactor > 1).
*
* The tree is rotated like so:
*
* p q
* / \ / \
* c q --> p a
* / \ / \
* b a c b
*
* where p is the current node.
*/
// guaranteed to not be null
AVLNode p = this;
AVLNode q = this.right;
// subtrees might be null
AVLNode b = q.left;
AVLNode a = q.right;
AVLNode c = p.left;
// swap p and q
int pValue = p.key;
Object pObject = p.object;
AVLNode pNode = p;
p.key = q.key;
q.key = pValue;
p.object = q.object;
q.object = pObject;
p = q;
q = pNode;
q.left = p;
p.parent = q;
// set subtrees
q.right = a;
p.right = b;
p.left = c;
if(c != null){
c.parent = p;
}
if(b != null){
b.parent = p;
}
if(a != null){
a.parent = q;
}
q.update();
p.update();
}
public String toString(){
String output = Integer.toString(this.key); // convert to string
if(this.left != null){
output += "(" + this.left.toString() + ")";
}
if(this.right != null){
output += "[" + this.right.toString() + "]";
}
return output;
}
}