34
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

1

CSE 326: Data Structures Trees

Lecture 7: Wednesday, Jan 23, 2003

Page 2: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

2

Outline

• Finish discussion on random binary search trees (BST)

• AVL trees

• Reading assignment for this week:Weiss: 4.3, 4.4, 4.5, and 4.7

Page 3: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

3

The Average Depth of a BST

• Insert the elements 1 <2 < ... < n in some order, starting with the empty tree

• For each permutation, :– T = the BST after inserting (1), (2) , ... ,

(n)

• The Average Depth:))/n!height(T(H(n)

ππ

Page 4: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

4

The Average Depth of a BST

• The average depth of a BST is:

H(n) = (log n)

• For some , height(T) = O(log n)• For other , height(T) = O(n)• But the average is O(log n)

• Please read the proof in the book and/or slides !

Page 5: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

5

log versus n n

Why is average depth of BST's made from

random inputs different from the average

depth of all pos

log

Because there are more ways to build shallow

trees than d

sible

eep t

BST

ee

?

r

's

n n

s!

Page 6: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

6

Random Input vs. Random Trees

Inputs

1,2,3

3,2,1

1,3,2

3,1,2

2,1,3

2,3,1

TreesFor three items, the shallowest tree is twice as likely as any other – effect

grows as n increases. For n=4,

probability of getting a shallow

tree > 50%

Page 7: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

7

Average cost

• The average, amortized cost of n insert/find operations is O(log(n))

• But the average, amortized cost of n insert/find/delete operations can be as bad as (n)– Deletions make life harder (recall stretchy arrays)

• Need guaranteed cost O(log n) – next

Page 8: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

8

Beauty is Only (log n) Deep

• Binary Search Trees are fast if they’re shallowe.g.: complete

• Problems occur when one branch is much longer than the otherHow to capture the notion of a “sort of” complete

tree?

Page 9: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

9

Balance

balance = height(left subtree) - height(right subtree)• convention: height of a “null” subtree is -1• zero everywhere perfectly balanced• small everywhere balanced enough: (log n)

– Precisely: Maximum depth is 1.44 log n

t

56

Page 10: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

10

AVL Tree (Adelson-Velskii Landis)

4

121062

115

8

14137 9

• Binary search tree properties

• Balance of every node is -1 b 1

• Tree re-balances itself after every insert or delete

15What is the balance of each node in this tree?

Page 11: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

11

AVL Tree Data Structure

15

92 12

5

10

20

17

0

0

100

1 2

3 10

3

data

height

children

300

Page 12: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

12

Not An AVL Tree

15

92 12

5

10

20

17

0

1

200

1 3

4 10

4

data

height

children

300

180

Page 13: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

13

Bad Case #1

Insert(small)

Insert(middle)

Insert(tall)

T

M

S

0

1

2

Page 14: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

14

Single Rotation

T

M

S

0

1

2

M

S T00

1

Basic operation used in AVL trees:

A right child could legally have its parent as its left child.

Page 15: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

15

General Case: Insert Unbalancesa

X

Y

b

Z

h h - 1

h + 1 h - 1

h + 2a

X

Y

b

Zh-1 h - 1

h h - 1

h + 1

a

XY

b

Z

h

h - 1

h

h - 1

h + 1

Page 16: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

16

Properties of General Insert + Single Rotation

• Restores balance to a lowest point in tree where imbalance occurs

• After rotation, height of the subtree (in the example, h+1) is the same as it was before the insert that imbalanced it

• Thus, no further rotations are needed anywhere in the tree!

Page 17: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

17

Bad Case #2

Insert(small)

Insert(tall)

Insert(middle)

M

T

S

0

1

2

Why won’t a single rotation (bringing T up to the top) fix this?

Page 18: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

18

Double Rotation

M

S T00

1

M

T

S

0

1

2

T

M

S

0

1

2

Page 19: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

19

General Double Rotation

• Initially: insert into X unbalances tree (root height goes to h+3)• “Zig zag” to pull up c – restores root height to h+2, left subtree height

to h

a

Z

b

W

c

Y

a

Z

b

W

c

Y

h+1

h

h

h

h + 3

h + 2

hh

h+1

h + 2

h+1

h

XX

Page 20: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

20

Another Double Rotation Case

• Initially: insert into Y unbalances tree (root height goes to h+2)• “Zig zag” to pull up c – restores root height to h+1, left subtree height

to h

a

Z

b

W

c

Y

a

Z

b

W

c

Y

h+1

h

h

h

h + 3

h + 2

hh

h+1

h + 2

h+1

h

XX

Page 21: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

21

Insert Algorithm

• Find spot for value

• Hang new node

• Search back up looking for imbalance

• If there is an imbalance:“outside”: Perform single rotation and exit

“inside”: Perform double rotation and exit

Page 22: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

22

AVL Insert AlgorithmNode insert(Comparable x, Node root){if ( root == NULL ) return new Node(x);if (x == root.key) return root;

if (x < root.key){root.left = insert( x, root.left );if (root unbalanced) { rotate... } }

else { // x > root.keyroot.right = insert( x,

root.right ); if (root unbalanced) { rotate... } }

root.height = max(root.left.height, root.right.height)+1;

return root;}

Node insert(Comparable x, Node root){if ( root == NULL ) return new Node(x);if (x == root.key) return root;

if (x < root.key){root.left = insert( x, root.left );if (root unbalanced) { rotate... } }

else { // x > root.keyroot.right = insert( x,

root.right ); if (root unbalanced) { rotate... } }

root.height = max(root.left.height, root.right.height)+1;

return root;}

Page 23: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

23

Deletion (Really Easy Case)

2092

155

10

30173

121

0

100

2 2

3

00

Delete(17)

Page 24: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

24

Deletion (Pretty Easy Case)

2092

155

10

30173

121

0

100

2 2

3

00

Delete(15)

Page 25: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

25

Deletion (Pretty Easy Case cont.)

2092

175

10

303

121 100

2 2

3

00

Delete(15)

Page 26: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

26

Deletion (Hard Case #1)

2092

175

10

303

121 100

2 2

3

00

Delete(12)

Page 27: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

27

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

What is different about deletion than insertion?

Page 28: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

28

Deletion (Hard Case)

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 29: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

29

Double Rotation on Deletion

2

3

0

202

175

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

Not finished!

Page 30: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

30

Deletion with Propagation

We get to choose whether to single or double rotate!

2052

173

10

30

120 220

1 3

4

33

15

13

1

0 0

111

0180

What different about this case?

Page 31: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

31

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 32: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

32

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 33: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

33

AVL Deletion Algorithm• Recursive1. If at node, delete it

2. Otherwise recurse to find it in

3. Correct heights

a. If imbalance #1,

single rotate

b. If imbalance #2 (or don’t

care),

double rotate

• Iterative1. Search downward for node, stacking parent nodes2. Delete node3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2

(or don’t care) double rotate

Page 34: 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

34

Pro:

• All operations guaranteed O(log N) • The height balancing adds no more than a

constant factor to the speed of insertion

Con:

• Space consumed by height field in each node• Slower than ordinary BST on random data

Can we guarantee O(log N) performance with less overhead? Splay trees next time

Pros and Cons of AVL Trees