Which rotations to use, when?
Assume a private Node class is used for an AVL tree
class:
public class AVL<Key extends Comparable<Key>, Value>
{
// AVL members and methods
...
// inner class
private class Node
{
private Key key;
private Value val;
private Node left;
private Node right;
private int height; // optional
private Node() {}
private Node(Key k, Value v) {
key = k;
val = v;
height = 0; // optional? Not really
left = right = null;
}
}
}
Recall insertion (without rebalancing) into a Binary Search
Tree. We assume the BST has a private member root whose
value is null if the BST is empty:
private Node root;
public void put(Key k, Value v)
{
root = put(k, v, root);
}
private Node put(Key k, Value v, Node t)
{
if ( t == null ) {
t = new Node(k,v);
} else if ( k.compareTo(t.key) < 0 ) {
t.left = put(k, v, t.left);
} else if ( k.compareTo(t.key) > 0 ) {
t.right = put(k, v, t.right);
}
return t;
}
An AVL tree is a binary search tree with a balance condtition
The insertion for an AVL tree uses the same code as for any
biinary search tree (above), but must add code to keep the AVL
tree balanced and to compute changes in heights of nodes:
public void put(Key k, Value v)
{
root = put(k, v, root);
}
private Node put(Key k, Value v, Node t)
{
if ( t == null ) {
t = new Node(k, v);
} else if ( k.compareTo(t.key) < 0 ) {
t.left = put(k, v, t.left);
// If t is unbalanced call rotate method(s) to restore the balance
} else if ( k.compareTo(t.key) > 0 ) {
t.right = put(k, v, t.right);
// If t is unbalanced call rotate method(s) to restore the balance
}
// Recompute the height of t if height is member of Node
return t;
}
Which rotation should be used when a node bcomes unbalanced depends on
which child subtree (left or right) the offending value was inserted
and which subtree (left or right) of that
child.
Single rotation with P's right child R (change 2 links)

Double rotation with P's right child Q (change 4 links)

For a binary search tree (which includes AVL trees), the order
of growth of the put, get, delete, min, max, floor,
ceiling, rank, and select methods all are determined by the height
the tree.
Fact:
For an AVL tree, the height = O(log(N)), where N is the number
of keys.
This follows from the fact that if an AVL tree has height h,
the number of keys, N, can't be too small. More precisely,
N >= 2h / 2
Solving for h, this means
h / 2 <= log(N)
So
h <= 2log(N)
(See the appendix below for the details.)
The level number (or depth) of a node in a binary search
tree is the distance from the root.
Level order means to list the
keys in order by their level and for keys on the same level, list
them from left to right (that is, in increasing order).
Listing
ALL the keys in a binary search tree in their usual order doesn't
give any information about the shape of the tree. Is it balanced,
essentially a linked list, or what?
However, if the keys are listed in level order you can use this
as the INPUT order to rebuild the original tree! So instead of
drawing binary search trees they can be described by giving the
level order list of keys.
Level order of a binary search tree: 80 30 90 10 85 5 20
89 12.
What is the level order after inserting 70?
Level order of a binary search tree: 80 30 90 10 85 5 20
89 12.
What is the level order after Hibbard deletion of 5, 10 and 80?
(Hibbard deletion of a Node with 2 children replaces that Node
with its successor.
Level order of a binary search tree: 80 30 90 10 85 5 20
89 12.
If we search for key 15, what keys in the tree are compared for
this search miss?
What about a search for key 100?
Level order of a binary search tree: 80 30 90 10 85 5 20
89 12.
What is the level of key 20?
What is the height of the subtree at 20?
Level order of a binary search tree: 80 50 100 30 70 90 105 20
40 60 85 10
Is this an AVL tree?
Level order of an AVL tree: 80 50 100 30 70 90 105 20
40 60 85 10
The key 65 is inserted into this tree. Is a rotation
required?
Here is a sketch of the proof that an AVL tree with N keys has
height h = O(log(N)).
Let f(h) be the minimum number of keys in an AVL tree of height
h.
(For example, try to draw AVL trees of heights 1, 2, and 3 with
the smallest number of keys.)
f(-1) = 0
f(0) = 1
f(1) = 2
For h >= 1,

Since f(h-1) >= f(h-2), we get
| f(7) > 2f(5) |
f(8) > 2f(6) |
| f(7) > 22f(3) |
f(8) > 22f(4) |
| f(7) > 23f(1) |
f(8) > 23f(2) |
| f(7) > 232 |
f(8) > 24f(0) |
| f(7) > 24 |
f(8) > 24 |
In general,
where ceiling(x) means the smallest integer that is >= x.