Basic Algorithms
================ Start Lecture #20 ================
Here are the three pictures for the remaining three possibilities.
That is, the other double rotation and both single rotations. The
original configuration is shown on top and the result after the
rotation is shown immediately below.
What is the complexity of insertion?

Let n be the number of nodes in the tree before the
insertion.

Finding the insertion point is Θ(log n).

Expanding the leaf and inserting the item is Θ(1).

Walking up the tree looking for an imbalance is Θ(1) per
level, which is O(log n) since the tree has height Θ(log
n).

Performing the one needed rotation is
Θ(1).
Hence we have the following
Theorem: The complexity of insertion in an AVL
tree is Θ(log n).
Problem Set 3, problem 2.
Please read the entire problem before beginning.

Draw an avl tree containing items with integer keys. Draw the
internal nodes as circles and write the key inside the circle.
Draw the leaves as squares; leaves do not contain items.
You will want to read the remaining parts before drawing this
tree.

Choose a key not present in the tree you drew in part A whose
insertion will require the "other" double rotation in order to
restore balance (i.e., the double rotation shown in the diagram
above showing one double and two single rotations).
Draw the tree after the insertion, but prior to the rotation.
Then draw the tree after the rotation.

Choose a key not present in the tree you drew in part A whose
insertion will require a single rotation in order to
restore balance.
Draw the tree after the insertion, but prior to the rotation.
Then draw the tree after the rotation.

Choose a key not present in the tree you drew in part A whose
insertion will require the "other" single rotation in order to
restore balance
Draw the tree after the insertion, but prior to the rotation.
Then draw the tree after the rotation.
Removal
In order to remove an item with key k, we begin just as we did for
an ordinary binary search tree. I repeat the procedure here.
The key concern is that we cannot simply remove an item from
an internal node and leave a hole as this would make future searches
fail. The beginning of the removal technique is familiar:
w=TreeSearch(k,T.root()). If w is a leaf, k is not present, which we
signal.
If w is internal, we have found k, but now the fun begins.
Returning the element with key k is easy, it is the element
stored in w.
We need to actually remove w, but we cannot leave a hole.
There are three cases.

If we are lucky both of w's children are leaves. Then we can
simply replace w with a leaf. (Recall that leaves do not contain
items.) This is the trivial case

The next case is when one child of w is a leaf and the other, call
it z, is an internal node.
In this case we can simply replace w by z;
that is have the parent of w now point to z.
This removes w as desired and also removes the leaf child of w,
which is OK since leaves do not contain items.
This is the easy case.

Note that the above two cases can be considered the same.
In both cases we notice that one child of w is a leaf and replace
w by the other child (and its descendents, if any).

Now we get to the difficult case: both children of w are internal
nodes. What we will do is to replace the item in w with the item
that has the next highest key.

First we must find the item with the next highest key.
But that is simply the next item in the inorder traversal.
So we go right and then keep going left until we get a leaf.
The parent of this leaf is the item we seek. Call the parent
y.

Store the item in y in the node w. This removes the old
item of w, which we wanted to do.

Does the tree still have its items in the correct order?
That is are parents still bigger than (or equal to if we
permit duplicate keys) all of the left subtree and smaller
than all of the right subtree?

Yes. The only new parent is the item y which has now
moved to node w. But this is the item right after the old
item in w. Since it came from the right subtree it is
bigger than the left and since it was the smallest in the
right, it is smaller than the right.

But what about the old node y? It's left child is a leaf so
it is the easy or trivial case and we just replace y by the
other child and its descendants.
But now we have to possibly restore balance, i.e., maintain the AVL
property. The possible trouble is that the light green node on the
left has been replaced by the light blue on the right, which is of
height one less. This might cause a problem. The
sibling of the light green (not shown) might have height equal to, one
less than, or one greater than, the light green.

If the sibling's height was equal to the light green, it is one
greater than the light blue, which is still in balance and their
parent has not changed height so all is well.

If the sibling's height was one less than the light green, it is
equal to the light blue, which remains in balance. But their
parent height has dropped by one so we need to look at it to see
if there is trouble.

If the sibling's height was one greater than the light green, it
is two greater than the light blue so we are out of balance.
To summarize the three cases either

We are balanced and done.

We proceed to the parent of the light blue and try again.

We must rebalance.
In the second case we move up the tree and again have one of the
same three cases so either.

We will hit a case 1 and are done.

We reach the root and are done.

We need to rebalance
The rebalancing is again a single or double rotation (since the
problem is the same so is the solution).
The rotation will fix the problem but the result has a highest node
whose height is one less the highest prior to the rotation (in the
diagrams for single and double rotation the largest height dropped
from k+2 to k+1).
Unlike the case for insertions this height reduction does not
cancel out a previous height increase. Thus the lack of balance may
continue to advance up the tree and more rotations may be needed.
Problem Set 3 problem 3 (end of problem set 3).
Please read the entire problem before beginning.

Draw an avl tree containing items with integer keys. Draw the
internal nodes as circles and write the key inside the circle.
Draw the leaves as squares; leaves do not contain items.
You will want to read the remaining parts before drawing this
tree.

Choose a key present in the tree you drew in part A whose
removal will require a double rotation and a
single rotation in order to
restore balance.
Draw the tree after the insertion, but prior to the rotations.
Then draw the tree after the double rotation, but prior to the
single rotation.
Finally, draw the tree after both rotations.
What is the complexity of a removal? Remember that the height of
an AVL tree is Θ(log(N)), where N is the number of nodes.

We must find a node with the key, which has complexity
Θ(height) = Θ(log(N)).

We must remove the item: Θ(1).

We must rebalance the tree, which might involve Θ(height)
rotations. Since each rotation is Θ(1), the complexity is
Θ(log(N))*Θ(1) = Θ(log(N)).
Theorem: The complexity of removal for an AVL tree
is logarithmic in the size of the tree.
3.2.2 Performance
The news is good.
Search, Inserting, and Removing all have logarithmic complexity.
The three operations all involve a sweep down the tree searching
for a key, and possibly an up phase where heights are adjusted and
rotations are performed. Since only a constant amount of work is
performed per level and the height is logarithmic, the complexity is
logarithmic.