Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better!...

Post on 19-Jan-2016

217 views 0 download

Tags:

Transcript of Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better!...

Data Structures

AVL Trees

Balanced BSTObservation• BST: the shallower the better!• For a BST with n nodes– Average height is O(log n)– Worst case height is O(n)

• Simple cases such as insert(1, 2, 3, ..., n) lead to the worst case scenario: height O(n)

Solution: Require a Balance Condition that1. ensures depth is O(log n)

2. is easy to maintain

– strong enough!

The AVL Tree Data Structure

1. A binary search tree2. Heights of left and

right subtrees of every node differ by at most 1

115

8

Adelson-Velskii and Landis

An AVL tree is:

4

121062

13 147 9

15

Recursive Height CalculationARecall: height is max number of

edges from root to a leaf

hright

How do we compute height at A?

Note: height(null) = -1

hleftt

84

6

AVL Tree?

1 2

3

111

10 12

70

0 0

0 1

84

6

13

4

AVL Tree?

3

111

2

5

0

0 0 7 0

1

2

An AVL Tree has Height O(log n)

8

Proof Let S(h) be the min # of nodes in an AVL tree of height hClaim: S(h) = S(h-1) + S(h-2) + 1

AVL tree of height h=4with the min # of nodes (12)

4

12106

115

15

Solution of recurrence:S(h) = O(2h)(like Fibonacci numbers)

0

11 2

70

20

9 13 14 1

0 0

32

Testing the Balance Property

155

10 We need to be able to:

1. Track Balance

2092

17 307

NULL has a height of -1

2. Detect Imbalance3. Restore Balance

An AVL Tree

2052 2

310

10

3

data

height

children

92

7

0

0 017

030

115

1

Track height at all times.

AVL trees: find, insert, delete• AVL find:– Same as BST find.

• AVL insert:– Same as BST insert, except you need to

check your balance and may need to “fix” the AVL tree after the insert.

• AVL delete:– We’re not going to talk about it, but same

basic idea. Delete it, check your balance, and fix it.

Bad Case #1

Insert(6) Insert(3) Insert(1)

6

3

1

Simple illustration

Fix: Apply Single Rotation

36

1

2 1

AVL Property violated at this node (x)

1 6003

10

Intuition: 3 must become root

Single Rotation:1. Rotate between x and child Simple illustration

Bad Case #2

Insert(1) Insert(6) Insert(3) 6

1

3

Simple illustration

Single Rotation Does Not Work

12

12

AVL Property violated at this node (x)

It’s a BST, but it’s unbalanced

30

1

6

Simple illustration

1

3

0

6

Fix: Apply Double Rotation

311

6

2

3

1

1

2

AVL Property violated at this node (x)

1 600

30

60

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

illustration

1

AVL tree insert

1. Find spot for new key2. Hang new node there with this key3. Search back up the path for imbalance4. If there is an imbalance:

Case #1: Perform single rotation and exitCase #2: Perform double rotation and exit

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

Case #1: Single Rotation

h+1

a

ZY

bh

h

X

h+2

h+3

a

b

X < b < Y < a < Z

single rotation

XZY

Single rotation example

4

15

822

3 6

10 19

17 20

24

16

Single rotation example

4

15

822

3 6

10 19

17 20

24

16

Single rotation example

4

15

822

3 6

10 19

17 20

24

16

104

8

15

3 6

19

17

16

22

2420

Case #2: Double Rotation

a

ZX

c

V

h-1

h

h

h

U

h+1

h+2

b

h+3

Let’s break subtree into pieces:

Insert on left child’s right (at U or

V) c

Case #2: Double Rotation

a

ZX

c

V

h-1

h

h

h

U

h+1

h+2

b

h+3

b

Insert on left child’s right (at U or

V)

Let’s break subtree into pieces: c

Case #2: Double Rotation

a

ZX

c

V

h-1

h

h

h

U

h+1

h+3

b

Insert on left child’s right (at U or

V) c

a

XU V

Z

Let’s break subtree into pieces:

h+2

b

Can also do this in two rotations a

Z

b

X

c

V

h-1

h

h

h

U

h+1

h+2h+3

a

Zb

V

h-1h

h h

First rotation

X < b < U < c < V < a < Z

c

Second rotation

a

Zb

c

V

h-1h

h h

U

h+1

h+2

h+3

a

Z

b

X

c

V

h-1 hh h

X

U

25

Second rotation

h+1

Double rotation example

104

8

15

3 6

19

17

2016

22

24

5

Double rotation example

104

8

15

3 6

19

17

2016

22

24

5

Double rotation example

104

8

15

3 6

19

17

2016

22

24

5

Double rotation example

104

8

15

3 6

19

17

2016

22

24

5

15

19

17

2016

22

24

10

8

Double rotation example

104

8

15

3 6

19

17

2016

22

24

5

15

19

17

2016

22

24

10

8

6

4

3 5

Double rotation example 15

19

17

2016

22

24

10

8

6

4

3 5

15

19

17

2016

22

2410

8

6

4

3 5

Case #3:a

X

b

Z

ch-1

hh

h

VU

h+1

h+2

h+3

c

Z

a

X

bh-1 hh h

VU

h+1 h+1

h+2

Double rotation

Case #4:a

X

b

Y

h+1h

h

Z

h+2

h+3

a

X

b

Y

h

h+1

hZ

h+1

h+2

Single rotation

Recap of AVL tree insertLet x be the node where an imbalance occurs.

Four cases to consider. The insertion is in the1. left subtree of the left child of x.2. right subtree of the left child of x.3. left subtree of the right child of x.4. right subtree of the right child of x.

Cases 1 & 4 are solved by a single rotationCases 2 & 3 are solved by a double rotation

AVL complexity

What is the worst case complexity of a find?

O(log n)

What is the worst case complexity of an insert?

O(log n)

What is the worst case complexity of build a AVL Tree?

O(n log n)