Vous êtes sur la page 1sur 11

AVL Trees

v
6 3 4 8

2004 Goodrich, Tamassia

AVL Trees

AVL Tree Definition


AVL trees are balanced. 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.
2004 Goodrich, Tamassia

4 44 2 17 1 32 1 48 62 2 50 1 88 78 1 3

An example of an AVL tree where the heights are shown next to the nodes:
AVL Trees 2

n(2)

3 4 n(1)

Height of an AVL Tree

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
n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), (by induction), n(h) > 2in(h-2i)

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)
2004 Goodrich, Tamassia AVL Trees

Insertion in an AVL Tree


Insertion is as in a binary search tree Always done by expanding an external node. 4 4 Example:
4 4 1 7 3 2 4 8 5 0 6 2 7 8 8 8 1 7 3 2 4 8 5 4 a=y 5 0 6 2 7 8 8 8 c=z

b=x

before insertion
2004 Goodrich, Tamassia AVL Trees

after insertion
4

Trinode Restructuring
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
a = z T
0

(other two cases are symmetrical)


b = y T c = x T
3 0

a = z

c = y b = x T
3

case 2: double rotation (a right rotation about c, then a left rotation about a)

T
1

T
2

b = y a = z T
0

T c = x
1

T
2

b = x a = z T
0

c = y T
1

case 1: single rotation (a left rotation about a)


2004 Goodrich, Tamassia

T
1

T
2

T
3

T
2

T
3

AVL Trees

Insertion Example, continued


5 44 2 17 1 32 1 48 1 3

64
78

2y
50 2

7
1 88

4
62

x
5

3
54

unbalanced...

T3 T2
4 3 17 1 32 1

T0

T1
2

44

4
62

x z6
5
78 2

2 y 2 1
48 50

3
54

...balanced

7
88

T2
2004 Goodrich, Tamassia AVL Trees

T0

T1

T3

Restructuring (as Single Rotations)


Single Rotations:
a= z b= y c= x T0 T1 T2 T3 T0 T1 T2 T3 sin g le ro ta tio n a= z b= y c= x

c=z b=y a=x T3 T0


2004 Goodrich, Tamassia

single rotation a=x T3 T2


AVL Trees

b=y c=z

T2 T1

T1

T0
7

Restructuring (as Double Rotations)


double rotations:
a=z c=y b=x T0 T2 T1
c=z a=y b=x T3 T0 T2 T1
2004 Goodrich, Tamassia AVL Trees 8

double rotation a=z

b=x c=y T2

T3

T0

T1

T3

double rotation a=y T2

b=x c=z

T3

T1

T0

Removal in an AVL Tree


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: 4 4
4 1 7 3 2 4 8 5 0 5 4 6 2 7 8 8 8 4 8 1 7 5 0 5 4 4 6 2 7 8 8 8

before deletion of 32
2004 Goodrich, Tamassia AVL Trees

after deletion
9

Rebalancing after a Removal


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 6
a=z w 1 7 5 0 4 8 2004 Goodrich, Tamassia 5 4 4 4 2 6 2 7 8 8 8 AVL Trees 4 4 1 7 4 8 5 0 5 4 10 7 8 8 8

b=y

c=x

Running Times for AVL Trees


a single restructure is O(1)

using a linked-structure binary tree height of tree is O(log n), no restructures needed initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n)

find is O(log n)

insert is O(log n)

remove is O(log n)

2004 Goodrich, Tamassia

AVL Trees

11

Vous aimerez peut-être aussi