You are on page 1of 3

AVL Tree Definition (§ 9.

2)
AVL trees are
AVL Trees
4
balanced. 44
2 3
An AVL Tree is a 17 78

v
6
binary search tree 1 2 1
32 50 88
3 8 such that for every
z 1 1
4 internal node v of T, 48 62
the heights of the
children of v can
differ by at most 1. An example of an AVL tree where the
heights are shown next to the nodes:

© 2004 Goodrich, Tamassia AVL Trees 1 © 2004 Goodrich, Tamassia 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
Fact: The height of an AVL tree storing n keys is O(log n).
Always done by expanding an external node.
Proof: Let us bound n(h): the minimum number of internal
nodes of an AVL tree of height h. Example: 44 44

We easily see that n(1) = 1 and n(2) = 2 c=z


17 78 17 78
For n > 2, an AVL tree of height h contains the root node, a=y
one AVL subtree of height n-1 and another of height n-2.
32 50 88 32 50 88
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 48 62 48 62
b=x
n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction),
n(h) > 2in(h-2i) 54
Solving the base case we get: n(h) > 2 h/2-1 w

Taking logarithms: h < 2log n(h) +2 before insertion after insertion


Thus the height of an AVL tree is O(log n)
© 2004 Goodrich, Tamassia AVL Trees 3 © 2004 Goodrich, Tamassia AVL Trees 4
Insertion Example, continued
Trinode Restructuring 5
44
let (a,b,c) be an inorder listing of x, y, z 2
z 64
17 78 7
perform the rotations needed to make b the topmost node of 1 3
2 y 1
the three 32 1 50 4 88
2 x
(other two cases 1
a=z case 2: double rotation 48 3 62
5
a=z are symmetrical) 1
(a right rotation about c, 54 T3
b=y
c=y then a left rotation about a) unbalanced... T2
T0 T0
T0 4
T1 44
b=x 4
c=x
2 3 x
T3
T1
b=y b=x
17
2 y 62
z6
1 2 2
T1 T2
T2 T3 32
1
1 50 3 5
78 7
1 1
a=z c=x a=z c=y
...balanced 48 54 88

case 1: single rotation T2


(a left rotation about a) T0 T1 T2 T3 T0 T1 T2 T3
T0 T1 T3
© 2004 Goodrich, Tamassia AVL Trees 5 © 2004 Goodrich, Tamassia AVL Trees 6

Restructuring Restructuring
(as Single Rotations) (as Double Rotations)
Single Rotations: double rotations:

a=z single rotation b=y a=z double rotation b=x


b=y a=z c=x c=y a=z c=y
c=x b=x
T0 T3 T0 T2
T1 T3 T0 T1 T2 T2 T3 T0 T1 T3
T2 T1

c=z double rotation b=x


c=z single rotation b=y a=y a=y c=z
b=y a=x c=z b=x
a=x
T0 T2
T3 T3 T3 T2 T3 T1 T0
T0 T2 T2 T1 T0 T1
T1
© 2004 Goodrich, Tamassia AVL Trees 7 © 2004 Goodrich, Tamassia AVL Trees 8
Removal in an AVL Tree Rebalancing after a Removal
Removal begins as in a binary search tree, which Let z be the first unbalanced node encountered while travelling
means the node removed will become an empty 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.
external node. Its parent, w, may cause an imbalance.
We perform restructure(x) to restore balance at z.
Example: 44 44 As this restructuring may upset the balance of another node
higher in the tree, we must continue checking for balance until
17 62 17 62 the root of T is reached
62
a=z 44

32 50 78 50 78 44 78
w 17 62 b=y

48 54 88 48 54 88 17 50 88
50 78 c=x

48 54
48 54 88
before deletion of 32 after deletion

© 2004 Goodrich, Tamassia AVL Trees 9 © 2004 Goodrich, Tamassia AVL Trees 10

Running Times for


AVL Trees
a single restructure is O(1)
„ using a linked-structure binary tree
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)

© 2004 Goodrich, Tamassia AVL Trees 11

You might also like