51
CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

  • View
    219

  • Download
    1

Embed Size (px)

Citation preview

Page 1: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

CSE 326: Data StructuresAVL Trees

Ben Lerner

Summer 2007

Page 2: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

2

The AVL Balance ConditionLeft and right subtrees of every nodehave equal heights differing by at most 1

Define: balance(x) = height(x.left) – height(x.right)

AVL property: –1 balance(x) 1, for every node x

• Ensures small depth– Will prove this by showing that an AVL tree of height

h must have a lot of (i.e. O(2h)) nodes• Easy to maintain

– Using single and double rotations

Page 3: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

3

The AVL Tree Data Structure

4

121062

115

8

14137 9

Structural properties

1. Binary tree property

2. Balance property:balance of every node isbetween -1 and 1

Result:

Worst case depth isO(log n)

Ordering property– Same as for BST

15

Page 4: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

4

Is this an AVL tree?

2092

155

10

30177

(NULLs have height -1)

We need to be able to:

1. Track the balance

2. Detect imbalance

3. Restore balance

Page 5: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

5

111

84

6

3

1171

84

6

2

5

How about these?

10 12

7

Page 6: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

6

Height of an AVL Tree

• M(h) = minimum number of nodes in an AVL tree of height h.

• Basis– M(0) = 1, M(1) = 2

• Induction– M(h) = M(h-1) + M(h-2) + 1

• Solution– M(h) > h - 1 ( = (1+5)/2 1.62)

h-1h-2

h

Page 7: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

7

Proof that M(h) > h

• Basis: M(0) = 1 > 0 -1, M(1) = 2 > 1-1

• Induction step.M(h) = M(h-1) + M(h-2) + 1 > (h-1 - 1) + (h-2 - 1) + 1 = h-1 + h-2 - 1 = h-2 ( +1) - 1 = h - 1 (Uses fact that 2 = +1)

Page 8: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

8

Height of an AVL Tree

• M(h) > h ( 1.62)

• Suppose we have n nodes in an AVL tree of height h.– N > M(h) – N > h - 1

– log(N+1) > h (relatively well balanced tree!!)

Page 9: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

9

Node Heights

1

00

2

0

6

4 9

81 5

1

height of node = hbalance factor = hleft-hright

empty height = -1

0

0

2

0

6

4 9

1 5

1

Page 10: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

10

Node Heights after Insert 7

2

10

3

0

6

4 9

81 5

1

height of node = hbalance factor = hleft-hright

empty height = -1

1

0

2

0

6

4 9

1 5

1

0

7

0

7

balance factor 1-(-1) = 2

-1

Page 11: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

11

Insert and Rotation in AVL Trees

• Insert operation may cause balance factor to become 2 or –2 for some node – only nodes on the path from insertion point to

root node have possibly changed in height– So after the Insert, go back up to the root

node by node, updating heights– If a new balance factor (i.e. the difference

hleft-hright) is 2 or –2, adjust tree by rotation around the node

Page 12: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

12

Single Rotation in an AVL Tree

2

10

2

0

6

4 9

81 5

1

0

7

0

1

0

2

0

6

4

9

8

1 5

1

0

7

Page 13: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

13

Let the node that needs rebalancing be .

There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of .

The rebalancing is performed through four separate rotation algorithms.

Insertions in AVL Trees

Page 14: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

14

Bad Case #1

Insert(6)

Insert(3)

Insert(1)

Where is AVL property violated?

Page 15: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

15

Fix: Apply Single Rotation

3

1 600

16

3

10

1

2

Single Rotation: 1. Rotate between x and child

AVL Property violated at this node (x)

Page 16: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

16

j

k

X Y

Z

Consider a validAVL subtree

AVL Insertion: Outside Case

h

hh

Page 17: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

17

j

k

XY

Z

Inserting into Xdestroys the AVL property at node j

AVL Insertion: Outside Case

h

h+1 h

Page 18: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

18

j

k

XY

Z

Do a “rotation from left”

AVL Insertion: Outside Case

h

h+1 h

Page 19: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

19

j

k

XY

Z

Single rotation from left

h

h+1 h

Page 20: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

20

j

k

X Y Z

“rotation from left” done!(“rotation from right” is mirror symmetric)

Outside Case Completed

AVL property has been restored!

h

h+1

h

Page 21: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

21

Single rotation example

21103

205

15

1

2 4

17

21

10

3 20

5

15

1

2

4

17

Page 22: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

22

Bad Case #2

Insert(1)

Insert(6)

Insert(3)

Will single rotation fix? (No)(try bringing up Large; doesn’t work)

