Вы находитесь на странице: 1из 10

Potential Balance Conditions

What are some candidate balance conditions on BSTs?


1. Left and right subtrees
of the root have
equal number of nodes

CSE 326: Data Structures


AVL Trees
Brian Curless
Spring 2008

2. Left and right subtrees


of the root have
equal height
1

Potential Balance Conditions

Balancing Trees
Many algorithms exist for keeping trees balanced
Adelson-Velskii and Landis (AVL) trees
Splay trees and other self-adjusting trees
B-trees and other multiway search trees (for
very large trees)

3. Left and right subtrees


of every node have
equal number of nodes

4. Left and right subtrees


of every node
have equal height

Today we will talk about AVL trees

The AVL Tree Data Structure


Ordering property
Same as for BST

Recursive Height Calculation


Recall: height is max number
of edges from root to a leaf

Structural properties
1. Binary tree property
(0,1, or 2 children)
2. Heights of left and right
subtrees of every node
differ by at most 1
Result:
Worst case depth of any
node is: O(log n)

What is the height at A?

11

6
4

10
7

12
13

14
15
5

Note: height(null) = -1

Proving Shallowness Bound

AVL trees or not?

A0

A1

A2

A3

A4

11

12

10
6
4
1

8
5

11

Ah is smallest AVL tree of height h.


Built from Ah-1 and Ah-2 attached to a new root.
(Note: these Ahs are not unique; e.g., could swap children.)

3
2
7

Minimum Size of an AVL Tree


m(h) = minimum number of nodes in an AVL tree
of height h.
h
Base cases
m(0) = 1, m(1) = 2
h-2
Induction
m(h) = m(h-1) + m(h-2) + 1
Bound solution
m(h) > h - 1
is the golden ratio, (1+5)/2

The Golden Ratio


Since the Renaissance, many artists and architects have
proportioned their work (e.g., length:height) to
approximate the golden ratio:
h-1

a+b a
= =
a
b

The golden
section:

Set a=1, solve for positive b, compute ratio: =

1+ 5
1.62
2

1+ 5
=
Note: =

Proof that m(h) > h -1

Maximum Height of an AVL Tree

Base cases h=0,1:


m(0) = 1 > 0 -1 = 0

m(1) = 2 > 1-1 0.62

Suppose we have n nodes in an AVL tree of height h.

m(h-1) > h-1 1

We can now say:


n > m(h) > h 1

Assume true for h-2 and h-1:


m(h-2) > h-2 1

10

Induction step:

What does this say about the complexity of h?

m(h) = m(h-1) + m(h-2) + 1 > (h-1 - 1) + (h-2 - 1) + 1


(h-1 - 1) + (h-2 - 1) + 1 = h-2 ( +1) 1
= h-2 (2) 1
= h - 1
h
m(h) > - 1

11

12

Testing the Balance Property


We need to be able to:

10
5

An AVL Tree
10

1. Track Balance

15

2. Detect Imbalance
2

10

20

3. Restore Balance

20
1

height
children

data

15

30

What if we insert(30)?
13

AVL trees: find, insert

14

Case #1: left-left insertion (zig-zig)

AVL find:

h+1

same as BST find.

AVL insert:

h+2

same as BST insert, except may need to fix


the AVL tree after inserting new value.

Insert on left childs left

We will consider the 4 fundamental insertion


cases

a
b
X
15

h
h

Y
16

Case #1: repair with single rotation


a
h+2
h+1

Single rotation example

h+3

15

4
3

Single rotation

X<b<Y<a<Z

22
24

19

10
17

20

16
15

b
h

h+1

3
Height of tree before/after? Effect on Ancestors? Cost?

17

Case #2: left-right insertion (zig-zag)


a

h+1
h

18

Case #2: repair with single rotation

h+2

a
h+2

Insert on left childs right

Single rotation
b

Z
X

h+1

X<b<Y<a<Z

a
h

h+3
h

10

a
h+1

Y
19

Are we better off now?

Z
20

Case #2: trying again


Lets break subtree Y
into pieces:

h+1

h-1

Case #2: first rotation

c
U

a
c

15

17

22

16

20

24

5
15

19

a
h-1

10

3
Second rotation

19

h-1

22

Double rotation, step 1

h+3

h
h-1

21

a
c

Case #2: second rotation


h+1

First rotation

h-1

h-1

h+2

h+1

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

V
Insert on left childs right (at U or V)

h-1

h+3

h+2

h+2

10
V

Height of tree before/after? Effect on Ancestors? Cost?

17
16

23

22
20

24
24

Double rotation, step 2

Case #3: right-left insertion (zig-zag)


h+3

15
19

8
6

17

10

4
3

h+2

20

h+1

22

16

24

h-1

5
Double rotation

15
h+2

19

h+1

17
16

22
20

24
25

Case #4: right-right insertion (zig-zig)


h+3

a
h

h+1

Single rotation
h+2
h+1
h

h-1

h+1

26

AVL tree case summary

Cases 1 & 4 are solved by a single rotation:


1. Rotate between a and child

b
h+1

a
h

Let x be the node where an imbalance occurs.


Four cases to consider. The insertion below a is in the
1. left childs left subtree. (zig-zig)
2. left childs right subtree. (zig-zag)
3. right childs left subtree. (zig-zag)
4. right childs right subtree. (zig-zig)

h+2

Z
27

Cases 2 & 3 are solved by a double rotation:


1. Rotate between as child and grandchild
2. Rotate between a and as new child

28

Single and Double Rotations:

Insertion procedure

Inserting what integer values would


cause the tree to need a:

1.
2.
3.
4.

1. single rotation?
5
2

2. double rotation?

11
13

Find spot for new key


Hang new node there with this key
Search back up the path for imbalance
If there is an imbalance:
cases #1,#4: Perform single rotation and exit

cases #2,#3: Perform double rotation and exit


3. no rotation?
29

Both rotations keep the subtree height unchanged.


Hence only one rotation is sufficient!

More insert examples

Single Rotation
4

15
0

10

10

Insert(33)

20
17

30

15
9

12

20
17

10

12

30

30

Unbalanced?

33

How to fix?
31

32

More insert examples

Single Rotation (oops!)

10

Insert(18)
1

15

10

12
0

Unbalanced?

20
1

30

12

17

15

20

10

17

30
0

How to fix?

18
33

34

Double Rotation (Step #1)


4

10

10

15

Double Rotation (Step #2)

12

20
1

15

12

10

15

17

10

12

17

30

20

18

18

35

30

36

More insert examples


Insert(3)

10

17

15

Unbalanced?

Insert into an AVL tree: 5, 8, 9, 4, 2, 7, 3, 1

12

20
0

18 30

How to fix?
37

AVL complexity
What is the worst case complexity of an insert?

What is the worst case complexity of a find?

39

38

Вам также может понравиться