Download presentation
Presentation is loading. Please wait.
Published byฮฮตฯฯฯฮฑ ฮฯฮฟฯฯฮฟฮปฮฏฮดฮทฯ Modified over 6 years ago
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.