Page 23: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

23

Fix: Apply Double Rotation

3

1 600

1

3

6

1

0

1

2

6

3

1

0

1

2

Balanced?

Intuition: 3 must become root

AVL Property violated at this node (x)

Double Rotation1. Rotate between x’s child and grandchild2. Rotate between x and x’s new child

Page 24: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

24

j

k

X Y

Z

AVL Insertion: Inside Case

Consider a validAVL subtree

h

hh

Page 25: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

25

Inserting into Y destroys theAVL propertyat node j

j

k

XY

Z

AVL Insertion: Inside Case

Does “rotation from left”restore balance?

h

h+1h

Page 26: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

26

jk

X

YZ

“Rotation from left”does not restorebalance… now k isout of balance

AVL Insertion: Inside Case

hh+1

h

Page 27: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

27

Consider the structureof subtree Y… j

k

XY

Z

AVL Insertion: Inside Case

h

h+1h

Page 28: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

28

j

k

X

V

Z

W

i

Y = node i andsubtrees V and W

AVL Insertion: Inside Case

h

h+1h

h or h-1

Page 29: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

29

j

k

X

V

Z

W

i

AVL Insertion: Inside Case

We will do a“double rotation” . . .

1

2

Page 30: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

30

j

k

X V

ZW

i

Double rotation : first rotation

Page 31: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

31

j

k

X V

ZW

i

Double rotation : second rotation

Page 32: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

32

jk

X V ZW

i

Double rotation : second rotation

right rotation complete

Balance has been restored to the universe

hh h or h-1

Page 33: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

33

Double rotation, step 1

104

178

15

3 6

16

5

106

178

15

4

3

16

5

Page 34: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

34

Double rotation, step 2

106

178

15

4

3

16

5

10

6 17

8

15

4

3

16

5

Page 35: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

35

Imbalance at node X

Single Rotation

1. Rotate between x and child

Double Rotation

1. Rotate between x’s child and grandchild

2. Rotate between x and x’s new child

Page 36: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

36

Insert into an AVL tree: a b e c d

Page 37: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

37

9

5

2

11

7

1. single rotation?

2. double rotation?

3. no rotation?

Inserting what integer values would cause the tree to need a:

Single and Double Rotations:

13

30

Student Activity

Page 38: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

38

Insertion into AVL tree

1. Find spot for new key

2. Hang new node there with this key

3. Search back up the path for imbalance

4. If there is an imbalance:case #1: Perform single rotation and exit

case #2: Perform double rotation and exit

Both rotations keep the subtree height unchanged.Hence only one rotation is sufficient!

Page 39: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

39

Easy Insert

2092

155

10

3017

Insert(3)

120

0

100

1 2

3

0

Unbalanced?

Page 40: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

40

Hard Insert (Bad Case #1)

2092

155

10

3017

Insert(33)

3

121

0

100

2 2

3

00

How to fix?

Unbalanced?

Page 41: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

41

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

Page 42: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

42

Hard Insert (Bad Case #2)

Insert(18)

2092

155

10

30173

121

0

100

2 2

3

00

How to fix?

Unbalanced?

Page 43: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

43

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

Page 44: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

44

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

180

Page 45: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

45

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

Page 46: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

46

Implementation

balance (1,0,-1)

key

rightleft

Page 47: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

47

Single Rotation

RotateFromRight(n : reference node pointer) {p : node pointer;p := n.right;n.right := p.left;p.left := n;n := p

}

X

Y Z

n

RotateFromLeft is symmetric

Page 48: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

48

Double Rotation

• Class participation• Implement Double Rotation in two lines.

DoubleRotateFromRight(n : reference node pointer) {????}

X

n

V W

Z

Page 49: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

49

AVL Tree Deletion

• Similar to insertion– Rotations and double rotations needed to

rebalance– Imbalance may propagate upward so that

many rotations may be needed.

Page 50: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

50

Arguments for AVL trees:

1. Search is O(log N) since AVL trees are always well balanced.2. The height balancing adds no more than a constant factor to the

speed of insertion, deletion, and find.

Arguments against using AVL trees:1. Difficult to program & debug; more space for height info.2. Asymptotically faster but rebalancing costs time.3. Most large searches are done in database systems on disk and use

other structures (e.g. B-trees).4. May be OK to have O(N) for a single operation if total run time for

many consecutive operations is fast (e.g. Splay trees).

Pros and Cons of AVL Trees

Page 51: CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

51

Double Rotation Solution

DoubleRotateFromRight(n : reference node pointer) {RotateFromLeft(n.right);RotateFromRight(n);}

X

n

V W

Z