1 / 36

AVL Trees

AVL Trees. When bad trees happen to good programmers. Good tree. Bad tree. Bad tree. Left and right subtrees have the same height!. For every node in the tree, the left and right subtrees of that node have the same height. Too rigid. AVL Trees. (Adelson-Velskii and Landis)

vernon
Download Presentation

AVL Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AVL Trees When bad trees happen to good programmers.

  2. Good tree.

  3. Bad tree.

  4. Bad tree. Left and right subtrees have the same height!

  5. For everynode in the tree, the left and right subtrees of that node have the same height. Too rigid.

  6. AVL Trees • (Adelson-Velskii and Landis) • Binary search trees with an additional property. • For every node in the tree, the height of the left and right subtrees differ by at most 1.

  7. AVL Property • If N is a node in a binary tree, node N has AVL property if the heights of the left sub-tree and right sub-tree are equal or if they differ by 1. Key Point

  8. Height of a Tree • Definition is same as level. Height of a tree is the length of the longest path from root to some leaf node. • Height of an empty tree is -1. • Height of a single node tree is 0. • Recursive definition: • height(t) = 0 if number of nodes = 1 = -1 if T is empty = 1+ max(height(LT), height(RT)) otherwise

  9. 5 3 4 1 2 2 3 1 1 0 0 2 0 1 1 0 0 0 1 0 0 0 0 Checking Balance

  10. 7 4 3 9 2 6 5 8 3 5 8 4 6 7 9 3 8 2 5 6 12 4 7 4 5 9 18 6 8 16 23 Examples

  11. Computing Height height( T:bin_tree ) { if T = null then return -1 HL = height( T->Left) HR = height( T->Right) H = max( HL, HR ) + 1 return H }

  12. Closer Inspection of “Balance” • A tree is balancedif for every node in the tree, the height of the left and right subtrees differ by at most 1. • A node is balanced if • The left and right subtrees are balanced and • The height of the left and right subtrees differ by at most 1 • A tree is balanced if its root node is balanced.

  13. Checking Balance ( Height, Boolean ) balance_test( T:bin_tree ) { if T = null then return (-1, true) ( HL, BL ) = balance_test( T->Left) ( HR, BR ) = balance_test( T->Right) H = max( H_L, H_R ) + 1 if (abs(HL-HR)<=1 && BL=true && BR=true) then B=true else B=false return ( H, B ) }

  14. Operations • Find, FindMin, FindMax • O(log N) time • Insert • Need to maintain balance • O(log N) time • Delete • a little Complicated

  15. For starters… binary search tree permutations of 1, 2, 3

  16. Details • Balance Factor • the difference between the height of a node’s left subtree and the height of its right subtree • Height-Balanced Trees – all of its nodes have a balance factor of 1, 0, or -1.

  17. Examples BF = -2 BF = -1 BF = 0

  18. Examples BF = 2 BF = 1 BF = 0

  19. Inserting a value into an AVL tree 1. Follow the insertion path (if < go left, otherwise go right). • Remember the deepest node with a balance factor of +1 or -1. (This is called the pivot node.) 3. Insert the node at the appropriate point.

  20. Inserting a value into an AVL tree 4. Recompute balance factors from the pivot node on down, including the pivot node. 5. Has the absolute value of the pivot node’s balance factor increased from 1 to 2?

  21. If yes, rebalance the tree!!!

  22. Rebalancing the tree • This has the visual effect of rotating the subtree of the pivot node. • This is also called an AVL rotation. • (Betcha didn’t see that one coming! ☺)

  23. 3 2 1 AVL Balancing : Four Rotations Single right

  24. AVL Balancing : Four Rotations 1 1 3 Double right 2 2 2 2 1 Single right 3 3 3 1 1 1 2 3 Single left Double left 2 2 1 3 1 2 3 3

  25. Let’s take a closer look at the cases The one selected depends on the direction of the “Guilty” insertion, relative to the pivot node.

  26. pivot node 1 Case 1 • The insertion that unbalanced the tree occurred in the left subtree of the left child of the pivot node. BF=1 insert 10 BF=0 12 BF=0 5 BF=0 7 BF=0 3

  27. 1 Case 1 BF=1 BF=2 10 BF=0 BF=1 12 BF=0 5 BF=0 BF=1 7 BF=0 3 BF=0

  28. Case 1 steps for AVL rotation • pivot’s left child becomes new pivot • pivots left child retains its left subtree • old pivot becomes the right child of new pivot (it’s old left child) • old pivot takes the right subtree of its old left child (the new pivot now) and makes it the left subtree • old pivot retains its right subtree

  29. pivot node 5 1 Case 1 10 12 7 3 pivot’s left child becomes new pivot

  30. new pivot node 5 1 3 Case 1 pivots left child retains its left subtree pivot’s left child becomes new pivot

  31. new pivot node 5 1 10 3 Case 1 old pivot becomes the right child of new pivot (it’s old left child) pivots left child retains its left subtree

  32. new pivot node 5 1 10 3 7 Case 1 old pivot becomes the right child of new pivot (it’s old left child) old pivot takes the right subtree of its left child (the new pivot now) and makes it the left subtree

  33. new pivot node 12 5 1 10 3 7 Case 1 old pivot takes the right subtree of its left child (the new pivot now) and makes it the left subtree old pivot retains its right subtree

  34. Case 2 • The insertion that unbalanced the tree occurred in the right subtree of the right child of the pivot node. Case 3 • The insertion that unbalanced the tree occurred in the right subtree of the left child of the pivot node. 3 subcases

  35. Case 4 • The insertion that unbalanced the tree occurred in the left subtree of the right child of the pivot node. 3 subcases

  36. Parting thoughts on AVL trees • Performance really depends on the unreliability of the input data. • i.e., you need almost sorted data to make it worth it! • A lot of design time is involved. • A lot of work, too! • Make sure you need it!

More Related