You are on page 1of 15

5 AVL trees: deletion

Summer Term 2011

Jan-Georg
Jan Georg Smaus
Reminder: Definition of AVL trees

Definition: A binaryy search tree is called AVL tree or height-balanced


g tree,, if
for each node v the height of the right subtree h(Tr) of v and the height of
the left subtree h(Tl) of v differ by at most 1.

Balance factor:

31.05.2011 Theory 1 - AVL trees: deletion 2


Deletion from an AVL tree

ƒ We p
proceed similarly
y to standard search trees:

1. Search for the key to be deleted.

2. If the key is not contained, we are done.

3. Otherwise we distinguish three cases:


3
1. The node to be deleted has two leaves as its children.
2. The node to be deleted has one internal node and one leaf as
its children.
3. The node to be deleted has two internal nodes as its children.

ƒ After deleting a node the AVL property may be violated (similar to


insertion). This must be fixed appropriately.

31.05.2011 Theory 1 - AVL trees: deletion 3


Node has only leaves as its children

Subcase of a one-node tree

2 subcases: sibling has


height 0. May need
readjustment.

Subcase: right sibling has height 1. No readjustment needed. There is also the
symmetric subcase.
subcase

31.05.2011 Theory 1 - AVL trees: deletion 4


Node has only leaves as its children

Subcase: height = 2
Deletion of node

Left-rotation
(or right-left
right left
rotation, not
shown)

Note: height may have decreased by 1! May need readjustment


31.05.2011 Theory 1 - AVL trees: deletion 5
Node has one internal node as child

Note that this illustration covers the two possible subcases. Height has
decreased which may need readjustment
decreased, readjustment.

31.05.2011 Theory 1 - AVL trees: deletion 6


Node has two internal nodes as children

ƒ First we p
proceed jjust like we do in standard search trees:

1. Replace the content of the node to be deleted p by the content of its


symmetrical successor q.
q

2. Then delete node q.

ƒ Since q can have at most one internal node as a child (the right one),
cases 1 and 2 apply for q.

31.05.2011 Theory 1 - AVL trees: deletion 7


The method upout
ƒ The method upout works similarly to upin.

ƒ It is called recursively along the search path and adjusts the balance
factors via rotations and double rotations.

ƒ When upout is called for a node p, we have (see above):

1. bal(p) = 0
1
2. The height of the subtree rooted in p has decreased by 1.

ƒ upout will be called recursively as long as these conditions are fulfilled


(invariant).

ƒ g
Again, we distinguish
g 2 cases, depending
p g on whether p Is the left or the
right child of its parent φp.

ƒ Since the two cases are symmetrical, we only consider the case
where p is the left child of φp.
φp

31.05.2011 Theory 1 - AVL trees: deletion 8


Case 1.1: p is the left child of φp and bal(φp)= -1

φp -1 φp 0 upout(φp)

upout(p) p 0

ƒ Since the height of the subtree rooted in p has decreased by 1, the


b l
balance ffactor
t off φp changes
h tto 0.
0

ƒ By this, the height of the subtree rooted in φp has also decreased by 1


and we have to call upout(φp) (the invariant now holds for φp!).

31.05.2011 Theory 1 - AVL trees: deletion 9


Case 1.2: p is the left child of φp and bal(φp)= 0

φp 0 φp 0

upout(p) p p 0 done!

ƒ Since the height of the subtree rooted in p has decreased by 1, the


b l
balance ffactor
t off φp changes
h tto 1.
1

ƒ Then we are done, because the height of the subtree rooted in φp has
not changed.

31.05.2011 Theory 1 - AVL trees: deletion 10


Case 1.3: p is the left child of φp and bal(φp)= +1

φp 0

upout(p) p 0 q

ƒ Then the right subtree of φp was higher (by 1) than the left subtree
before the deletion.

ƒ Hence, in the subtree rooted in φp the AVL property is now violated.

ƒ We distinguish three cases according to the balance factor of q.


31.05.2011 Theory 1 - AVL trees: deletion 11
Case 1.3.1: bal(q) = 0

φp v +1 w -1

p u 0 q w 0 v +1

left rotation done!


p u 0

0 1 3

h–1 h–1 h+1


2 3
0 1 2
h+1 h+1
h-1 h–1 h +1

31.05.2011 Theory 1 - AVL trees: deletion 12


Case 1.3.2: bal(q) = +1
φp v +1 r w 0
upout(r)

upout(p) p u 0 q w 1 v 0

left rotation
p u 0

0 1

h–1 h–1
2
0 1 2 3
h 3
h-1 h–1 h h+1
h+1

ƒ Again, the height of the subtree has decreased by 1, while bal(r) = 0


(invariant).
ƒ Hence we call upout(r).

31.05.2011 Theory 1 - AVL trees: deletion 13


Case 1.3.3: bal(q) = -1
φp v +1 r z 0 upout(r)

upout(p)
p u 0 q w -1 double rotation v 0 w 0
right left
right-left
z
p u 0
0 1

h–1 h–1
4

h 0 1 2 3 4
2 3
h-1 h–1 h

ƒ Since bal(q) = -1, one of the trees 2 or 3 must have height h.


ƒ Therefore, the height of the complete subtree has decreased by 1, while
bal(r) = 0 (invariant).
ƒ Hence, we again call upout(r).
31.05.2011 Theory 1 - AVL trees: deletion 14
Observation

ƒ Unlike insertions,, deletions mayy cause recursive calls of upout


p after a
double rotation.

ƒ Therefore, in general a single rotation or double rotation is not sufficient


Therefore
to rebalance the tree.

ƒ There are examples


Th l where
h f allll nodes
for d along
l th
the search
h path
th rotations
t ti
or double rotations must be carried out.

ƒ Since h ≤ 1.44 ... log2(n) + 1, we may conclude that the deletion of a key
form an AVL tree with n keys can be carried out in at most O(log n)
steps.

ƒ AVL trees are a worst-case efficient data structure for finding, inserting
and deleting
g keys.
y

31.05.2011 Theory 1 - AVL trees: deletion 15

You might also like