-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLTree.java
329 lines (220 loc) · 7.16 KB
/
AVLTree.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
import java.util.ArrayList;
import java.util.List;
/** Implementation of a proper linked Binary Tree with labels of type E */
public class AVLTree {
/** reference to the root: */
private BTNode root;
/** number of nodes: */
private int size;
/** default constructor: */
public AVLTree() {
root = new BTNode(null,null,null,null);
size = 1;
}
/**
*
*
* check if the tree is height balanced
* @return
*/
public boolean checkHBE(){
return checkHBE(root);
}
public boolean checkHBE(BTNode node){
//if we reached the leaf then return true
if(node.isLeaf()){
return true;
}
//get the height of left tree and height or right tree
int leftHeight = height(node.leftChild());
int rightHeight= height(node.rightChild());
int hbe = leftHeight- rightHeight;
//check if the difference between heights of right and left tree are less than 2
//if so then go down the tree and check the height balanced property for sub trees
if(Math.abs(hbe)<=1 && checkHBE(node.leftChild()) && checkHBE(node.rightChild())){
return true;
}
return false;
}
/**
*
*
* checks if the tree if binary search tree
* @return
*/
public boolean checkSearchTree(){
if(root!=null){
//we pass the values of min integer and max integer to
// help us to keep track of parent values in the tree
//because its not enough to check only locally if
//the parent is bigger than left child and smaller than
//right child , we must check that all values in left sub
//tree are smaller and all values in right tree are bigger
return checkSearchTree(root, Integer.MIN_VALUE,Integer.MAX_VALUE);
}
return true;
}
private boolean checkSearchTree(BTNode node, int min, int max){
if(node.isLeaf()){
return true;
}
if (node.element()<min || node.element()>max){
return false;
}
return checkSearchTree(node.leftChild(),min,node.element()) &&
checkSearchTree(node.rightChild(),node.element(),max);
}
public static String outputBT(AVLTree t){
String str="";
if(t.root!=null){
//call the method on root
str=outputBT(t.root);
}
return str;
}
/*
*
* construct this art " 8(5(9, 3), 3(6, 2(5, 1)))."
* of notation recursively
*
*/
public static String outputBT(BTNode node){
String output="";
//if node is a leaf , simply add and number to the
//string
if (node.isLeaf()){
return ""+node.element();
}
output+= node.element();
//if the node has left child then add "(" to string and go
//down
if(node.leftChild()!=null){
output+="("+outputBT(node.leftChild());
}
//if the node has right child
//add "," to string and go down the free to that right child
//then we return to the current level
//we close our string with ")"
if(node.rightChild()!=null){
output+= ","+outputBT(node.rightChild())+")";
}
return output;
}
public static void height(AVLTree t ){
height(t.root);
}
/**
* change the entries values of nodes
* to their height
* calculates height recursively
*
* @param node
* @return height of current node
*
*/
public static Integer height(BTNode node){
if(node.isLeaf()){
return 0;
}
int leftt = height(node.leftChild());
//go to right child node
int rigtht =height(node.rightChild());
//choose the biggest height of two trees
int height = 1+ Math.max(leftt, rigtht) ;
return height;
}
/**
/** accessor methods: */
public int size() { return size; }
public boolean isTrivial() { return (size==1); }
/** a trivial Tree consists of the root node only
* empty trees are excluded */
public boolean isInternal(BTNode v) {return v.isInternal(); }
public boolean isLeaf(BTNode v) {return v.isLeaf(); }
public boolean isRoot(BTNode v) { return (v==root()); }
public BTNode root() { return root; }
public BTNode leftChild(BTNode v) { return v.leftChild(); }
public BTNode rightChild(BTNode v) { return v.rightChild(); }
public BTNode sibling(BTNode v) {
BTNode p = parent(v);
BTNode lc = leftChild(p);
if (v == lc)
return rightChild(p);
else
return lc;
}
public BTNode parent(BTNode v) { return v.parent(); }
/** update methods: */
/** turn a leaf v to an inner node by appending two new leaves
* if v is not a leaf, the method won't do anything
* alternatively one could throw an exception */
public void expandExternalNode(BTNode v){
if( isLeaf(v)){
v.setLeft(new BTNode(null,v,null,null));
v.setRight(new BTNode(null,v,null,null));
size += 2;
}
}
/** delete a leaf v and replace v's parent node by v's sibling
* if v is the root or if v is not a leaf, the method won't do anything
* alternatively one could throw an exception */
public void removeAboveExternalNode(BTNode v){
if( isLeaf(v) && !isRoot(v)){
BTNode p = parent(v);
BTNode s = sibling(v);
if( isRoot(p)){
s.setParent(null);
root = s;
}
else{
BTNode g = parent(p);
if( p == leftChild(g))
g.setLeft(s);
else
g.setRight(s);
s.setParent(g);
}
size -= 2;
}
}
public class BTNode {
private Integer element;
private BTNode left, right, parent;
/** default constructor */
public BTNode() { }
/** constructor with parameters */
public BTNode(Integer e, BTNode u, BTNode v, BTNode w) {
setElement(e);
setParent(u);
setLeft(v);
setRight(w);
}
/** accessor methods: */
public Integer element() { return element; }
public void setElement(Integer e) { element=e; }
public BTNode leftChild() { return left; }
public void setLeft(BTNode v) { left=v; }
public BTNode rightChild() { return right; }
public void setRight(BTNode v) { right=v; }
public BTNode parent() { return parent; }
public void setParent(BTNode v) { parent=v; }
/** methods for checking basic properties: */
public boolean isLeaf() {
return ( left==null && right==null);
}
public boolean isInternal() {
return ( left!=null || right!=null);
}
/** note that in proper binary trees one could use && instead of || */
public boolean isRoot() { return (parent==null); }
}
/** Just a short test: */
public static void main(String[] args){
AVLTree bt = new AVLTree();
BTNode v = bt.new BTNode();
(bt.root()).setLeft(v);
bt.expandExternalNode(v);
bt.expandExternalNode(v.leftChild());
System.out.println(" Size = " + bt.size());
}
}