-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVL tree
More file actions
582 lines (502 loc) · 11.5 KB
/
AVL tree
File metadata and controls
582 lines (502 loc) · 11.5 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
* Java Program to Implement AVL Tree
*/
import java.util.Scanner;
/* Class AVLNode */
class AVLNode
{
AVLNode left, right;
int data;
int height;
/* Constructor */
public AVLNode()
{
left = null;
right = null;
data = 0;
height = 0;
}
/* Constructor */
public AVLNode(int n)
{
left = null;
right = null;
data = n;
height = 0;
}
}
/* Class AVLTree */
class AVLTree
{
private AVLNode root;
/* Constructor */
public AVLTree()
{
root = null;
}
/* Function to check if tree is empty */
public boolean isEmpty()
{
return root == null;
}
/* Make the tree logically empty */
public void makeEmpty()
{
root = null;
}
/* Function to insert data */
public void insert(int data)
{
root = insert(data, root);
}
/* Function to get height of node */
private int height(AVLNode t )
{
return t == null ? -1 : t.height;
}
/* Function to max of left/right node */
private int max(int lhs, int rhs)
{
return lhs > rhs ? lhs : rhs;
}
/* Function to insert data recursively */
private AVLNode insert(int x, AVLNode t)
{
if (t == null)
t = new AVLNode(x);
else if (x < t.data)
{
t.left = insert( x, t.left );
if( height( t.left ) - height( t.right ) == 2 )
if( x < t.left.data )
t = rotateWithLeftChild( t );
else
t = doubleWithLeftChild( t );
}
else if( x > t.data )
{
t.right = insert( x, t.right );
if( height( t.right ) - height( t.left ) == 2 )
if( x > t.right.data)
t = rotateWithRightChild( t );
else
t = doubleWithRightChild( t );
}
else
; // Duplicate; do nothing
t.height = max( height( t.left ), height( t.right ) ) + 1;
return t;
}
/* Rotate binary tree node with left child */
private AVLNode rotateWithLeftChild(AVLNode k2)
{
AVLNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max( height( k2.left ), height( k2.right ) ) + 1;
k1.height = max( height( k1.left ), k2.height ) + 1;
return k1;
}
/* Rotate binary tree node with right child */
private AVLNode rotateWithRightChild(AVLNode k1)
{
AVLNode k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = max( height( k1.left ), height( k1.right ) ) + 1;
k2.height = max( height( k2.right ), k1.height ) + 1;
return k2;
}
/**
* Double rotate binary tree node: first left child
* with its right child; then node k3 with new left child */
private AVLNode doubleWithLeftChild(AVLNode k3)
{
k3.left = rotateWithRightChild( k3.left );
return rotateWithLeftChild( k3 );
}
/**
* Double rotate binary tree node: first right child
* with its left child; then node k1 with new right child */
private AVLNode doubleWithRightChild(AVLNode k1)
{
k1.right = rotateWithLeftChild( k1.right );
return rotateWithRightChild( k1 );
}
/* Functions to count number of nodes */
public int countNodes()
{
return countNodes(root);
}
private int countNodes(AVLNode r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.left);
l += countNodes(r.right);
return l;
}
}
/* Functions to search for an element */
public boolean search(int val)
{
return search(root, val);
}
private boolean search(AVLNode r, int val)
{
boolean found = false;
while ((r != null) && !found)
{
int rval = r.data;
if (val < rval)
r = r.left;
else if (val > rval)
r = r.right;
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
/* Function for inorder traversal */
public void inorder()
{
inorder(root);
}
private void inorder(AVLNode r)
{
if (r != null)
{
inorder(r.left);
System.out.print(r.data +" ");
inorder(r.right);
}
}
/* Function for preorder traversal */
public void preorder()
{
preorder(root);
}
private void preorder(AVLNode r)
{
if (r != null)
{
System.out.print(r.data +" ");
preorder(r.left);
preorder(r.right);
}
}
/* Function for postorder traversal */
public void postorder()
{
postorder(root);
}
private void postorder(AVLNode r)
{
if (r != null)
{
postorder(r.left);
postorder(r.right);
System.out.print(r.data +" ");
}
}
}
/* Class AVL Tree Test */
public class AVLTreeTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of AVLTree */
AVLTree avlt = new AVLTree();
System.out.println("AVLTree Tree Test\n");
char ch;
/* Perform tree operations */
do
{
System.out.println("\nAVLTree Operations\n");
System.out.println("1. insert ");
System.out.println("2. search");
System.out.println("3. count nodes");
System.out.println("4. check empty");
System.out.println("5. clear tree");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
avlt.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to search");
System.out.println("Search result : "+ avlt.search( scan.nextInt() ));
break;
case 3 :
System.out.println("Nodes = "+ avlt.countNodes());
break;
case 4 :
System.out.println("Empty status = "+ avlt.isEmpty());
break;
case 5 :
System.out.println("\nTree Cleared");
avlt.makeEmpty();
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* Display tree */
System.out.print("\nPost order : ");
avlt.postorder();
System.out.print("\nPre order : ");
avlt.preorder();
System.out.print("\nIn order : ");
avlt.inorder();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
AVLTree Tree Test
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
4
Empty status = true
Post order :
Pre order :
In order :
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
10
Post order : 10
Pre order : 10
In order : 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
9
Post order : 9 10
Pre order : 10 9
In order : 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
8
Post order : 8 10 9
Pre order : 9 8 10
In order : 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
7
Post order : 7 8 10 9
Pre order : 9 8 7 10
In order : 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
6
Post order : 6 8 7 10 9
Pre order : 9 7 6 8 10
In order : 6 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
5
Post order : 5 6 8 10 9 7
Pre order : 7 6 5 9 8 10
In order : 5 6 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
4
Post order : 4 6 5 8 10 9 7
Pre order : 7 5 4 6 9 8 10
In order : 4 5 6 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
3
Post order : 3 4 6 5 8 10 9 7
Pre order : 7 5 4 3 6 9 8 10
In order : 3 4 5 6 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
2
Post order : 2 4 3 6 5 8 10 9 7
Pre order : 7 5 3 2 4 6 9 8 10
In order : 2 3 4 5 6 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
1
Post order : 1 2 4 6 5 3 8 10 9 7
Pre order : 7 3 2 1 5 4 6 9 8 10
In order : 1 2 3 4 5 6 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
0
Post order : 0 2 1 4 6 5 3 8 10 9 7
Pre order : 7 3 1 0 2 5 4 6 9 8 10
In order : 0 1 2 3 4 5 6 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
3
Nodes = 11
Post order : 0 2 1 4 6 5 3 8 10 9 7
Pre order : 7 3 1 0 2 5 4 6 9 8 10
In order : 0 1 2 3 4 5 6 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
2
Enter integer element to search
12
Search result : false
Post order : 0 2 1 4 6 5 3 8 10 9 7
Pre order : 7 3 1 0 2 5 4 6 9 8 10
In order : 0 1 2 3 4 5 6 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
2
Enter integer element to search
4
Search result : true
Post order : 0 2 1 4 6 5 3 8 10 9 7
Pre order : 7 3 1 0 2 5 4 6 9 8 10
In order : 0 1 2 3 4 5 6 7 8 9 10
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
5
Tree Cleared
Post order :
Pre order :
In order :
Do you want to continue (Type y or n)
y
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
4
Empty status = true
Post order :
Pre order :
In order :
Do you want to continue (Type y or n)
n