Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1.

Similar presentations


Presentation on theme: "CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1."— Presentation transcript:

1 CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1

2 Today’s Outline Addressing one of our problems Single and Double Rotations AVL Tree Implementation 2

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 subtree is much taller than the other! 3

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 lg n 4

5 AVL Tree Dictionary Data Structure 4 121062 115 8 141379 Binary search tree properties –binary tree invariant –search tree invariant Balance invariant –balance of every node is: -1  b  1 –result: depth is  (log n) 15 5 Note that (statically… ignoring how it updates) an AVL tree is a BST.

6 Testing the Balance Property 2092 155 10 30177 NULL s have height -1 6 FIRST calculate heights THEN calculate balances

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

8 Today’s Outline Addressing one of our problems Single and Double Rotations AVL Tree Implementation 8

9 But, How Do We Stay Balanced? 9 Need: three volunteers proud of their very diverse height.

10 Beautiful Balance (SIMPLEST version) Insert(middle) Insert(small) Insert(tall) 00 1 10 But… BSTs are under-constrained in unfortunate ways; ours may not look like this.

11 Bad Case #1 (SIMPLEST version) Insert(small) Insert(middle) Insert(tall) 0 1 2 11 How do we fix the bad case? How do we transition among different possible trees?

12 So you say you want a revolution? Scenario: k is one of the 1%; we want a revolution to depose k and give m root privileges. What do we do? Well, first… 12 k Z m Y X

13 Well.. what do we know? Scenario: k is one of the 1%; we want a revolution to depose k and give m root privileges. What’s everything we know about nodes k and m and subtrees X, Y, and Z? 13 k Z m Y X

14 A real solution? Scenario: k is one of the 1%; we want a revolution to depose k and give m root privileges. Now, how can we make m the root? 14 k Z m Y X

15 Single Rotation (SIMPLEST version) 0 1 2 00 1 15

16 General Single Rotation After rotation, subtree’s height same as before insert! Height of all ancestors unchanged. a X Y b Z a XY b Z h h - 1 h + 1 h - 1 h + 2 h h - 1 h h + 1  An insert made this BAD! 16 Why couldn’t the bad insert be in X? Why does it matter that ancestors stay the same?

17 Bad Case #2 (SIMPLEST version) Insert(small) Insert(tall) Insert(middle) 0 1 2 17

18 Double Rotation (SIMPLEST version) 00 1 0 1 2 0 1 2 18

19 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? 19

20 Today’s Outline Addressing one of our problems Single and Double Rotations AVL Tree Implementation 20

21 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 Mirrored cases also possible 21

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

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

24 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 24

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

26 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 26

27 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 27

28 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 28

29 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 29

30 Single Rotation Code void RotateLeft(Node *& root) { Node * temp = root->right; root->right = temp->left; temp->left = root; root->height = max(height(root->right), height(root->left)) + 1; temp->height = max(height(temp->right), height(temp->left) + 1; root = temp; } X Y Z root temp (The “height” function returns -1 for a NULL subtree or the “height” field of a non-NULL subtree.) 30

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

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

33 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 33

34 To Do Posted readings on priority queues and sorts, but de-emphasizing heaps 34

35 Coming Up Quick Jump Back to Priority Queue ADT –A very familiar data structure gives us O(lg n) performance! On to sorts Then forward again! 35


Download ppt "CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1."

Similar presentations


Ads by Google