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

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

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

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

CPSC 221: Algorithms and Data Structures

Lecture #6Balancing Act

Steve Wolfman

2014W1

1

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

Today’s Outline

• Addressing one of our problems• Single and Double Rotations• AVL Tree Implementation

2

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

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

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

Balance

• Balance– height(left subtree) - height(right subtree)– zero everywhere perfectly balanced– small everywhere balanced enough

t

57

Balance between -1 and 1 everywhere maximum height of ~1.44 lg n 4

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

AVL Tree Dictionary Data Structure

4

121062

115

8

14137 9

• 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

5Note that (statically… ignoring how it updates) an AVL tree is a BST.

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

Testing the Balance Property

2092

155

10

30177

NULLs have height -1

6FIRST calculate heightsTHEN calculate balances

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

An AVL Tree

20

92 15

5

10

30

177

0

0 0

011

2 2

3 10

3

data

height

children

7

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

Today’s Outline

• Addressing one of our problems• Single and Double Rotations• AVL Tree Implementation

8

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

But, How Do We Stay Balanced?

9

Need: three volunteers proud of their very diverse height.

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

Beautiful Balance (SIMPLEST version)

Insert(middle)

Insert(small)

Insert(tall)

00

1

10But… BSTs are under-constrained in unfortunate ways;

ours may not look like this.

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

Bad Case #1(SIMPLEST version)

Insert(small)

Insert(middle)

Insert(tall)

0

1

2

11How do we fix the bad case?

How do we transition among different possible trees?

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

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

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

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 weknow about nodes k and m and subtrees X,Y, and Z?

13

k

Z

m

Y

X

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

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 makem the root?

14

k

Z

m

Y

X

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

Single Rotation(SIMPLEST version)

0

1

2

00

1

15

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

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

Zh h - 1

h + 1 h - 1

h + 2

h

h - 1

h

h - 1

h + 1

An insert made this BAD!

16Why couldn’t the bad insert be in X?Why does it matter that ancestors stay the same?

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

Bad Case #2(SIMPLEST version)

Insert(small)

Insert(tall)

Insert(middle)

0

1

2

17

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

Double Rotation(SIMPLEST version)

00

1

0

1

2

0

1

2

18

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

General Double Rotation

• Height of subtree still the same as it was before insert!• Height of all ancestors unchanged.

a

Z

b

W

c

X Y

a

Z

b

W

c

X Y

h

h - 1?

h - 1

h - 1

h + 2

h + 1

h - 1h - 1

h

h + 1

h

h - 1?

19

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

Today’s Outline

• Addressing one of our problems• Single and Double Rotations• AVL Tree Implementation

20

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

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

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

Easy Insert

2092

155

10

3017

Insert(3)

120

0

100

1 2

3

0

22

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

Hard Insert (Bad Case #1)

2092

155

10

3017

Insert(33)

3

121

0

100

2 2

3

00

23

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

Single Rotation

2092

155

10

30173

12

33

1

0

200

2 3

3

10

0

3092

205

10

333

151

0

110

2 2

3

001712

0

24

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

Hard Insert (Bad Case #2)

Insert(18)

2092

155

10

30173

121

0

100

2 2

3

00

25

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

Single Rotation (oops!)

2092

155

10

30173

121

1

200

2 3

3

00

3092

205

10

3

151

1

020

2 3

3

01712

0

180

180

26

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

Double Rotation (Step #1)

2092

155

10

30173

121

1

200

2 3

3

00

180

1792

155

10

203

121 200

2 3

3

10

300

Look familiar?18

0

27

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

Double Rotation (Step #2)

1792

155

10

203

121 200

2 3

3

10

300

180

2092

175

10

303

151

0

110

2 2

3

0012

018

28

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

AVL Algorithm Revisited• Recursive1. Search downward for spot2. Insert node3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, double rotate

• Iterative1. Search downward for spot, stacking parent nodes2. Insert node3. Unwind stack, correcting heights a. If imbalance #1, single rotate and exit b. If imbalance #2, double rotate and exit 29

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

Single Rotation Codevoid 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 subtreeor the “height” field of a non-NULL subtree.)

30

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

Double Rotation Codevoid DoubleRotateLeft(Node *& root) { RotateRight(root->right); RotateLeft(root);}

a

Z

b

W

c

XY

a

Z

c

b

X

Y

W

First Rotation

31

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

Double Rotation Completed

a

Z

c

b

X

Y

W

a

Z

c

b

XY

W

First Rotation Second Rotation

32

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

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 Landis33

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

To Do

• Posted readings on priority queues and sorts, but de-emphasizing heaps

34

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

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