# Avl Tree

Topics: Binary search tree, AVL tree, Binary trees Pages: 9 (812 words) Published: January 9, 2012
AVL Tree Definition AVL Trees
v
6 3 4 8

AVL trees are balanced. An AVL Tree is a
z

44 2 17 1 32

4 3 1

binary search tree
such that for every internal node v of T, the heights of the

78 2 50 1 48 62 1

88

children of v can differ by at most 1.
AVL Trees 1

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

Insertion in an AVL Tree
Insertion is as in a binary search tree Always done by expanding an external node. Example: 44 44 17 78 17 a=y 32 50 88 32 50 88 78 c=z

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)

48

62

48

62

b=x

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

w

54

before insertion
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 b=y T0 c=x T1 T2 T3 a=z c=x b=y T1 T2 a=z c=y

Insertion Example, continued
44 2 17 32 1 1 3 5

z

64
78

2 y
50 1 2

7
1 88

1
48

4
62

(other two cases are symmetrical)
T0

x
5

a=z c=y

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

3
54

unbalanced...

T3 T2
4 3 17 32 1

T0

b=x T3 b=x

T1
2

44

4 x
62

1 1
48

2 y 2
50

z6
5
78

3
54

2

...balanced
T3

1

7
88

1

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

T2
T0 T1 T2 T3 T0 T1 T2 5 AVL Trees AVL Trees

T0

T1

6

T3

Restructuring (as Single Rotations)
Single Rotations:
a=z b=y c=x T0 T1 T3 T0 b=y a=z c=x T3 single rotation

Restructuring (as Double Rotations)
double rotations:
a=z c=y b=x
T1 T2

double rotation a=z

b=x

c=y T2

T0 T1

T2

T2

T3

T0

T1

T3

c=z
c=z b=y a=x T0 T2 T3 T3
AVL Trees

double rotation a=y

b=x c=z T2

single rotation

b=y a=x c=z

a=y b=x T3 T2 T0 T1
AVL Trees

T3

T1

T0
8

T1

T2

T1

T0
7

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: 44 44 17 62 17 62

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 a=z 44 62 b=y 17 44 62 78

32

50

78

50

78

w 88

17

48

54

88

48

54

50

78

c=x

50

88

before deletion of 32
AVL Trees

after deletion
9

48

54

88 AVL Trees

48

54

10

Running Times for AVL Trees
a single restructure is O(1)

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

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

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

AVL Trees

11