================ Start Lecture #18 ================
I forgot to write homework 16 on the board so I will accept it next
tuesday as well as today. Also I forgot to ask for homework 15 last
time so I will accept it today.
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 (shown in purple) might have height equal
to, one less than, or one greater than, the light green.

If the purple height was equal to the light green, it is one
greater than the light blue, which is still in balance and their
parent (red) 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 red's
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 up the tree to red, 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 keep hitting case 2, keep going up and eventually reach the
root. Done.

We hit case 3 and 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.
Homework: R3.6
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 removal, 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.