-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAVL.java
355 lines (282 loc) · 9.62 KB
/
AVL.java
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
import java.util.Stack;
/**
* Tree Node for AVL tree
*
* @author Huiping Cao
*
*/
class AVLNode{
private int data; //the element value for this node
private AVL left; //the left child of this node
private AVL right; //the right child of this node
private int height; //height of the tree rooted at this node
public AVLNode() { data = 0; left = new AVL(); right = new AVL(); height = 0;}
public AVLNode(int initData) { data = initData; left = new AVL(); right = new AVL(); height = 0;}
/**
* Constructor with the initial element, initial left and right children
* @param initData: the initial element
* @param initLeft: left child
* @param initRight: right child
*/
public AVLNode(int initData, AVL initLeft, AVL initRight){
data = initData;
left = initLeft;
right = initRight;
height = 1;
}
public int getData() { return data; }
public AVL getLeft() { return left; }
public AVL getRight() { return right; }
public int getHeight() { return height;}
public void setData(int data) {this.data = data;}
public void setLeft(AVL left) { this.left = left;}
public void setRight(AVL right) { this.right = right;}
/**
* Set the height of the tree rooted at this node
*/
public void setHeight()
{
this.height = 1+Math.max(getLeftHeight(), getRightHeight());
}
/**
* Convert this BTNode to a string
*/
public String toString()
{
String str="";
if(left==null) str +="(null)";
else str +="("+left.getRoot().getData()+")";
str += data;
if(right==null) str +="(null)";
else str +="("+right.getRoot().getData()+")";
return str;
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//Rebalancing related methods
/**
* Get the height of the left subtree
*/
public int getLeftHeight(){
if(left==null||left.getRoot()==null) return 0;
else return left.getRoot().getHeight();
}
/**
* Get the height of the right subtree
* @return: the height of the right sub-tree
*/
public int getRightHeight(){
if(right==null||right.getRoot()==null) return 0;
else return right.getRoot().getHeight();
}
}
public class AVL {
private AVLNode root; //instance variable to denote the root of the AVL tree
//Constructors for the AVL tree
public AVL() {root = null;}
public AVL(int e) {root = new AVLNode(e,new AVL(),new AVL());}
//Basic set and get methods
public AVLNode getRoot() {return root;}
public void setRoot(AVLNode _root) {this.root = _root;}
public boolean isEmpty() {return (root==null);}
private AVL getLeftSubtree() { return root.getLeft(); }
private AVL getRightSubtree() { return root.getRight(); }
private void setHeight() {root.setHeight(); }
/**
* Check whether the tree (rooted at this node) is right heavy or not
* @return
*/
private boolean rightHeavy(){
return (root.getLeftHeight() < root.getRightHeight());
}
/**
* Check whether the tree (rooted at this node) is left heavy or not
* @param node
* @return
*/
private boolean leftHeavy(){
return (root.getLeftHeight() > root.getRightHeight());
}
/**
* Check whether the tree (rooted at this node) is right too heavy or not
* @return
*/
private boolean rightTooHeavy(){
return ((root.getLeftHeight()+1)< root.getRightHeight());
}
/**
* Check whether the tree (rooted at this node) is left too heavy or not
* @param node
* @return
*/
private boolean leftTooHeavy(){
return (root.getLeftHeight() > (root.getRightHeight()+1));
}
/**
* Traversal the tree in-order and print it
*/
public void inOrderTraversal(){
inOrderTraversal(0);
}
/**
* Private function to print the tree with in-order traversal
* @param indentation: the number of space before the data, to make the printing more beautiful
*/
private void inOrderTraversal(int indentation){
if(root!=null){
if(root.getRight()!=null)root.getRight().inOrderTraversal(indentation+1);
for(int i=0;i<indentation*2;i++)
System.out.print(" ");
System.out.println(root.getData());
if(root.getLeft()!=null)root.getLeft().inOrderTraversal(indentation+1);
}
}
public String inOrderStr()
{
if(root!=null)
return (root.getLeft().inOrderStr() + " " + root.getData() + " " + root.getRight().inOrderStr());
else
return "";
}
/**
* Print the tree using pre-order traversal strategy recursively.
*/
public void preOrderPrtRecursive(){
if(root==null) return;
System.out.print(root.getData()+" ");
this.getLeftSubtree().preOrderPrtRecursive();
this.getRightSubtree().preOrderPrtRecursive();
}
private void rotateLeft() {
AVL treeB = new AVL();
treeB.root = getRightSubtree().root;
AVL T1 = getRightSubtree().getLeftSubtree();
getRightSubtree().root = T1.root;
treeB.getLeftSubtree().root = this.root;
this.root = treeB.root;
getLeftSubtree().setHeight();
treeB.setHeight();
treeB.getLeftSubtree().setHeight();
this.setHeight();
}
private void rotateRight() {
AVL b = new AVL();
b.root = getLeftSubtree().root;
getLeftSubtree().root = getLeftSubtree().getRightSubtree().root;
b.getRightSubtree().root = this.root;
this.root = b.root;
getRightSubtree().setHeight();
b.setHeight();
b.getRightSubtree().setHeight();
this.setHeight();
}
private void doubleRotateRight() {
getRightSubtree().rotateRight();
this.rotateLeft();
}
private void doubleRotateLeft() {
getLeftSubtree().rotateLeft();
this.rotateRight();
}
public void insert(int e) {
if (root == null) {
root = new AVLNode(e);
root.setHeight();
//this.rebalance();
return;
}
if (e <= root.getData()) {
//rebalance();
getLeftSubtree().insert(e);
setHeight();
rebalance();
}
else {
//rebalance();
getRightSubtree().insert(e);
setHeight();
rebalance();
}
}
private void rebalance() {
if(root == null)
return;
//System.out.println("in rebalance, this.data: " + this.root.getData() + "\ngetLeftHeight: " + root.getLeftHeight() + " and getRightHeight: " + root.getRightHeight());
if(leftTooHeavy()) {
System.out.println("in rebalance, leftTooHeavy()");
if(getLeftSubtree().rightHeavy())
getLeftSubtree().rotateLeft();
rotateRight();
} else if(rightTooHeavy()) {
System.out.println("in rebalance, rightTooHeavy()");
if(getRightSubtree().leftHeavy())
getRightSubtree().rotateRight();
rotateLeft();
}
//getLeftSubtree().rebalance();
//getRightSubtree().rebalance();
}
private void removeCurrentNode() {
AVL left = getLeftSubtree();
AVL right = getRightSubtree();
if(left.root == null && right.root == null)
this.root = null;
else if (left.root == null && right.root != null)
this.root = right.root;
else if (left.root != null && right.root == null)
this.root = left.root;
else {
AVL rover = left;
while(rover.getRightSubtree().root != null) {
rover = rover.getRightSubtree();
}
this.root = rover.root;
this.root.setRight(right);
this.setHeight();
this.rebalance();
}
}
public boolean remove(int e) {
if(root == null) {
return false;
}
if(e == root.getData()) {
this.removeCurrentNode();
return true;
} else if (e < root.getData()) {
boolean result = getLeftSubtree().remove(e);
setHeight();
rebalance();
return result;
} else {
boolean result = getRightSubtree().remove(e);
setHeight();
rebalance();
return result;
}
}
public int countOccurrences(int e) {
if(root == null) return 0;
if(root.getData() == e) return 1 + getLeftSubtree().countOccurrences(e) + getRightSubtree().countOccurrences(e);
else return 0 + getLeftSubtree().countOccurrences(e) + getRightSubtree().countOccurrences(e);
}
public void preOrderPrtNonRecursive() {
if(root == null)
return;
/*
AVL roverPrev = this;
AVL rover = roverPrev.getLeftSubtree();*/
Stack<AVL> s = new Stack<>();
s.push(this);
while(!s.empty()) {
AVL curr = s.pop();
System.out.print(curr.getRoot().getData() + " ");
//System.out.println("\ncurr.getRoot().getRightSub(): " + curr.getRoot().getRight());
//System.out.println("\ncurr.getRoot().getLeftSub(): " + curr.getRoot().getLeft());
if(curr.getRightSubtree().getRoot() != null)
s.push(curr.getRightSubtree());
if(curr.getLeftSubtree().getRoot() != null)
s.push(curr.getLeftSubtree());
}
}
}