Professional Documents
Culture Documents
Jan-Georg
Jan Georg Smaus
Reminder: Definition of AVL trees
Balance factor:
We p
proceed similarly
y to standard search trees:
Subcase: right sibling has height 1. No readjustment needed. There is also the
symmetric subcase.
subcase
Subcase: height = 2
Deletion of node
Left-rotation
(or right-left
right left
rotation, not
shown)
Note that this illustration covers the two possible subcases. Height has
decreased which may need readjustment
decreased, readjustment.
First we p
proceed jjust like we do in standard search trees:
Since q can have at most one internal node as a child (the right one),
cases 1 and 2 apply for q.
It is called recursively along the search path and adjusts the balance
factors via rotations and double rotations.
1. bal(p) = 0
1
2. The height of the subtree rooted in p has decreased by 1.
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
φp -1 φp 0 upout(φp)
upout(p) p 0
φp 0 φp 0
upout(p) p p 0 done!
Then we are done, because the height of the subtree rooted in φp has
not changed.
φp 0
upout(p) p 0 q
Then the right subtree of φp was higher (by 1) than the left subtree
before the deletion.
φp v +1 w -1
p u 0 q w 0 v +1
0 1 3
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
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 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