Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Presentation for use with the textbook Algorithm Design and

Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

AVL Trees
6
v
3 8
z
4

© 2015 Goodrich and Tamassia AVL Trees 1


AVL Tree Definition
AVL trees are rank-
balanced trees.
The rank, r(v), of each 4
44
node, v, is its height. 2 3
Rank-balance rule: 17 78
1 2
An AVL Tree is a binary 32 50 88
1

search tree such that for 1 1


every internal node v of 48 62

T, the heights (ranks) of


the children of v can
An example of an AVL tree where the
differ by at most 1.
ranks are shown next to the nodes

© 2015 Goodrich and Tamassia 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 (by induction): 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)
© 2015 Goodrich and Tamassia AVL Trees 3
Insertion
Insertion is as in a binary search tree
Always done by expanding an external node.
Example:
44 44

c=z
17 78 17 78
a=y

32 50 88 32 50 88

48 62 48 62
b=x

54
w
before insertion

after insertion
© 2015 Goodrich and Tamassia AVL Trees 4
Trinode Restructuring
Let (a,b,c) be the inorder listing of x, y, z
Perform the rotations needed to make b the topmost node of the three
Single rotation Double rotation around
around b a=z
c and a
a=z

c=y
b=y
T0
T0
c=x b=x
T3
T1 b=y b=x

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

T0 T1 T2 T3 T0 T1 T2 T3

© 2015 Goodrich and Tamassia AVL Trees 5


Insertion Example, continued
5
44
2
z 64
17 78 7
3
2y 1
1
32 1 50 4 88
1
2 x
48
1
3 62
5
54 T3
unbalanced... T2
T0
4
T1 44 4
2 3 x
17
2 y 62
z6
1 2 2
32
1
1 50 3 5
78 7
1
The image cannot be
displayed. Your computer 1
...balanced
may not have enough
memory to open the

54 88
image, or the image may

48 have been corrupted.


Restart your computer, and
then open the file again. If
the red x still appears, you
may have to delete the
image and then insert it
again.

T2

T0 T1 T3
© 2015 Goodrich and Tamassia AVL Trees 6
Restructuring (as Single Rotations)
  Single Rotations:

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

T0 T3
T1 T3 T0 T1 T2
T2

c=z single rotation b =y


b =y a =x c=z
a =x

T3 T3
T0 T2 T2 T1 T0
T1
© 2015 Goodrich and Tamassia AVL Trees 7
Restructuring (as Double Rotations)
double rotations:

a=z double rotation b=x


c=y a=z c=y
b=x

T0 T2
T2 T3 T0 T1 T3
T1

c=z double rotation b=x


a=y a=y c=z
b=x

T0 T2
T3 T2 T3 T1 T0
T1
© 2015 Goodrich and Tamassia AVL Trees 8
Pseudo-code
Insertion.

© 2015 Goodrich and Tamassia AVL Trees 9


Pseudo-code
Rebalance at a node violating the rank rule.

© 2015 Goodrich and Tamassia AVL Trees 10


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

32 50 78 50 78

48 54 88 48 54 88

before deletion of 32 after deletion

© 2015 Goodrich and Tamassia AVL Trees 11


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 a trinode restructuring 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
a=z 44

44 78
w 17 62 b=y

17 50 88
50 78 c=x

48 54
48 54 88

© 2015 Goodrich and Tamassia AVL Trees 12


Pseudo-code
Removal

© 2015 Goodrich and Tamassia AVL Trees 13


AVL Tree Performance
AVL tree storing n items
n  The data structure uses O(n) space
n  A single restructuring takes O(1) time
w  using a linked-structure binary tree
n  Searching takes O(log n) time
w  height of tree is O(log n), no restructures needed
n  Insertion takes O(log n) time
w  initial find is O(log n)
w  restructuring up the tree, maintaining heights is O(log n)
n  Removal takes O(log n) time
w  initial find is O(log n)
w  restructuring up the tree, maintaining heights is O(log n)

© 2015 Goodrich and Tamassia AVL Trees 14

You might also like