Basic Algorithms

================ Start Lecture #19 ================

A Review of (the Real) Mod

I was asked to review modulo as it is no longer taught in high school, which I didn't know.

The top diagram shows an almost ordinary analog clock. The major difference is that instead of 12 we have 0. The hands would be useful if this was a video, but I omitted them for the static picture. Positive numbers go clockwise (cw) and negative counter-clockwise (ccw). The numbers shown are the values mod 12. This example is good to show arithmetic. (2-5) mod 12 is obtained by starting at 2 and moving 5 hours ccw, which gives 9. (-7) mod 12 is (0-7) mod 12 is obtained by starting at 0 and going 7 hours ccw, which gives 5.

To get mod 8, divide the circle into 8 hours instead of 12.

The bottom picture shows mod 5 in a linear fashion. In pink are the 5 values one can get when doing mod 5, namely 0, 1, 2, 3, and 4. I only illustrate numbers from -3 to 11 but that is just due to space limitations. Each blue bar is 5 units long so the numbers at its endpoints are equal mod 5 (since they differ by 5). So you just lay off the blue bar until you wind up in the pink.

End of Mod Review


3.2.1 Update Operations

Insertion

Begin by a standard binary search tree insertion. In the diagrams on the right, black shows the situation before the insertion; red after. The numbers are the heights. Ignore the blue markings, they are explained in the text as needed.

  1. Do a find for the key to be inserted.

  2. Presumably wind up at a leaf (meaning the key not already in the tree).

  3. Call this leaf w.

  4. Insert the item by converting w to an internal node at height one (i.e., the new internal node has two leaves as children).

  5. Call the new internal node w. We view the previous operation as expanding the leaf w into an internal node (with leaves as children).

  6. In the diagram below, w is converted from a black leaf into a red height 1 node.

Why aren't we finished?

  1. The tree may no longer be in balance, i.e. it may no longer by an AVL tree.

  2. We have raised the height of w by 1 (from 0 to 1).

  3. We may have raised the height of w's parent by 1, depending on the height of w's sibling.
  4. What is the problem? We just proceed up as in the figure on the right and eventually we hit the root and are done.

  5. WRONG.

  6. In the figure, before the insertion, siblings had the same height. This is certainly possible but not required. We know that the heights of siblings could have differed by 1.

  7. If an ancestor of w, had height one less than its sibling, then the insertion has made them equal. That is a particularly easy case. The height of the parent doesn't change and we are done. Recall that this situation was illustrated in the previous (small) figure.

  8. The difficult case occurs when an ancestor of w had height one greater than its sibling, then the insertion has made it two greater, which is not permitted for an AVL tree.

  9. For example the light blue node could have originally been at height 2 instead of 3.

The top left diagram illustrates the problem case from the previous figure (k was 3 above).
Definition: The operation we will perform when x, y, and z lie in a straight line is called a single rotation. When x, y, and z form an angle, the operation is called a double rotation.

Let us consider the upper left double rotation, which is the rotation that we need to apply the example above. It is redrawn to the right with the subtrees drawn and their heights labeled. The colors are so that you can see where they trees go when we perform the rotation.

Recall that x is an ancestor of w and has had its height raised from k-1 to k. The sibling of x is at height k-1 and so is the sibling of y. The reason x had its height raised is that one of its siblings (say the right one) has been raised from k-2 to k-1.
How do I know the other one is k-2?
Ans: we will discuss it later.

The double rotation transforms the picture on top to the one on the bottom. We now actually are done with the insertion. Let's check the bottom picture and make sure.

  1. The order relation remains intact. That is, every node is greater than its entire left subtree and less than its entire right subtree.

  2. The tree (now rooted at y) is balanced.

  3. Nodes x and z are each at height k. Hence y, the root of this tree is at height k+1.

  4. The tree above, rooted at z, has height k+2.

  5. But remember that before the insert z was at height k+1.

  6. So the rotated tree (which is after the insert) has the same height as the original tree before the insert.

  7. Hence every node above z in the original tree keeps its original height so the entire tree is now balanced.

Thus, if an insertion causes an imbalance, just one rotation re-balances the tree globally. We will see that for removals it is not this simple.

Answer to question posed above. One node was at height k-2 and its parent was at k-1. Hence the sibling was either at k-2 or k-3. But if it was at k-3, we would have found the tree out of balance when we got to x (x at k-1 after insertion, sibling at k-3). However, z is assumed to be the first time we found the tree out of balance starting from w (the insertion point) and moving up.