You are on page 1of 3

AVL Tree Definition

AVL trees are


balanced
2
An AVL Tree is a
binary search tree
such that for every
internal node v of T,
the heights of the
children of v can
differ by at most 1

AVL Trees
6

z
4

2004 Goodrich, Tamassia

AVL Trees

n(2)

Height of an AVL Tree

32

1
88

50
1
48

62

An example of an AVL tree where the


heights are shown next to the nodes:

AVL Trees

2004 Goodrich, Tamassia

n(1)

Insertion
Insertion is as in a binary search tree
Always done by expanding an external node.
Example:
44
44
17

78

17

78

c=z

a=y
32

50

48

n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), (by induction),
n(h) > 2in(h-2i)

88

62

32

50

88

48

62

b=x

54

Solving the base case we get: n(h) > 2 h/2-1


Taking logarithms: h < 2log n(h) +2
Thus the height of an AVL tree is O(log n)
AVL Trees

3
78

17

Fact: The height of an AVL tree storing n keys is O(log n).


Proof: Let us bound n(h): the minimum number of internal
nodes of an AVL tree of height h.
We easily see that n(1) = 1 and n(2) = 2
For n > 2, an AVL tree of height h contains the root node,
one AVL subtree of height n-1 and another of height n-2.
That is, n(h) = 1 + n(h-1) + n(h-2)
Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So

2004 Goodrich, Tamassia

4
44

before insertion
3

2004 Goodrich, Tamassia

after insertion
AVL Trees

Insertion Example, continued

Trinode Restructuring

5
44

let (a,b,c) be an inorder listing of x, y, z


perform the rotations needed to make b the topmost node of
the three

17
3

1
32

a=z

case 2: double rotation


(a right rotation about c,
then a left rotation about a)

c=y

b=y

unbalanced...

T1

a=z

T3
T2
44

4
3

17
b=x

1
32

T2

c=x

a=z

c=y

...balanced

x
z6

62

2 y
3

50

78

54

48

88

T2

case 1: single rotation


(a left rotation about a)

T2

T1

T0

T3

T0

T1

AVL Trees

2004 Goodrich, Tamassia

T3

T2
5

Restructuring
(as Single Rotations)
b=y

single rotation
b=y

double rotation

a=z

a=z

c=x

c=y

c=x

T3

T1

b=x
a=z

c=y

b=x

T0
T3

T0

T1

T0

T2

T3

T2

T2

T0

T2

T1

T3

T1
double rotation

c=z
c=z

a=x

T3

T3

T2

T2
AVL Trees

T1

c=z

b=x

c=z

a=x
T3

b=x
a=y

a=y

b=y

single rotation

b=y

2004 Goodrich, Tamassia

T1

double rotations:

a=z

T0

T0

AVL Trees

2004 Goodrich, Tamassia

Restructuring
(as Double Rotations)

Single Rotations:

T1

2
T3

T1

88

T1

b=y
T3

62

T0

b=x

c=x

54

T0

T0

T2

48

7
1

50

64
78

2y

(other two cases


are symmetrical)

a=z

T0
T2

T0

T3

T2

T1

T0

T1
7

2004 Goodrich, Tamassia

AVL Trees

T3

Removal

Rebalancing after a Removal

Removal begins as in a binary search tree, which


means the node removed will become an empty
external node. Its parent, w, may cause an imbalance.
Example:
44
44
17

62

32

17

50

48

62

78

50

88

54

before deletion of 32
2004 Goodrich, Tamassia

48

78

54

48

after deletion

AVL Trees

using a linked-structure binary tree

find takes O(log n) time


height of tree is O(log n), no restructures needed

put takes O(log n) time

initial find is O(log n)


Restructuring up the tree, maintaining heights is O(log n)

erase takes O(log n) time

initial find is O(log n)


Restructuring up the tree, maintaining heights is O(log n)

2004 Goodrich, Tamassia

AVL Trees

11

2004 Goodrich, Tamassia

c=x

78

54

44

b=y

62

50

a single restructure takes O(1) time

17

88

AVL Tree Performance

Let z be the first unbalanced node encountered while travelling


up the tree from w. Also, let y be the child of z with the larger
height, and let x be the child of y with the larger height
We perform restructure(x) to restore balance at z
As this restructuring may upset the balance of another node
higher in the tree, we must continue checking for balance until
the root of T is reached
62
44
a=z

17

50

48

88
AVL Trees

78

88

54

10

You might also like