-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.java
More file actions
391 lines (348 loc) · 13 KB
/
AVLTree.java
File metadata and controls
391 lines (348 loc) · 13 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import java.util.ArrayList;
import java.util.List;
/**
* AVL Tree
* Input-Spell type, with the spell creating new node and insert as a root or new leaf
*/
public class AVLTree {
private Node root;
private int size=0;
private String category; // every tree has his own category
//
/**
* Private Node class for the AVL Tree nodes
* Input - Spell type
*/
private class Node {
private Spell spell;
private Node left=null;
private Node right=null;
private int height;
/**
* Node - Constructor
*/
private Node(Spell spell) {
this.spell = spell;
set_height();
category = spell.getCategory();
}
/**
* Getter
*/
protected Spell get_spell(){
return spell;
}
/**
* Updating height to the branch who got a new leaf node
*/
private void set_height(){
if (left == null && right == null){ //leaf
height = 0;
} else if (left == null) { // only have right son
height = right.height + 1;
} else if (right == null) { // only have left son
height = left.height + 1;
}
else { // max from left and right son heights
if (left.height > right.height){
height = left.height + 1;
}
else {
height = right.height + 1;
}
}
}
}
/**
* AVL tree - Constructor
*/
public AVLTree(Spell spell) {
root = new Node(spell);
size++;
category = spell.getCategory();
}
/**
* Getter for the tree Height - int
*/
public int getTreeHeight(){
if (size == 0){
return -1;
}
return root.height;
}
/**
* Getter for the tree size - int
*/
public int getSize(){
return size;
}
/**
* Getter for the tree category
*/
public String getCategory() { // Every Tree is a category, then the root category is all the nodes category
return root.get_spell().getCategory();
}
/**
* Searching for the node, calling binary search helper func
*/
public Spell search(String spellName, int powerLevel) {
return binary_search_helper(root, spellName, powerLevel);
}
/**
* Searching for the node with the input specific details, if was not found return null
*/
private Spell binary_search_helper(Node temp_root, String spellName, int powerLevel){
if (temp_root != null && temp_root.get_spell().getPowerLevel() == powerLevel && (temp_root.get_spell().getName().equals(spellName))){
return temp_root.get_spell(); // new add
}
if (temp_root == null){ // was not found
return null;
}
if (temp_root.get_spell().getPowerLevel() > powerLevel){ // Go left
return binary_search_helper(temp_root.left, spellName, powerLevel);
}
else {
return binary_search_helper(temp_root.right, spellName, powerLevel); // Go right
}
}
/**
* Inserting new node, first insert like it was binary tree, then arrange it as avl tree and at last update height branch to be safe
*/
public void insert(Spell spell) {
Node new_spell = insert_binary_tree(root, spell);
Node new_node = new Node(spell);
order_by_avl(new_node, root);
height_update(new_node, root);
size++;
}
/**
* Insert as binary tree inserting, getting as an input the new spell and the root
*/
private Node insert_binary_tree(Node temp_root, Spell spell){
if (temp_root == null){ // adding the node
temp_root = new Node(spell);
return temp_root;
} else if (temp_root.get_spell().getPowerLevel() > spell.getPowerLevel()) {
temp_root.left = insert_binary_tree(temp_root.left, spell); // Go left
}
else {
temp_root.right = insert_binary_tree(temp_root.right, spell); // Go right
}
height_update(new Node(spell), this.root); // branch height update
return temp_root;
}
/**
* Updating height, after inserting the avl can change heights to the nodes then we will update branch heights
*/
private void height_update(Node new_leaf, Node temp_root){
if (new_leaf.get_spell().getPowerLevel() != temp_root.get_spell().getPowerLevel()){
if (temp_root.get_spell().getPowerLevel() > new_leaf.get_spell().getPowerLevel()){
height_update(new_leaf, temp_root.left); // go left
}
else{
height_update(new_leaf, temp_root.right); // go right
}
if (temp_root.right != null){
if (temp_root.left == null || temp_root.right.height > temp_root.left.height){
temp_root.height = temp_root.right.height +1;
} // if left is not null and the power is more than right
else {
temp_root.height = temp_root.left.height +1;
}
}
else { // if right is null
if (temp_root.left != null){
temp_root.height = temp_root.left.height +1;
}
}
}
}
/**
* Order the tree to comply with the laws of AVL trees
*/
private void order_by_avl(Node spell, Node temp_root) {
if (temp_root != null) { // Branch going down to the new node add
if (temp_root.get_spell().getPowerLevel() < spell.get_spell().getPowerLevel()) {
order_by_avl(spell, temp_root.right); // Go right
} else if (temp_root.get_spell().getPowerLevel() > spell.get_spell().getPowerLevel()) {
order_by_avl(spell, temp_root.left); // Go left
} else {
return;
}
Node right_son = temp_root.right;
Node left_son = temp_root.left;
// if temp.root != this.root
if (temp_root.height > 2 && right_son != null && left_son != null) {
//right
if (right_son.height - left_son.height == 2){
// right_right
if (right_son.right != null && right_son.left == null){
temp_root.right = right_right(temp_root.right);
return;
}
else if (right_son.right == null && right_son.left != null){
temp_root.right = left_left(temp_root.right);
return;
}
} //left
else if (right_son.height - left_son.height == -2) {
// left_left
if (left_son.left != null && left_son.right == null){
temp_root.left = left_left(temp_root.left);
return;
} // left_right
else if (left_son.left == null && left_son.right != null) {
temp_root.left = right_right(temp_root.left);
return;
}
}
}
if (right_son != null && left_son != null) {
// Right
if (temp_root.height > 1 && right_son.height - left_son.height == 2) {
if (right_son.right == null || (right_son.left != null && right_son.left.height > right_son.right.height)) {
right_left(temp_root);
} else {
right_right(temp_root);
}
}
// Left
else if (temp_root.height > 1 && right_son.height - left_son.height == -2) {
if (left_son.left == null || (left_son.right != null && left_son.right.height < left_son.left.height)) {
left_left(temp_root);
} else {
left_right(temp_root);
}
}
} // o\
// if temp.root == this.root // o\
else if (temp_root.height == 2 && temp_root == this.root) { // o\
// left
if (left_son != null){ // && right_son == null - didn't enter to the first
//left_left
if (left_son.left != null) {
left_left(temp_root);
// left_right
} else if (left_son.right != null) {
left_right(temp_root);
}
// right
} else if (right_son != null) { // && left_son == null - didn't enter to the first
// right_right
if (right_son.right != null){
right_right(temp_root);
// right_left
} else if (right_son.left != null) {
right_left(temp_root);
}
}
}
}
}
/**
* Left_left- need to rotate right
*/
private Node left_left(Node root){
root.height = root.height -2;
return rotate_right(root);
}
/**
* Right_right - need to rotate left
*/
private Node right_right(Node root){
root.height = root.height -2;
this.root.height--;
return rotate_left(root);
}
/**
* Right_left - need to rotate right and then rotate left
*/
private Node right_left(Node root){
root.height = root.height -2;
root.right.height--;
root.right.left.height++;
// Multiple action as rotating right and then rotating left
Node replace_root = root;
Node new_root = root.right.left;
Node right_son_root = root.right;
right_son_root.left = new_root.right;
new_root.right = right_son_root;
root.right = new_root.left;
new_root.left = root;
if (replace_root == this.root){
this.root = new_root;
}
return new_root;
}
/**
* Left_right - need to rotate left and the rotate right
*/
private Node left_right(Node root){
root.height = root.height -2;
root.left.right.height++;
root.left.height--;
// Multiple action as rotating left and then rotating right
Node replace_root = root;
Node left_son = root.left;
Node new_root = root.left.right;
left_son.right = new_root.left;
new_root.left = left_son;
root.left = new_root.right;
new_root.right = root;
if (replace_root == this.root){
this.root = new_root;
}
return new_root;
}
/**
* Left rotation
*/
private Node rotate_left(Node root){
Node replace_root = root; // was the root of the tree
Node right_left = root.right.left;
Node new_root = root.right; // new root
replace_root.right = right_left; // old root new right son
new_root.left = replace_root; // old root is left son of the new root
if (replace_root == this.root) { // if the root is the original root then update it
this.root = new_root;
}
return new_root;
}
/**
* Right rotation
*/
private Node rotate_right(Node root){
Node replace_root = root; // was the root of the tree
Node left_right = root.left.right;
Node new_root = root.left; // new root
replace_root.left = left_right; // old root new left son
new_root.right = replace_root; // old root is right son of the new root
if (replace_root == this.root) { // if the root is the original root then update it
this.root = new_root;
}
return new_root;
}
/**
* Returning the biggest Top K Spells power level and getting the help of TopK helper
*/
public List<Spell> getTopK(int k) {
List<Spell> list = new ArrayList<>(k);
TopK_helper(list, k, this.root);
return list;
}
/**
* Reverse inorder to get the biggest Top K Spells power level
*/
private void TopK_helper(List<Spell> list, int k, Node root){
// reverse inorder
if (root.right != null){
TopK_helper(list, k, root.right); // Go left
}
if (list.size() < k ) {
list.add(root.get_spell());
}
if (root.left != null){
TopK_helper(list, k, root.left); // Go right
}
}
}