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

35
Data Structures AVL Trees

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

Page 1: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

Data Structures

AVL Trees

Page 2: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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!

Page 3: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 4: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 5: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

84

6

AVL Tree?

1 2

3

111

10 12

70

0 0

0 1

Page 6: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

84

6

13

4

AVL Tree?

3

111

2

5

0

0 0 7 0

1

2

Page 7: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 8: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 9: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

An AVL Tree

2052 2

310

10

3

data

height

children

92

7

0

0 017

030

115

1

Track height at all times.

Page 10: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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.

Page 11: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

Bad Case #1

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

6

3

1

Simple illustration

Page 12: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 13: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

Bad Case #2

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

1

3

Simple illustration

Page 14: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 15: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 16: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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!

Page 17: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 18: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

Single rotation example

4

15

822

3 6

10 19

17 20

24

16

Page 19: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

Single rotation example

4

15

822

3 6

10 19

17 20

24

16

Page 20: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

Single rotation example

4

15

822

3 6

10 19

17 20

24

16

104

8

15

3 6

19

17

16

22

2420

Page 21: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 22: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 23: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 24: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 25: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 26: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

Double rotation example

104

8

15

3 6

19

17

2016

22

24

5

Page 27: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

Double rotation example

104

8

15

3 6

19

17

2016

22

24

5

Page 28: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

Double rotation example

104

8

15

3 6

19

17

2016

22

24

5

Page 29: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

Double rotation example

104

8

15

3 6

19

17

2016

22

24

5

15

19

17

2016

22

24

10

8

Page 30: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 31: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 32: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 33: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 34: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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

Page 35: Data StructuresData Structures AVL Trees. Balanced BST Observation BST: the shallower the better! For a BST with n nodes – Average height is O(log n)

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)