Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 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 3 AVL Trees: Formal Definition 1.All empty trees are AVL-trees 2.If T is a non-empty binary search tree with T L and T R as its left and right sub-trees, then T is an AVL tree iff 1.T L and T R are AVL trees 2.|h L – h R | <= 1, where h L and h R are the heights of T L and T R respectively hLhL h L +1 or h L -1 TLTL TRTR

4 4 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 6 4 1 7 9 0 0 0 1 Red numbers are Balance Factors An AVL Tree

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

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

7 7 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 6 5 4 7 9 0 0 0 1 Red numbers are Balance Factors An AVL Tree Height = O(logN)

8 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! 6 5 4 7 9 0 0 0 1 6 5 4 7 9 0 1 1 2 3 Insert 3 0 An AVL Tree No longer an AVL Tree

9 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 1.Back up towards root updating balance factors along the access path 2.If Balance Factor of a node = 2 or –2, adjust the tree by rotation around deepest such node. 6 5 4 7 9 0 1 1 2 3 0 Non- AVL Tree

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

11 11 AVL Tree Insertion (1) 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 P is the Pivot: bf is 2 or -2

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

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

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

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

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

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

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

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

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

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

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

23 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 24 Deletion Example (1) 10 4 20 3 1 0 0 Initial AVLTree Delete(20) Backup 5 0 0 LL Imbalance: –bf of P(10) is 2 –bf of L(4) is 0 or 1 Tree after deletion of 20 10 4 3 1 0 5 0 0 Idenfitied 10 as the pivot 10 4 3 2 0 5 0 0 Classify the type of imbalance Now, backup the tree updating balance factors Rotate 4 10 1 5 3 0 0 AVL Tree after LL Correction

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

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

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

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

29 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 30 Summary of AVL Trees Arguments for using AVL trees: 1.Search/insertion/deletion is O(log N) since AVL trees are always balanced. 2.The height balancing adds no more than a constant factor to the speed of insertion/deletion. Arguments against using AVL trees: 1.Requires extra space for balancing factor (height) 2.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


Download ppt "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."

Similar presentations


Ads by Google