1 / 17

AVL Tree Example (This is the example we did in tutorial on Thursday)

AVL Tree Example (This is the example we did in tutorial on Thursday). Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008. AVL trees are self-balancing binary search trees. Each node of an AVL tree is assigned a balance factor as follows:.

pearl-brock
Download Presentation

AVL Tree Example (This is the example we did in tutorial on Thursday)

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 Tree Example(This is the example we did in tutorial on Thursday) Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008

  2. AVL trees are self-balancing binary search trees. Each node of an AVL tree is assigned a balance factor as follows: • -1 if the depth of the node’s left subtree is 1 more than the depth of the right node’s left sub tree • +1 if the depth of the node’s left sub tree is 1 more than the depth of the right node’s left subtree • 0 if both of the node’s subtrees has the same depths • -2 if the depth of the node’s left subtree is 2 more than the depth of the right node’s left sub tree • +2 if the depth of the node’s left sub tree is 2 more than the depth of the right node’s left subtree • If the balance factor becomes -2 or +2, the tree must be rebalanced. Next: Rotations -->

  3. If the balance factor of a node in an AVL tree is +2 or -2, the node is rotated to re-balance the tree using one of the four cases shown in the following picture: The 4 cases of AVL tree rebalancing using rotations. Picture created by Marc Tanti, licensed for reuse under the GNU Free Documentation License, Version 1.2 Next: example -->

  4. Balance ok +1 72 -1 25 -1 94 0 0 0 0 0 0 0 0 38 57 78 26 63 37 3 47 Initial AVL tree with balance factors: Next step: insert 1 -->

  5. Balance not ok (Balance factor of -2 is not allowed) -1 57 -1 +1 26 72 -2 25 -1 94 -1 0 0 0 0 0 47 3 38 78 63 37 0 1 Insert 1 and recalculate balance factors Next step: Find rebalancing case -->

  6. Balance not ok -1 57 -1 +1 26 72 -2 25 0 0 0 0 0 78 37 63 38 47 -1 3 0 1 Find rebalancing case Left Left Case -1 94 Pivot Next step: apply Left Left rebalancing -->

  7. Balance ok 0 57 0 +1 26 72 0 25 0 -1 3 94 0 0 0 0 0 38 78 63 37 47 0 1 Rebalance and recalculate balance factors Next step: insert 30 -->

  8. Balance ok -1 57 +1 +1 26 72 0 25 -1 38 0 0 0 63 3 78 0 1 -1 37 0 47 0 30 Insert 30 and recalculate balance factors -1 94 Next step: Insert 32 -->

  9. Balance not ok -2 57 +2 +1 26 72 0 25 -2 -1 38 94 0 0 0 63 78 3 0 1 -2 37 0 47 +1 30 0 32 Insert 32 and recalculate balance factors Next step: Find rebalancing case -->

  10. Balance not ok -2 57 +2 +1 26 72 0 25 -2 -1 38 94 0 0 0 3 63 78 0 1 -2 37 0 47 Find rebalancing case +1 30 Left Right Case 0 32 Next step: Rebalance (Step 1) -->

  11. Balance not ok -2 57 +2 +1 26 72 0 25 -2 -1 38 94 0 0 0 63 78 3 0 1 -2 37 0 47 0 30 -1 32 Rebalance (Step 1) Next step: Rebalance (Step 2) -->

  12. Balance ok -1 57 +1 +1 26 72 0 25 -1 -1 38 94 0 0 0 63 78 3 0 1 0 37 0 47 0 30 0 32 Rebalance (Step 2) and recalculate balance factors Next step: Insert 35 -->

  13. Balance not ok -2 57 +2 +1 26 72 0 25 -2 -1 38 94 0 0 0 78 63 3 0 1 -1 37 +1 0 32 47 0 30 0 35 Insert 35 and recalculate balance factors Next step: Find rebalancing case -->

  14. Balance not ok -2 57 +2 +1 26 72 0 25 -2 -1 38 94 0 0 0 3 78 63 0 1 +1 0 32 47 0 -1 30 37 0 35 Insert 35 Start from first spot (from bottom of tree) where balance factor is incorrect. Left Right Case Next step: Rebalance (Step 1) -->

  15. Balance not ok -2 57 +2 +1 26 72 0 25 -2 -1 38 94 0 0 0 78 63 3 0 1 0 0 32 47 0 -2 30 37 0 35 Rebalance (Step 1) Next step: Rebalance (Step 2) -->

  16. Balance ok -1 57 +1 +1 26 72 0 25 +1 -1 38 94 0 0 0 78 63 3 0 1 0 0 32 47 0 0 30 37 0 35 Rebalance (Step 2) Next step: Finished! -->

  17. Balance ok -1 57 +1 +1 26 72 0 25 +1 -1 38 94 0 0 0 3 63 78 0 1 0 0 32 47 0 30 0 35 Finished! 0 37 Exercise: insert 36

More Related