-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL_Tree_Example.java
335 lines (274 loc) · 9.66 KB
/
AVL_Tree_Example.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
/**
*
* @author pulkit4tech
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class AVL_Tree_Example implements Runnable {
BufferedReader c;
PrintWriter pout;
// static long mod = 1000000007;
public void run() {
try {
c = new BufferedReader(new InputStreamReader(System.in));
pout = new PrintWriter(System.out, true);
solve();
pout.close();
} catch (Exception e) {
pout.close();
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
new Thread(new AVL_Tree_Example()).start();
}
void solve() throws Exception {
AVL_example();
}
private void AVL_example(){
AVLTree tree = new AVLTree();
/* Constructing tree given in the above figure */
tree.root = tree.insertHelp(tree.root, 9);
tree.root = tree.insertHelp(tree.root, 5);
tree.root = tree.insertHelp(tree.root, 10);
tree.root = tree.insertHelp(tree.root, 0);
tree.root = tree.insertHelp(tree.root, 6);
tree.root = tree.insertHelp(tree.root, 11);
tree.root = tree.insertHelp(tree.root, -1);
tree.root = tree.insertHelp(tree.root, 1);
tree.root = tree.insertHelp(tree.root, 2);
/* The constructed AVL Tree would be
9
/ \
1 10
/ \ \
0 5 11
/ / \
-1 2 6
*/
pout.println("The preorder traversal of constructed tree is : ");
tree.preOrder();
tree.root = tree.deleteNode(tree.root, 10);
/* The AVL Tree after deletion of 10
1
/ \
0 9
/ / \
-1 5 11
/ \
2 6
*/
pout.println("");
pout.println("Preorder traversal after deletion of 10 :");
tree.preOrder();
}
class Node{
int data,height;
Node left,right;
Node(int d){
data = d;
height = 1;
}
}
class AVLTree{
// a) Left Left Case
//
// T1, T2, T3 and T4 are subtrees.
// z y
// / \ / \
// y T4 Right Rotate (z) x z
// / \ - - - - - - - - -> / \ / \
// x T3 T1 T2 T3 T4
// / \
// T1 T2
// b) Left Right Case
//
// z z x
// / \ / \ / \
// y T4 Left Rotate (y) x T4 Right Rotate(z) y z
// / \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
// T1 x y T3 T1 T2 T3 T4
// / \ / \
// T2 T3 T1 T2
// c) Right Right Case
//
// z y
// / \ / \
// T1 y Left Rotate(z) z x
// / \ - - - - - - - -> / \ / \
// T2 x T1 T2 T3 T4
// / \
// T3 T4
// d) Right Left Case
//
// z z x
// / \ / \ / \
// T1 y Right Rotate (y) T1 x Left Rotate(z) z y
// / \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
// x T4 T2 y T1 T2 T3 T4
// / \ / \
// T2 T3 T3 T4
Node root;
int height(Node x){
if(x==null)
return 0;
return x.height;
}
int max(int a,int b){
return Math.max(a, b);
}
Node rightRotate(Node z){
Node temp1 = z.left;
Node temp2 = temp1.right;
//rotate
temp1.left =z;
z.right = temp2;
z.height = max(height(z.left),height(z.right)) + 1;
temp1.height = max(height(temp1.left),height(temp1.right)) + 1;
return temp1;
}
Node leftRotate(Node x) {
Node y = x.right;
Node T2 = y.left;
// Perform rotation
y.left = x;
x.right = T2;
// Update heights
x.height = max(height(x.left), height(x.right)) + 1;
y.height = max(height(y.left), height(y.right)) + 1;
// Return new root
return y;
}
int getBalance(Node x){
if(x==null)
return 0;
return height(x.left) - height(x.right);
}
void insert(int key){
root = insertHelp(root, key);
}
Node insertHelp(Node node,int key){
if(node==null)
return new Node(key);
if(key<node.data)
node.left = insertHelp(node.left, key);
else
node.right = insertHelp(node.right, key);
node.height = max(height(node.left),height(node.right))+1;
int balance = getBalance(node);
// Left Left Case
if (balance > 1 && key < node.left.data) {
return rightRotate(node);
}
// Right Right Case
if (balance < -1 && key > node.right.data) {
return leftRotate(node);
}
// Left Right Case
if (balance > 1 && key > node.left.data) {
node.left = leftRotate(node.left);
return rightRotate(node);
}
// Right Left Case
if (balance < -1 && key < node.right.data) {
node.right = rightRotate(node.right);
return leftRotate(node);
}
return node;
}
Node minValueNode(Node node) {
Node current = node;
/* loop down to find the leftmost leaf */
while (current.left != null) {
current = current.left;
}
return current;
}
Node deleteNode(Node root, int key) {
// STEP 1: PERFORM STANDARD BST DELETE
if (root == null) {
return root;
}
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
if (key < root.data) {
root.left = deleteNode(root.left, key);
}
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if (key > root.data) {
root.right = deleteNode(root.right, key);
}
// if key is same as root's key, then this is the node
// to be deleted
else {
// node with only one child or no child
if ((root.left == null) || (root.right == null)) {
Node temp = null;
if (temp == root.left) {
temp = root.right;
} else {
temp = root.left;
}
// No child case
if (temp == null) {
temp = root;
root = null;
} else // One child case
{
root = temp; // Copy the contents of the non-empty child
}
} else {
// node with two children: Get the inorder successor (smallest
// in the right subtree)
Node temp = minValueNode(root.right);
// Copy the inorder successor's data to this node
root.data = temp.data;
// Delete the inorder successor
root.right = deleteNode(root.right, temp.data);
}
}
// If the tree had only one node then return
if (root == null) {
return root;
}
// STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
root.height = max(height(root.left), height(root.right)) + 1;
// STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to check whether
// this node became unbalanced)
int balance = getBalance(root);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && getBalance(root.left) >= 0) {
return rightRotate(root);
}
// Left Right Case
if (balance > 1 && getBalance(root.left) < 0) {
root.left = leftRotate(root.left);
return rightRotate(root);
}
// Right Right Case
if (balance < -1 && getBalance(root.right) <= 0) {
return leftRotate(root);
}
// Right Left Case
if (balance < -1 && getBalance(root.right) > 0) {
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
void preOrder(){
preOrderHelp(root);
}
void preOrderHelp(Node node) {
if (node != null) {
pout.print(node.data + " ");
preOrderHelp(node.left);
preOrderHelp(node.right);
}
}
}
}