# 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.

### 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.
• Since w was at height 0 and the tree was balanced, the sibling must have height -1, 0, or +1. Since -1 is impossible, we have just two possibilities.

• If the sibling was at height 1, their parent was, and still is, at height 2. Hence no heights other than w's changed and the tree remains in balance. So in this case, which is illustrated in the figure above, we are indeed done.

• If the sibling was at height 0, their parent was at height 1 and has had its height changed to 2 (since w is at height 1). This situation is illustrated on the right. Note that not all of the tree is shown.
• The top node need not be the root of the tree, i.e. it could be the right or left child of a node further up.
• Nodes not ancestors of w do not have their descendants shown.
• Recall that the black shows the original tree fragment and the red is what happens after we expand w to do the insertion. The black and red numbers are the heights before and after.
• For the moment ignore the blue shading and the blue 2.

• Since the parent's height has increased, this may affect the height of the parent's parent, etc. This is also shown in the diagram. We see the red numbers uniformly 1 higher than the corresponding blue.

• So we need to start at w and proceed up the tree to find possible imbalances. Imbalance can only occur at an ancestor of w since these are the only nodes whose height's have changed by the expansion of w to an internal node at height 1.

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).
• Node x is an ancestor of w (the node where the insert occurred).

• Node x has had it height raised from k-1 to k and node y, the parent of x, has had its height raised from k to k+1.

• The height of y was one greater than its sibling (which was the blue node above). The height difference is now 2. The parent of y is z.

• Node z was at height k+1 with children at height k and k-1, but now the higher child has had its height raised so z's children have height differing by 2.

• Thus the tree is out of balance (it is no longer an AVL tree).

• In this diagram y is the right child of z and x is the left child of y. There are three other possibilities, which are also shown (but with less detail).

• We will isolate on the three nodes x y and z and their subtrees and see how to rearrange the diagram to restore balance.

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.