Presentation is loading. Please wait.

Presentation is loading. Please wait.

6 3 8 4 v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,

Similar presentations


Presentation on theme: "6 3 8 4 v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,"โ€” Presentation transcript:

1 6 3 8 4 v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004) and slides from Nancy M. Amato And Jory Denny 1 1

2 AVL Tree Definition AVL trees are balanced An AVL Tree is a binary search tree such that for every internal node ๐‘ฃ of ๐‘‡, the heights of the children of ๐‘ฃ can differ by at most 1 48 62 50 88 78 44 32 17 4 3 2 1 An example of an AVL tree where the heights are shown next to the nodes:

3 Insertion in an AVL Tree
Insertion is as in a binary search tree Always done by expanding an external node. Example insert 54: After Insertion 44 17 78 32 50 88 48 62 54 Before Insertion 44 17 78 32 50 88 48 62

4 Trinode Restructuring
let (๐‘Ž, ๐‘, ๐‘) be an inorder listing of ๐‘ฅ, ๐‘ฆ, ๐‘ง perform the rotations needed to make ๐‘ the topmost node of the three Trinode Restructuring b=y a=z c=x T0 T1 T2 T3 case 2: double rotation (a right rotation about c, then a left rotation about a) (other two cases are symmetrical) c=y b=x a=z T0 T1 T2 T3 b=y a=z c=x T0 T1 T2 T3 b=x c=y a=z T0 T1 T2 T3 case 1: single rotation (a left rotation about a)

5 Insertion Example, continued
88 44 17 78 32 50 48 62 2 4 1 3 54 T x y z ...balanced unbalanced...

6 Restructuring Single Rotations

7 Restructuring Double Rotations

8 Restructuring Algorithm restructure(๐‘ฅ)
Let (a,b,c) be a left-to-right (inorder) listing of the nodes x, y, and z. Let (T0,T1,T2,T3) be a left-to-right (inorder) listing of the four subtrees of x, y, and z not rooted at x, y, or z. Replace the subtree rooted at z with a new subtree rooted at b. Let a be the left child of b and let T0 and T1 be the left and right subtrees of a, respectively. Let c be the right child of b and let T2 and T3 be the left and right subtrees of c, respectively.

9 Exercise AVL Trees Insert into an initially empty AVL tree items with the following keys (in this order). Draw the resulting AVL tree 10, 20, 13, 30, 24, 7

10 Removal in an AVL Tree remove 32
Removal begins as in a binary search tree, which means the node removed will become an empty external node. Its parent, ๐‘ค, may cause an imbalance. Example: 44 17 78 32 50 88 48 62 54 remove 32 44 17 78 50 88 48 62 54 before deletion of 32 after deletion

11 Rebalancing after a Removal
Let ๐‘ง be the first unbalanced node encountered while travelling up the tree from ๐‘ค (parent of removed node) . Also, let ๐‘ฆ be the child of ๐‘ง with the larger height, and let ๐‘ฅ be the child of ๐‘ฆ with the larger height. We perform restructure(๐‘ฅ) to restore balance at ๐‘ง. 44 17 78 50 88 48 62 54 44 17 78 50 88 48 62 54 w c=x b=y a=z

12 Rebalancing after a Removal
As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of ๐‘‡ is reached This can happen at most ๐‘‚( log ๐‘› ) times. Why? 44 17 78 50 88 48 62 54 44 17 78 50 88 48 62 54 w c=x b=y a=z

13 Exercise AVL Trees Insert into an initially empty AVL tree items with the following keys (in this order). Draw the resulting AVL tree 10, 20, 13, 30, 24, 7 Now, remove the item with key 24. Draw the resulting tree Now, remove the item with key 20. Draw the resulting tree

14 Height of an AVL Tree 3 4 n(1) n(2) Fact: The height of an AVL tree storing ๐‘› keys is ๐‘‚ log ๐‘› . Proof: Let us bound ๐‘› โ„Ž : the minimum number of internal nodes of an AVL tree of height โ„Ž. We easily see that ๐‘› 1 =1 and ๐‘› 2 =2 For ๐‘›>2, an AVL tree of height โ„Ž contains the root node, one AVL subtree of height โ„Žโˆ’1 and another of height โ„Žโˆ’2. That is, ๐‘› โ„Ž =1+๐‘› โ„Žโˆ’1 +๐‘› โ„Žโˆ’2 Knowing ๐‘› โ„Žโˆ’1 >๐‘› โ„Žโˆ’2 , we get ๐‘› โ„Ž >2๐‘› โ„Žโˆ’2 . So ๐‘› โ„Ž >2๐‘› โ„Žโˆ’2 >4๐‘› โ„Žโˆ’4 >8๐‘› ๐‘›โˆ’6 ,โ€ฆ (by induction), ๐‘› โ„Ž > 2 ๐‘– ๐‘› โ„Žโˆ’2๐‘– Solving the base case we get: ๐‘› โ„Ž > 2 โ„Ž 2 โˆ’1 Taking logarithms: โ„Ž<2 log ๐‘›(โ„Ž) +2 Thus the height of an AVL tree is ๐‘‚( log ๐‘› )

15 Running Times for AVL Trees
A single restructure is ๐‘‚(1) โ€“ using a linked-structure binary tree find ๐‘˜ takes ๐‘‚ log ๐‘› time โ€“ height of tree is ๐‘‚ log ๐‘› , no restructures needed put ๐‘˜, ๐‘ฃ takes ๐‘‚ log ๐‘› time Initial find is ๐‘‚ log ๐‘› Restructuring up the tree, maintaining heights is ๐‘‚ log ๐‘› erase ๐‘˜ takes ๐‘‚ log ๐‘› time

16 Other Types of Self-balancing Trees
Splay Trees โ€“ A binary search tree which uses an operation splay ๐‘ฅ to allow for amortized complexity of ๐‘‚ log ๐‘› 2, 4 Trees โ€“ A multiway search tree where every node stores internally a list of entries and has 2, 3, or 4 children. Defines self- balancing operations Red-Black Trees โ€“ A binary search tree which colors each internal node red or black. Self-balancing dictates changes of colors and required rotation operations 9 10 14 9 15 4 6 2 12 7 21


Download ppt "6 3 8 4 v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,"

Similar presentations


Ads by Google