• Like
  • Save
Avl trees
Upcoming SlideShare
Loading in...5
×
Uploaded on

 

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
143
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
1
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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.  First, its a binary search tree... L <= P and P <= RRLP
  • 3.  Is this a binary tree search tree?3 9515 211728 343243 514652 545358 6860 71 797712 35 5629 7050
  • 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.  By default, nodes with no children havea height of Height of 0.5532 7164 86 Height 0Height 1Height 2Height0Height 0
  • 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.  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.  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. 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.  Left-Left (Single Rotation) Right-Right(Single Rotation) Left-Right (Double Rotation) Right-Left(Double Rotation)
  • 11. Single rotation: insert 14, 15, 16, 13, 12, 11, 101415• First insert 14 and15:• Now insert 16.
  • 12. Single rotations:1415• Inserting 16 causes AVL violation:• Need to rotate.16
  • 13. Single rotations:1415• Rotation type:16
  • 14. Single rotations:1415• Rotation restores AVL balance:16
  • 15. Single rotations:141516• Now insert 13 and 12:1312• AVL violation - need torotate.
  • 16. Single rotations:• Rotation type:1415161312
  • 17. Single rotations:131516• Now insert 11.12 14
  • 18. Single rotations:131516• AVL violation – need torotate12 1411
  • 19. Single rotations:• Rotation type:13151612 1411
  • 20. Single rotations:131516121411• Now insert 10.
  • 21. Single rotations:13151612141110• AVL violation – need torotate
  • 22. Single rotations:13151612141110• Rotation type:
  • 23. Single rotations:131516111410 12• AVL balance restored.
  • 24. Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8• First insert 1 and 2:131516111410 12
  • 25. Double rotations:131516111410• AVL violation -rotate1212
  • 26. Double rotations:131516111410• Rotation type:1212
  • 27. Double rotations:• Now insert 3.• AVL balancerestored:1 1013151611142 12
  • 28. Double rotations:• AVL violation –rotate:1 1013151611142 12312
  • 29. Double rotations:• Rotation type:1 1013151611142 123
  • 30. Double rotations:• AVL balancerestored:1 313151610142 1112• Now insert 4.
  • 31. Double rotations:• AVL violation - rotate1 313151610142 11124
  • 32. Double rotations:• Rotation type:1 313151610142 11124
  • 33. Double rotations:1013152111 34 12 14 16• Now insert 5.
  • 34. Double rotations:1013152111 34 12 14 16• AVL violation –rotate.5
  • 35. Single rotations:1013152111 34 12 14 165• Rotation type:
  • 36. Single rotations:1013152111 45 12 14 163• AVL violation –rotate.7
  • 37. Single rotations:1013152111 45 12 14 1637• Rotation type:
  • 38. Double rotations:1013154112 57 12 14 1631• AVL violation -rotate.6
  • 39. Double rotations:1013154112 57 12 14 1631• Rotation type:6
  • 40. Double rotations:1013154112 6712 14 1631• AVL balancerestored.• Now insert 9 and 8.5
  • 41. Double rotations:1013154112 6712 14 1631• Rotation type:985
  • 42. Final tree:1013154112 6812 14 1631• Tree is almost perfectlybalanced597