-
Notifications
You must be signed in to change notification settings - Fork 496
/
AvlTree.java
277 lines (261 loc) · 8.86 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
import java.util.NoSuchElementException;
import java.util.Random;
public class AvlTree {
public static void main(String[] args) {
AVL avl = new AVL();
Random rand = new Random();
int inp = 10;
System.out.println(avl.isEmpty());
for (int i = 0; i < inp; i++) {
avl.root = avl.insert(avl.root, rand.nextInt(100));
}
avl.prntIn(avl.root);
System.out.println(" ");
System.out.println(avl.isEmpty());
avl.delNode(avl.root,avl.root.data);
avl.prntIn(avl.root);
System.out.println(" ");
try {
avl.delNode(avl.root,1111);
} catch(NoSuchElementException e) {
System.out.println("Cannot delete element");
}
}
}
class AVL {
public NodeAVL root;
public AVL(){
root = null;
}
public NodeAVL insert(NodeAVL node, int data) {
// These method takes care of rotation needed after insertion
if (node == null) {
node = new NodeAVL(data);
return node;
} else {
if (node.data > data) {
node.left = insert(node.left, data);
if (node.left == null)
node.hLeft = 0;
else
node.hLeft = Math.max(node.left.hLeft, node.left.hRight) + 1;
} else {
node.right = insert(node.right, data);
if (node.right == null)
node.hRight = 0;
else
node.hRight = Math.max(node.right.hLeft, node.right.hRight) + 1;
}
node = isRotate(node);
}
return node;
}
private NodeAVL rotateLR(NodeAVL node) {
NodeAVL sec = node.left;
NodeAVL temp = sec.right;
node.left = temp;
sec.right = temp.left;
temp.left = sec;
node.left = temp.right;
temp.right = node;
if (node.left == null)
node.hLeft = 0;
else
node.hLeft = Math.max(node.left.hLeft, node.left.hRight) + 1;
if (sec.right == null)
sec.hRight = 0;
else
sec.hRight = Math.max(sec.right.hLeft, sec.right.hRight) + 1;
temp.hLeft = Math.max(sec.hLeft, sec.hRight) + 1;
temp.hRight = Math.max(node.hLeft, node.hRight) + 1;
return temp;
}
private NodeAVL rotateRL(NodeAVL node) {
NodeAVL sec = node.right;
NodeAVL temp = sec.left;
node.right = temp;
sec.left = temp.right;
temp.right = sec;
node.right = temp.left;
temp.left = node;
if (node.right == null)
node.hRight = 0;
else
node.hRight = Math.max(node.right.hLeft, node.right.hRight) + 1;
if (sec.left == null)
sec.hLeft = 0;
else
sec.hLeft = Math.max(sec.left.hLeft, sec.left.hRight) + 1;
temp.hRight = Math.max(sec.hLeft, sec.hRight) + 1;
temp.hLeft = Math.max(node.hLeft, node.hRight) + 1;
return temp;
}
private NodeAVL rotateLL(NodeAVL node) {
NodeAVL temp = node.left;
node.left = temp.right;
temp.right = node;
if (node.left == null)
node.hLeft = 0;
else
node.hLeft = Math.max(node.left.hRight, node.left.hLeft) + 1;
temp.hRight = Math.max(node.hRight, node.hLeft) + 1;
return temp;
}
private NodeAVL rotateRR(NodeAVL node) {
NodeAVL temp = node.right;
node.right = temp.left;
temp.left = node;
if (node.right == null)
node.hRight = 0;
else
node.hRight = Math.max(node.right.hRight, node.right.hLeft) + 1;
temp.hLeft = Math.max(node.hRight, node.hLeft) + 1;
return temp;
}
private NodeAVL isRotate(NodeAVL node) {
// This Method see if there is nessesity for rotation and if
// there is need, it'll do suitable rotation
if (node.hRight - node.hLeft >= 2) {
if (node.right.hRight - node.right.hLeft >= 1)
node = rotateRR(node);
else if (node.right.hRight - node.right.hLeft <= -1)
node = rotateRL(node);
} else if (node.hRight - node.hLeft <= -2) {
if (node.left.hRight - node.left.hLeft <= -1)
node = rotateLL(node);
else if (node.left.hRight - node.left.hLeft >= 1)
node = rotateLR(node);
}
return node;
}
public boolean isEmpty() {
return root == null;
}
public void prntIn(NodeAVL node) {
if (node == null)
return;
else if (node.left == null && node.right == null)
System.out.print(node.data + " ");
else {
prntIn(node.left);
System.out.print(node.data + " ");
prntIn(node.right);
}
}
public void delNode(NodeAVL node, int data) {
// These is the method to delete node if it exist
// Otherwise it throws an exception
NodeAVL root = node;
if (root == null) {
throw new NoSuchElementException("AVL Tree is Empty!!!");
}
if (root.data == data) {
NodeAVL temp = root.right;
if (root.left == null && root.right == null)
root = null;
else if (root.left == null)
root = root.right;
else if (temp == null)
root = root.left;
else {
int dta = 0;
if (root.right.left == null) {
root.right.left = root.left;
root = root.right;
} else {
dta = transverseLeftmost(temp);
root.data = dta;
}
if (root.right == null)
root.hRight = 0;
else
root.hRight = Math.max(root.right.hLeft, root.right.hLeft);
root = isRotate(root);
}
}
else if (node.right == null && node.left == null) {
throw new NoSuchElementException("element you wanna delete not exist");
}
else if (node.right != null && node.right.data == data) {
NodeAVL del = node.right;
if (del.right == null && del.left == null)
node.right = null;
else if (del.left == null)
node.right = del.right;
else if (del.right == null)
node.right = del.left;
else {
NodeAVL temp = del.right;
if (temp.left == null)
node.right = node.right.right;
else
del.data = transverseLeftmost(temp);
del.hRight = Math.max(del.right.hLeft, del.right.hRight);
}
} else if (node.left != null && node.left.data == data) {
NodeAVL del = node.left;
if (del.right == null && del.left == null)
node.left = null;
else if (del.left == null)
node.left = del.right;
else if (del.right == null)
node.left = del.left;
else {
NodeAVL temp = del.right;
if (temp.left == null)
node.left = node.left.right;
del.data = transverseLeftmost(temp);
del.hRight = Math.max(del.right.hLeft, del.right.hRight);
}
} else if (node.data > data) {
delNode(node.left, data);
if (node.left == null)
node.hLeft = 0;
else
node.hLeft = Math.max(node.left.hLeft, node.left.hRight) + 1;
} else if (node.data < data) {
delNode(node.right, data);
if (node.right == null)
node.hRight = 0;
else
node.hRight = Math.max(node.right.hLeft, node.right.hRight) + 1;
}
node = isRotate(node);
}
private int transverseLeftmost(NodeAVL node) {
// These method is special method which comes
// in play when we have to delete a node
// which have both childeren.
if (node.left.left == null) {
int data;
if (node.left != null)
data = node.left.data;
else
data = node.data;
node.left = null;
return data;
}
node = node.left;
int data = transverseLeftmost(node);
if (node.left == null)
node.hLeft = 0;
else
node.hLeft = Math.max(node.left.hLeft, node.left.hLeft);
node = isRotate(node);
return data;
}
}
class NodeAVL {
protected int hLeft;
protected int hRight;
public int data;
public NodeAVL left;
public NodeAVL right;
public NodeAVL(int data) {
hLeft = 0;
hRight = 0;
this.data = data;
left = null;
right = null;
}
}