Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000.

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000."— Presentation transcript:

1 CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000

2 Today’s Outline Double-tailed distributions and the mean AVL trees What AVL Stands For Bonus! (Deletion)

3 Beauty is Only  (log n) Deep Binary Search Trees are fast if they’re shallow: –perfectly complete –perfectly complete except the one level fringe (like a heap) –anything else? What matters here? Problems occur when one branch is much longer than the other!

4 Balance –height(left subtree) - height(right subtree) –zero everywhere  perfectly balanced –small everywhere  balanced enough t 5 7 Balance between -1 and 1 everywhere  maximum height of 1.44 log n

5 AVL Tree Dictionary Data Structure 4 121062 115 8 141379 Binary search tree properties –binary tree property –search tree property Balance property –balance of every node is: -1  b  1 –result: depth is  (log n) 15

6 Testing the Balance Property 2092 155 10 30177 NULL s have height -1

7 An AVL Tree 20 92 15 5 10 30 177 0 0 0 011 22 3 10 3 data height children

8 But, How Do We Stay Balanced? I need: –the smallest person in the class –the tallest person in the class –the averagest (?) person in the class

9 Beautiful Balance Insert(middle) Insert(small) Insert(tall) 00 1

10 Bad Case #1 Insert(small) Insert(middle) Insert(tall) 0 1 2

11 Single Rotation 0 1 2 00 1

12 General Single Rotation Height of subtree same as it was before insert! Height of all ancestors unchanged. So? a X Y b Z a XY b Z h h - 1 h + 1 h - 1 h + 2 h h - 1 h h + 1

13 Bad Case #2 Insert(small) Insert(tall) Insert(middle) 0 1 2

14 Double Rotation 00 1 0 1 2 0 1 2

15 General Double Rotation Height of subtree still the same as it was before insert! Height of all ancestors unchanged. a Z b W c XY a Z b W c XY h h - 1? h - 1 h + 2 h + 1 h - 1 h h + 1 h h - 1?

16 Insert Algorithm Find spot for value Hang new node Search back up for imbalance If there is an imbalance: case #1: Perform single rotation and exit case #2: Perform double rotation and exit

17 Easy Insert 2092 155 10 3017 Insert(3) 12 0 0 100 12 3 0

18 Hard Insert (Bad Case #1) 2092 155 10 3017 Insert(33) 3 12 1 0 100 22 3 0 0

19 Single Rotation 2092 155 10 30173 12 33 1 0 200 23 3 1 0 0 3092 205 10 333 15 1 0 110 22 3 0 0 1712 0

20 Hard Insert (Bad Case #2) Insert(18) 2092 155 10 30173 12 1 0 100 22 3 0 0

21 Single Rotation (oops!) 2092 155 10 30173 12 1 1 200 23 3 0 0 3092 205 10 3 15 1 1 020 23 3 0 1712 0 18 0 0

22 Double Rotation (Step #1) 2092 155 10 30173 12 1 1 200 23 3 0 0 18 0 1792 155 10 203 12 1 200 23 3 1 0 30 0 Look familiar? 18 0

23 Double Rotation (Step #2) 1792 155 10 203 12 1 200 23 3 1 0 30 0 18 0 2092 175 10 303 15 1 0 110 22 3 0 0 12 0 18

24 AVL Algorithm Revisited Recursive 1. Search downward for spot 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, double rotate Iterative 1. Search downward for spot, stacking parent nodes 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate and exit b. If imbalance #2, double rotate and exit

25 Single Rotation Code void RotateRight(Node *& root) { Node * temp = root->right; root->right = temp->left; temp->left = root; root->height = max(root->right->height, root->left->height) + 1; temp->height = max(temp->right->height, temp->left->height) + 1; root = temp; } X Y Z root temp

26 Double Rotation Code void DoubleRotateRight(Node *& root) { RotateLeft(root->right); RotateRight(root); } a Z b W c XY a Z c b X Y W First Rotation

27 Double Rotation Completed a Z c b X Y W a Z c b X Y W First Rotation Second Rotation

28 AVL Automatically Virtually Leveled Architecture for inVisible Leveling (the “in” is inVisible) All Very Low Articulating Various Lines Amortizing? Very Lousy! Absolut Vodka Logarithms Amazingly Vexing Letters Adelson-Velskii Landis

29 Bonus: Deletion (Easy Case) 2092 155 10 30173 12 1 0 100 22 3 0 0 Delete(15)

30 Deletion (Hard Case #1) 2092 175 10 303 12 1 100 22 3 0 0 Delete(12)

31 Single Rotation on Deletion 2092 175 10 303 1 10 22 3 0 0 92 205 10 17 3 1 00 21 3 0 0 Something very bad happened!

32 To Do Finish Project II Read chapter 4 in the book Understand Zasha’s worksheet Prepare for the midterm

33 Coming Up A bit more AVL trees Splay trees Another worksheet and midterm study guide Second project due (January 31 st ) Midterm (February 4 th )


Download ppt "CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000."

Similar presentations


Ads by Google