1 / 30

AVL-Trees: Motivation

AVL-Trees: Motivation. Recall our discussion on BSTs The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into an empty BST Problem : Lack of “balance” – Tree becomes highly asymmetric and degenerates to a linked-list!!

kacy
Download Presentation

AVL-Trees: Motivation

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: Motivation • Recall our discussion on BSTs • The height of a BST depends on the order of insertion • E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into an empty BST • Problem: Lack of “balance” – Tree becomes highly asymmetric and degenerates to a linked-list!! • Since all operations take O(h) time, where log N <= h <= N-1, worst case running time of BST operations are O(N) • Question: Can we make sure that regardless of the order of key insertion, the height of the BST is log(n)? In other words, can we keep the BST balanced?

  2. Height-Balanced Trees • Many efficient algorithms exist for balancing BSTs in order to achieve faster running times for the BST operations • Adelson-Velskii and Landis (AVL) trees (1962) • Splay trees and other self-adjusting trees (1978) • B-trees and other multiway search trees (1972) • Red-Black trees (1972) • Also known as Symmetric Binary B-Trees • Will not be covered in this course

  3. AVL Trees: Formal Definition • All empty trees are AVL-trees • If T is a non-empty binary search tree with TL and TR as its left and right sub-trees, then T is an AVL tree iff • TL and TR are AVL trees • |hL – hR| <= 1, where hL and hR are the heights of TL and TR respectively TR TL hL+1 or hL-1 hL

  4. 0 6 -1 1 7 4 0 1 0 9 Red numbers are Balance Factors AVL Trees • AVL trees are height-balanced binary search trees • Balance factor of a node = height(left subtree) - height(right subtree) • An AVL tree can only have balance factors of –1, 0, or 1 at every node • For every node, heights of left and right subtree differ by no more than 1 An AVL Tree

  5. AVL Trees: Examples and Non-Examples 0 0 1 6 6 6 0 -1 1 1 0 9 0 4 7 5 9 4 0 0 1 8 0 0 5 4 0 1 0 9 5 0 An AVL Tree An AVL Tree An AVL Tree 0 -1 -1 6 6 6 2 1 1 7 -2 4 9 2 4 10 2 4 -1 1 8 3 -1 0 0 7 0 8 1 1 0 1 0 0 8 9 9 7 0 0 Non-AVL Tree Non-AVL Tree Non-AVL Tree Red numbers are Balance Factors

  6. AVL Trees: Implementation • To implement AVL-trees, associate a height with each node “x” C++ Declaration structAVLTreeNode{ int key; int height; AVLTreeNodeleft; AVLTreeNoderight; }; x key left right height • Balance factor (bf) of x = height of leftsubtree of x – height of rightsubtree of x • In an AVL-tree, “bf” can be one of {-1, 0, 1}

  7. 0 6 -1 1 7 5 0 4 0 9 Red numbers are Balance Factors Good News about AVL Trees • Can prove: Height of an AVL tree of N nodes is always O(log N) • How? Can show: • Height h = 1.44 log(N) • Prove using recurrence relation for minimum number of nodes S(h) in an AVL tree of height h: • S(h) = S(h-1) + S(h-2) + 1 • Use Fibonacci numbers to get bound on S(h) bound on height h An AVL Tree Height = O(logN)

  8. Good and Bad News about AVL Trees • Good News: • Search takes O(h) = O(logN) • Bad News • Insert and Delete may cause the tree to be unbalanced! Insert 3 0 1 6 6 -1 2 1 -1 7 5 7 5 0 4 4 1 0 9 0 9 3 0 An AVL Tree No longer an AVL Tree

  9. Restoring Balance in an AVL Tree • Problem: Insert may cause balance factor to become 2 or –2 for some node on the path from root to insertion point • Idea: After Inserting the new node • Back up towards root updating balance factors along the access path • If Balance Factor of a node = 2 or –2, adjust the tree by rotation around deepest such node. 1 6 2 -1 7 5 4 1 9 0 3 0 Non- AVL Tree

  10. Restoring Balance: Example Insert 3 Rotate 0 1 0 6 6 6 2 -1 -1 1 -1 7 7 5 5 7 0 4 0 4 4 1 5 3 0 0 9 9 0 0 9 0 3 0 AVL AVL Not AVL • After Inserting the new node • Back up towards root updating heights along the access path • If Balance Factor of a node = 2 or –2, adjust the tree by rotation around deepest such node.

  11. AVL Tree Insertion (1) P is the Pivot: bf is 2 or -2 P L R A B C D • Let the node that needs rebalancing be P. • P is called the pivot node • P is the first node that has a bf of 2 or -2 as we backup towards the root after an insertion

  12. AVL Tree Insertion (2) P is the Pivot: bf is 2 or -2 P L R A B C D • There are 4 cases: • Outside Cases (require single rotation) : • Insertion into left subtreeof left child of P (LL Imbalance). • Insertion into right subtreeof right child of P (RR Imbalan.) • Inside Cases (require double rotation) : • Insertion into left subtreeof right child of P (RL Imbalance) • Insertion into right subtreeof left child of P (LR Imbalance)

  13. LL Imbalance & Correction Rotate L 2 P P 0 or 1 L R R A A B B C C D D Tree after insertion Tree after LL correction • LL Imbalance: We have inserted into the leftsubtree of the left child of P (into subtreeA) • bf of P is 2 • bf of L is 0 or 1 • Correction: Single rotation to the right around P

  14. LL Imbalance Correction Example (1) Insert(2) Backup Rotate 1 1 1 1 10 10 10 10 2 4 4 1 4 1 20 20 20 0 0 0 0 20 0 0 3 3 3 3 0 0 2 1 4 0 0 Initial AVLTree AVL Tree after LL correction 2 2 0 0 Identified 4 as the pivot Tree right after insertion of 2 Classify the type of imbalance Now, backup the tree updating balance factors • LL Imbalance: • bf of P(4) is 2 • bf of L(3) is 0 or 1

  15. LL Imbalance Correction Example (2) Backup Insert(2) Rotate 1 2 10 1 10 10 0 4 4 0 0 1 4 4 20 0 20 20 0 0 3 10 0 1 5 3 0 3 3 0 5 5 1 0 0 0 5 20 0 0 2 0 Initial AVLTree 2 2 0 0 AVL Tree after LL Correction Identified 10 as the pivot Tree right after insertion of 2 Classify the type of imbalance Now, backup the tree updating balance factors • LL Imbalance: • bf of P(10) is 2 • bf of L(4) is 0 or 1

  16. RR Imbalance & Correction Rotate R -2 P P L 0 or -1 R L A A B B C C D D Tree after insertion Tree after RR correction • RR Imbalance: We have inserted into the rightsubtree of the right child of P (into subtreeD) • bf of P is -2 • bf of R is 0 or -1 • Correction: Single rotation to the left around P

  17. RR Imbalance Correction Example (1) -1 Rotate 10 Insert(40) 10 10 -1 -1 Backup 10 -1 -1 -2 0 30 4 0 4 4 20 20 0 0 4 -1 20 0 0 40 20 -1 0 30 30 0 30 0 Initial AVLTree AVL Tree after RR Correction 40 40 0 0 Tree right after insertion of 40 Identified 20 as the pivot Classify the type of imbalance Now, backup the tree updating balance factors • RR Imbalance: • bf of P(20) is -2 • bf of R(30) is 0 or -1

  18. RR Imbalance Correction Example (2) -2 Rotate 10 20 10 -1 0 Insert(40) & Backup 4 -1 20 0 4 0 0 -1 20 0 10 30 -1 30 15 0 30 4 15 15 0 0 0 0 40 0 Initial AVLTree 40 0 AVL Tree after RR Correction Tree after insertion of 40 Classify the type of imbalance As we backed up the tree, we updated balance factors and identified 10 as the pivot • RR Imbalance: • bf of P(10) is -2 • bf of R(20) is 0 or -1

  19. LR Imbalance & Correction Rotate(1) 2 Rotate(2) P P LR 2 R 2 -1 L R LR P L L 1 LR R C C C D D D A A A Tree after insertion Tree after rotate(1) B1 B2 B2 B1 B1 B2 Tree after LR correction • LR Imbalance: We have inserted into the rightsubtree of the left child of P (into subtreeLR) • bf of P is 2 • bf of L is -1 • Correction: Double rotation around L & P

  20. LR Imbalance Correction Example 2 Rotate 1 Insert(5) & Backup 0 7 10 10 2 4 4 0 10 0 -1 20 4 -1 0 20 0 1 3 5 0 3 3 0 20 7 0 0 0 0 7 1 AVL Tree after LR Correction Initial AVLTree 5 0 Tree after insertion of 5 As we backed up the tree, we updated balance factors and identified 10 as the pivot Classify the type of imbalance • LR Imbalance: • bf of P(10) is 2 • bf of L(4) is -1

  21. RL Imbalance & Correction RL -2 P Rotate(1) -2 Rotate(2) P P R 2 1 R 2 L RL L L 1 R RL D D D B A A B B A Tree after insertion Tree after RL correction Tree after Rotate(1) C1 C2 C1 C2 C1 C2 • RL Imbalance: We have inserted into the left subtree of the right child of P (into subtreeRL) • bf of P is -2 • bf of R is 1 • Correction: Double rotation around L & P

  22. RL Imbalance Correction Example -2 Insert(17) Rotate 15 0 10 -1 10 2 4 0 1 20 1 1 10 20 4 0 20 0 15 30 0 4 -1 30 0 0 17 15 30 0 0 0 Initial AVLTree 17 0 AVL Tree after RL Correction Tree after insertion of 17 1 As we backed up the tree, we updated balance factors and identified 10 as the pivot Classify the type of imbalance • RL Imbalance: • bf of P(10) is -2 • bf of R(20) is 1

  23. Deletion • Deletion is similar to insertion • First do regular BST deletion keeping track of the nodes on the path to the deleted node • After the node is deleted, simply backup the tree and update balance factors • If an imbalance is detected, do the appropriate rotation to restore the AVL tree property • You may have to do more than one rotation as you backup the tree

  24. DeletionExample (1) Delete(20) Rotate Backup 1 1 2 10 10 10 -1 4 4 0 20 4 0 4 0 10 0 3 1 0 5 0 5 3 0 5 3 5 0 3 0 0 0 0 AVL Tree after LL Correction Tree after deletion of 20 Idenfitied 10 as the pivot Initial AVLTree Now, backup the tree updating balance factors Classify the type of imbalance • LL Imbalance: • bf of P(10) is 2 • bf of L(4) is 0 or 1

  25. DeletionExample (2) Delete(20) Rotate Backup 1 1 2 10 10 10 0 5 2 4 -1 20 4 -1 4 10 0 -1 4 0 0 1 AVL Tree after LL Correction 5 0 5 5 0 0 Tree after deletion of 20 Idenfitied 10 as the pivot Initial AVLTree Now, backup the tree updating balance factors Classify the type of imbalance • LR Imbalance: • bf of P(10) is 2 • bf of L(4) is -1

  26. DeletionExample (3) 1 15 1 10 Delete(10) -1 5 20 1 -1 20 5 1 -1 30 1 3 7 -1 -1 30 15 1 3 7 0 -1 2 1 4 0 40 8 0 0 2 1 4 0 40 8 0 0 1 0 Tree after deletion of 10 We have copied the successor of 10 to root and deleted 15 1 0 Initial AVLTree Now, backup the tree updating balance factors

  27. DeletionExample (3) - continued 1 1 15 15 Rotate Backup -2 5 30 1 0 5 20 1 20 0 40 1 7 3 -1 0 -1 30 1 3 7 -1 2 1 4 0 2 8 1 0 4 0 40 8 0 0 1 0 1 0 Tree after RR imbalance is corrected Identified 20 as the pivot Classify the type of imbalance Is this an AVL tree? • RR Imbalance: • bf of P(20) is -2 • bf of R(30) is 0 or -1 Continue backing up the tree updating balance factors

  28. DeletionExample (3) - continued 2 15 5 0 Rotate Backup 5 30 1 0 1 3 0 15 20 2 0 40 1 7 1 3 0 7 -1 4 0 -1 30 0 2 1 1 0 4 0 8 20 0 0 8 40 0 0 1 0 Final Tree Identified 15 as the pivot Classify the type of imbalance • LL Imbalance: • bf of P(15) is 2 • bf of L(5) is 0 or 1

  29. Search (Find) • Since AVL Tree is a BST, search algorithm is the same as BST search and runs in guaranteed O(logn) time

  30. Summary of AVL Trees • Arguments for using AVL trees: • Search/insertion/deletion is O(log N)since AVL trees are always balanced. • The height balancing adds no more than a constant factorto the speed of insertion/deletion. • Arguments against using AVL trees: • Requires extra space for balancing factor (height) • It may be OK to have a partially balanced tree that would give performance similar to AVL trees without requiring the balancing factor • Splay trees

More Related