29
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Embed Size (px)

Citation preview

Page 1: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

CSE 326: Data StructuresLecture #11

AVL and Splay Trees

Steve Wolfman

Winter Quarter 2000

Page 2: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Today’s Outline

• Extra operations in AVL trees • Splaying and Splay Trees

Page 3: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

AVL Tree Dictionary Data Structure

4

121062

115

8

14137 9

• 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

Page 4: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Deletion (Hard Case #1)

2092

175

10

303

121 100

2 2

3

00

Delete(12)

Page 5: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Single Rotation on Deletion

2092

175

10

303

1 10

2 2

3

00

3092

205

10

17

3

1 00

2 1

3

0

0

Something very bad happened!

Page 6: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Deletion (Hard Case #2-4)

Delete(9)

2092

175

10

303

121 220

2 3

4

0

33

15

130 0

1

0

20

30

12

33

15

13

1

0 0

110

180

Page 7: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Double Rotation on Deletion

2

3

0

202

173

10

30

121 22

2 3

4

33

15

13

1

0 0

111

0180

2052

173

10

30

120 220

1 3

4

33

15

13

1

0 0

111

01800

Page 8: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Deletion with Propagation: Choose Your Own Adventure!

We get to choose whether to single or double rotate!

If you take the gold and single rotate, flip to Slide 10.

If you decide to search further into the scary cavern, continue on to the next slide.

If you decide not to rotate atall, jump to Slide 11.

2052

173

10

30

120 220

1 3

4

33

15

13

1

0 0

111

0180

Page 9: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Propagated Double Rotation

0

17

12

11

52

3

10

4

2 3

1 0

0 0

2052

173

10

30

120 220

1 3

4

33

15

13

1

0

111

0180

151

0

20

30

33

1180

130

2

Page 10: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Propagated Single Rotation

0

30

20

17

33

12

15

13

1

0

52

3

10

4

3 2

1 2 1

0 0 011

0

2052

173

10

30

120 220

1 3

4

33

15

13

1

0

111

0180

180

Page 11: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

You Didn’t Rotate

Casually walking away from the AVL tree, minding your own business, you fall into a sewer and are eaten by wild Red-Black trees (some people say they grow as big as B-Trees in the sewers!).

You die.

Page 12: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

AVL Deletion Algorithm• Recursive1. Search downward for

node

2. Delete node

3. Unwind stack,

correcting heights

a. If imbalance #1,

single rotate

b. If imbalance #2,

double rotate

• Iterative1. Search downward for

node, stacking

parent nodes

2. Delete node

3. Unwind stack,

correcting heights

a. If imbalance #1,

single rotate

b. If imbalance #2,

double rotate

Page 13: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

AVL buildTree8 10 15 20 30 35 405

17

17

8 10155 20303540

Divide & Conquer– Divide the problem into parts

– Solve each part recursively

– Merge the parts into a general solution

How long does divide & conquer take?

Page 14: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

BuildTree Example

35

17

15

5

8

10

3

2 2

1 0

0

301

40

200

0

8 10 15 20 30 35 405 17

8 10 155

85

30 35 4020

3020

Page 15: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

BuildTree Analysis(Approximate)

T(1) = 1

T(n) = 2T(n/2) + 1

T(n) = 2(2T(n/4)+1) + 1

T(n) = 4T(n/4) + 2 + 1

T(n) = 4(2T(n/8)+1) + 2 + 1

T(n) = 8T(n/8) + 4 + 2 + 1

T(n) = 2kT(n/2k) +

let 2k = n, log n = k

T(n) = nT(1) +

T(n) = (n)

k

i

i

1

22

1

n

i

ilog

1

22

1

Page 16: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

2

1n

2

1n

2

1n

2

1n

BuildTree Analysis (Exact)

Precise Analysis: T(0) = bT(n) = T( ) + T( ) + c

By induction on n:

T(n) = (b+c)n + b

Base case:

T(0) = b = (b+c)0 + b

Induction step:

T(n) = (b+c) + b +

(b+c) + b + c

= (b+c)n + b

QED: T(n) = (b+c)n + b = (n)

12

1

2

1

nnn

Page 17: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Thinking About AVL

• Observations+ Worst case height of an AVL tree is about 1.44 log n

+ All operations supported in worst case O(log n)

+ Only one (single or double) rotation needed on insertion

– O(log n) rotations needed on deletion

– Height fields must be maintained (or 2-bit balance)

? Coding complexity

Page 18: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Splay Trees

• Problems with AVL Trees– extra storage/complexity for height fields

– ugly delete code

• Solution: splay trees– blind adjusting version of AVL trees

– amortized time for all operations is O(log n)

– worst case time is O(n)

– insert/find always rotates node to the root!

Page 19: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Idea

17

10

92

5

3

You’re forced to make a really deep access:

Since you’re down there anyway,fix up a lot of deep nodes!

Page 20: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Zig-Zig*

*I told you it was a technical term!

n

Z

Y

p

X

g

W

g

W

X

p

Y

n

Z

Page 21: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Zig-Zag*

g

Xp

Y

n

Z

W

*This is just a double rotation

n

Y

g

W

p

ZX

Page 22: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Zig

p

X

n

Y

Z

n

Z

p

Y

X

root root

Page 23: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Splaying Example

2

1

3

4

5

6

Find(6)

2

1

3

6

5

4

zig-zig

Page 24: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Still Splaying 6

zig-zig2

1

3

6

5

4

1

6

3

2 5

4

Page 25: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Almost There, Stay on Target*

zig

1

6

3

2 5

4

6

1

3

2 5

4

Page 26: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Splay Again

Find(4)

zig-zag

6

1

3

2 5

4

6

1

4

3 5

2

Page 27: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Example Splayed Out

zig-zag

6

1

4

3 5

2

61

4

3 5

2

Page 28: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

To Do

• Turn in Project II• Read chapter 4 in the book• Do the new worksheet• Prepare for the midterm• Finish HW#4

– Just kidding

Page 29: CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Coming Up

• Polish off Splay Trees• Talk about Project III• Second project due (today)• Midterm (Friday)