Upcoming SlideShare
×

# Avl trees

324 views

Published on

1 Comment
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• nice ppt

Are you sure you want to  Yes  No
Views
Total views
324
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
0
1
Likes
2
Embeds 0
No embeds

No notes for slide

### Avl trees

1. 1.  First-invented self-balancing binarysearch tree Named after its two inventors,1. G.M. Adelson-Velsky and2. E.M. Landis,› published it in their 1962 paper "Analgorithm for the organization ofinformation."
2. 2.  First, its a binary search tree... L <= P and P <= RRLP
3. 3.  Is this a binary tree search tree?3 9515 211728 343243 514652 545358 6860 71 797712 35 5629 7050
4. 4.  An AVL tree is a balanced binary tree To understand balance we need tounderstand the notion of Tree Height5532 7164 86 Height 0Height 1Height 2
5. 5.  By default, nodes with no children havea height of Height of 0.5532 7164 86 Height 0Height 1Height 2Height0Height 0
6. 6.  But, we must also understand theconcept of Sub-trees5532 7164 86Height 0 Height 1Height 2Height 0 Height 0sub-tree L has aheight of 0 sub-tree R has aheight of 1Height = max(L.height, R.height) + 1
7. 7.  Also empty sub-trees have a Height of -1445891Height = 2 = max(0, 1) + 1Height = 0 = max(-1,-1) + 1Height = max(L.height, R.height) + 1Height = 1 = max(-1, 0) + 1
8. 8.  Anyway, the AVL Balance Property is asfollows... For ALL nodes, the Height of the Left andRight Sub-trees can only differ by 1.P A NodeL R1.. heightRheightL
9. 9. 1. After every insertion2. Check to see if an imbalance wascreated.• All you have to do backtrack up the tree3. If you find an imbalance, correct it.4. As long as the original tree is an AVLtree, there are only 4 types ofimbalances that can occur.
10. 10.  Left-Left (Single Rotation) Right-Right(Single Rotation) Left-Right (Double Rotation) Right-Left(Double Rotation)
11. 11. Single rotation: insert 14, 15, 16, 13, 12, 11, 101415• First insert 14 and15:• Now insert 16.
12. 12. Single rotations:1415• Inserting 16 causes AVL violation:• Need to rotate.16
13. 13. Single rotations:1415• Rotation type:16
14. 14. Single rotations:1415• Rotation restores AVL balance:16
15. 15. Single rotations:141516• Now insert 13 and 12:1312• AVL violation - need torotate.
16. 16. Single rotations:• Rotation type:1415161312
17. 17. Single rotations:131516• Now insert 11.12 14
18. 18. Single rotations:131516• AVL violation – need torotate12 1411
19. 19. Single rotations:• Rotation type:13151612 1411
20. 20. Single rotations:131516121411• Now insert 10.
21. 21. Single rotations:13151612141110• AVL violation – need torotate
22. 22. Single rotations:13151612141110• Rotation type:
23. 23. Single rotations:131516111410 12• AVL balance restored.
24. 24. Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8• First insert 1 and 2:131516111410 12
25. 25. Double rotations:131516111410• AVL violation -rotate1212
26. 26. Double rotations:131516111410• Rotation type:1212
27. 27. Double rotations:• Now insert 3.• AVL balancerestored:1 1013151611142 12
28. 28. Double rotations:• AVL violation –rotate:1 1013151611142 12312
29. 29. Double rotations:• Rotation type:1 1013151611142 123
30. 30. Double rotations:• AVL balancerestored:1 313151610142 1112• Now insert 4.
31. 31. Double rotations:• AVL violation - rotate1 313151610142 11124
32. 32. Double rotations:• Rotation type:1 313151610142 11124
33. 33. Double rotations:1013152111 34 12 14 16• Now insert 5.
34. 34. Double rotations:1013152111 34 12 14 16• AVL violation –rotate.5
35. 35. Single rotations:1013152111 34 12 14 165• Rotation type:
36. 36. Single rotations:1013152111 45 12 14 163• AVL violation –rotate.7
37. 37. Single rotations:1013152111 45 12 14 1637• Rotation type:
38. 38. Double rotations:1013154112 57 12 14 1631• AVL violation -rotate.6
39. 39. Double rotations:1013154112 57 12 14 1631• Rotation type:6
40. 40. Double rotations:1013154112 6712 14 1631• AVL balancerestored.• Now insert 9 and 8.5
41. 41. Double rotations:1013154112 6712 14 1631• Rotation type:985
42. 42. Final tree:1013154112 6812 14 1631• Tree is almost perfectlybalanced597