# AVL trees

AVL trees solve the balancing problem by enforcing the invariant that the heights of the two subtrees sitting under each node differ by at most one. This does not guarantee perfect balance, but it does get close. Let S(k) be the size of the smallest AVL tree with height k. This tree will have at least one subtree of height k − 1, but its other subtree can be of height k − 2 (and should be, to keep it as small as possible). We thus have the recurrence S(k) = 1 + S(k − 1) + S(k − 2), which is very close to the Fibonacci recurrence.

It is possible to solve this exactly using generating functions. But we can get close by guessing that S(k) ≥ ak for some constant a. This clearly works for S(0) = a0 = 1. For larger k, compute

• S(k) = 1 + ak − 1 + ak − 2 = 1 + ak(1/a + 1/a2) > ak(1/a + 1/a2).

This last quantity is at least ak provided (1/a + 1/a2) is at least 1. We can solve exactly for the largest a that makes this work, but a very quick calculation shows that a = 3/2 works: 2/3 + 4/9 = 10/9 > 1. It follows that any AVL tree with height k has at least (3/2)k nodes, or conversely that any AVL tree with (3/2)k nodes has height at most k. So the height of an arbitrary AVL tree with n nodes is no greater than log3/2n = O(log n).

How do we maintain this invariant? The first thing to do is add extra information to the tree, so that we can tell when the invariant has been violated. AVL trees store in each node the difference between the heights of its left and right subtrees, which will be one of  − 1, 0, or 1. In an ideal world this would require log23 ≈ 1.58 bits per node, but since fractional bits are difficult to represent on modern computers a typical implementation uses two bits. Inserting a new node into an AVL tree involves

1. Doing a standard binary search tree insertion.
2. Updating the balance fields for every node on the insertion path.
3. Performing a single or double rotation to restore balance if needed.

Implementing this correctly is tricky. Intuitively, we can imagine a version of an AVL tree in which we stored the height of each node (using O(log log n) bits). When we insert a new node, only the heights of its ancestors change—so step 2 requires updating O(log n) height fields. Similarly, it is only these ancestors that can be too tall. It turns out that fixing the closest ancestor fixes all the ones above it (because it shortens their longest paths by one as well). So just one single or double rotation restores balance.

Deletions are also possible, but are uglier: a deletion in an AVL tree may require as many as O(log n) rotations. The basic idea is to use the standard binary search tree deletion trick of either splicing out a node if it has no right child, or replacing it with the minimum value in its right subtree (the node for which is spliced out); we then have to check to see if we need to rebalance at every node above whatever node we removed.

Which rotations we need to do to rebalance depends on how some pair of siblings are unbalanced. Below, we show the possible cases.

Zig-zig case: This can occur after inserting in A or deleting in C. Here we rotate A up:

    y            x
/ \   ===>   / \
x   C        A   y
/ \           |  / \
A   B          # B   C
|
#


Zig-zag case: This can occur after inserting in B or deleting in C. This requires a double rotation.

    z              z                y
/ \   ===>     / \     ===>     / \
x   C          y   C            x   z
/ \            / \              /|   |\
A   y          x  B2            A B1 B2 C
/ \        / \
B1 B2      A  B1


Zig-zag case, again: This last case comes up after deletion if both nephews of the short node are too tall. The same double rotation we used in the previous case works here, too. Note that one of the subtrees is still one taller than the others, but that’s OK.

    z              z                y
/ \   ===>     / \     ===>     / \
x   C          y   C            x   z
/ \            / \              /|   |\
A   y          x  B2            A B1 B2 C
|  / \        / \               |
# B1 B2      A  B1              #
|
#


### Sample implementation

If we are not fanatical about space optimization, we can just keep track of the heights of all nodes explicitly, instead of managing the  − 1, 0, 1 balance values. Below, we give a not-very-optimized example implementation that uses this approach to store a set of ints. This is pretty much our standard unbalanced BST (although we have to make sure that the insert and delete routines are recursive, so that we can fix things up on the way back out), with a layer on top, implemented in the treeFix function, that tracks the height and size of each subtree (although we don’t use size), and another layer on top of that, implemented in the treeBalance function, that fixes any violations of the AVL balance rule.

/*
* Basic binary search tree data structure without balancing info.
*
* Convention:
*
* Operations that update a tree are passed a struct tree **,
* so they can replace the argument with the return value.
*
* Operations that do not update the tree get a const struct tree *.
*/

#define LEFT (0)
#define RIGHT (1)
#define TREE_NUM_CHILDREN (2)

struct tree {
/* we'll make this an array so that we can make some operations symmetric */
struct tree *child[TREE_NUM_CHILDREN];
int key;
int height;    /* height of this node */
size_t size;   /* size of subtree rooted at this node */
};

#define TREE_EMPTY (0)
#define TREE_EMPTY_HEIGHT (-1)

/* free all elements of a tree, replacing it with TREE_EMPTY */
void treeDestroy(struct tree **root);

/* insert an element into a tree pointed to by root */
void treeInsert(struct tree **root, int newElement);

/* return 1 if target is in tree, 0 otherwise */
/* we allow root to be modified to allow for self-balancing trees */
int treeContains(const struct tree *root, int target);

/* delete minimum element from the tree and return its key */
/* do not call this on an empty tree */
int treeDeleteMin(struct tree **root);

/* delete target from the tree */
/* has no effect if target is not in tree */
void treeDelete(struct tree **root, int target);

/* return height of tree */
int treeHeight(const struct tree *root);

/* return size of tree */
size_t treeSize(const struct tree *root);

/* pretty-print the contents of a tree */
void treePrint(const struct tree *root);

/* return the number of elements in tree less than target */
size_t treeRank(const struct tree *root, int target);

/* return an element with the given rank */
/* rank must be less than treeSize(root) */
int treeUnrank(const struct tree *root, size_t rank);

/* check that aggregate data is correct throughout the tree */
void treeSanityCheck(const struct tree *root);


examples/trees/AVL/tree.h

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>

#include "tree.h"

int
treeHeight(const struct tree *root)
{
if(root == 0) {
return TREE_EMPTY_HEIGHT;
} else {
return root->height;
}
}

/* recompute height from height of kids */
static int
treeComputeHeight(const struct tree *root)
{
int childHeight;
int maxChildHeight;
int i;

if(root == 0) {
return TREE_EMPTY_HEIGHT;
} else {
maxChildHeight = TREE_EMPTY_HEIGHT;

for(i = 0; i < TREE_NUM_CHILDREN; i++) {
childHeight = treeHeight(root->child[i]);
if(childHeight > maxChildHeight) {
maxChildHeight = childHeight;
}
}

return maxChildHeight + 1;
}
}

size_t
treeSize(const struct tree *root)
{
if(root == 0) {
return 0;
} else {
return root->size;
}
}

/* recompute size from size of kids */
static int
treeComputeSize(const struct tree *root)
{
int size;
int i;

if(root == 0) {
return 0;
} else {
size = 1;

for(i = 0; i < TREE_NUM_CHILDREN; i++) {
size += treeSize(root->child[i]);
}

return size;
}
}

/* fix aggregate data in root */
/* assumes children are correct */
static void
treeAggregateFix(struct tree *root)
{
if(root) {
root->height = treeComputeHeight(root);
root->size = treeComputeSize(root);
}
}

/* rotate child in given direction to root */
static void
treeRotate(struct tree **root, int direction)
{
struct tree *x;
struct tree *y;
struct tree *b;

/*
*      y           x
*     / \         / \
*    x   C  <=>  A   y
*   / \             / \
*  A   B           B   C
*/

y = *root;                  assert(y);
x = y->child[direction];    assert(x);
b = x->child[!direction];

/* do the rotation */
*root = x;
x->child[!direction] = y;
y->child[direction] = b;

/* fix aggregate data for y then x */
treeAggregateFix(y);
treeAggregateFix(x);
}

/* restore AVL property at *root after an insertion or deletion */
/* assumes subtrees already have AVL property */
static void
treeRebalance(struct tree **root)
{
int tallerChild;

if(*root) {
for(tallerChild = 0; tallerChild < TREE_NUM_CHILDREN; tallerChild++) {
if(treeHeight((*root)->child[tallerChild]) >= treeHeight((*root)->child[!tallerChild]) + 2) {

/* which grandchild is the problem? */
if(treeHeight((*root)->child[tallerChild]->child[!tallerChild])
> treeHeight((*root)->child[tallerChild]->child[tallerChild])) {
/* opposite-direction grandchild is too tall */
/* rotation at root will just change its parent but not change height */
/* so we rotate it up first */
treeRotate(&(*root)->child[tallerChild], !tallerChild);
}

/* now rotate up the taller child */
treeRotate(root, tallerChild);

/* don't bother with other child */
break;
}
}

/* test that we actually fixed it */
assert(abs(treeHeight((*root)->child[LEFT]) - treeHeight((*root)->child[RIGHT])) <= 1);

#ifdef PARANOID_REBALANCE
treeSanityCheck(*root);
#endif
}
}

/* free all elements of a tree, replacing it with TREE_EMPTY */
void
treeDestroy(struct tree **root)
{
int i;

if(*root) {
for(i = 0; i < TREE_NUM_CHILDREN; i++) {
treeDestroy(&(*root)->child[i]);
}
free(*root);
*root = TREE_EMPTY;
}
}

/* insert an element into a tree pointed to by root */
void
treeInsert(struct tree **root, int newElement)
{
struct tree *e;

if(*root == 0) {
/* not already there, put it in */

e = malloc(sizeof(*e));
assert(e);

e->key = newElement;
e->child[LEFT] = e->child[RIGHT] = 0;

*root = e;
} else if((*root)->key == newElement) {
/* already there, do nothing */
return;
} else {
/* do this recursively so we can fix data on the way back out */
treeInsert(&(*root)->child[(*root)->key < newElement], newElement);
}

/* fix the aggregate data */
treeAggregateFix(*root);
treeRebalance(root);
}

/* return 1 if target is in tree, 0 otherwise */
int
treeContains(const struct tree *t, int target)
{
while(t && t->key != target) {
t = t->child[t->key < target];
}

return t != 0;
}

/* delete minimum element from the tree and return its key */
/* do not call this on an empty tree */
int
treeDeleteMin(struct tree **root)
{
struct tree *toFree;
int retval;

assert(*root);  /* can't delete min from empty tree */

if((*root)->child[LEFT]) {
/* recurse on left subtree */
retval = treeDeleteMin(&(*root)->child[LEFT]);
} else {
/* delete the root */
toFree = *root;
retval = toFree->key;
*root = toFree->child[RIGHT];
free(toFree);
}

/* fix the aggregate data */
treeAggregateFix(*root);
treeRebalance(root);

return retval;
}

/* delete target from the tree */
/* has no effect if target is not in tree */
void
treeDelete(struct tree **root, int target)
{
struct tree *toFree;

/* do nothing if target not in tree */
if(*root) {
if((*root)->key == target) {
if((*root)->child[RIGHT]) {
/* replace root with min value in right subtree */
(*root)->key = treeDeleteMin(&(*root)->child[RIGHT]);
} else {
/* patch out root */
toFree = *root;
*root = toFree->child[LEFT];
free(toFree);
}
} else {
treeDelete(&(*root)->child[(*root)->key < target], target);
}

/* fix the aggregate data */
treeAggregateFix(*root);
treeRebalance(root);
}
}

/* how far to indent each level of the tree */
#define INDENTATION_LEVEL (2)

/* print contents of a tree, indented by depth */
static void
treePrintIndented(const struct tree *root, int depth)
{
int i;

if(root != 0) {
treePrintIndented(root->child[LEFT], depth+1);

for(i = 0; i < INDENTATION_LEVEL*depth; i++) {
putchar(' ');
}
printf("%d Height: %d Size: %zu (%p)\n", root->key, root->height, root->size, (void *) root);

treePrintIndented(root->child[RIGHT], depth+1);
}
}

/* print the contents of a tree */
void
treePrint(const struct tree *root)
{
treePrintIndented(root, 0);
}

size_t
treeRank(const struct tree *t, int target)
{
size_t rank = 0;

while(t && t->key != target) {
if(t->key < target) {
/* go right */
/* root and left subtree are all less than target */
rank += (1 + treeSize(t->child[LEFT]));
t = t->child[RIGHT];
} else {
/* go left */
t = t->child[LEFT];
}
}

/* we must also count left subtree */
return rank + treeSize(t->child[LEFT]);
}

int
treeUnrank(const struct tree *t, size_t rank)
{
size_t leftSize;

/* basic idea: if rank < treeSize(child[LEFT]), recurse in left child */
/* if it's equal, return the root */
/* else recurse in right child with rank = rank - treeSize(child[LEFT]) - 1 */
while(rank != (leftSize = treeSize(t->child[LEFT]))) {
if(rank < leftSize) {
t = t->child[LEFT];
} else {
t = t->child[RIGHT];
rank -= (leftSize + 1);
}
}

return t->key;
}

/* check that aggregate data is correct throughout the tree */
void
treeSanityCheck(const struct tree *root)
{
int i;

if(root) {
assert(root->height == treeComputeHeight(root));
assert(root->size == treeComputeSize(root));

assert(abs(treeHeight(root->child[LEFT]) - treeHeight(root->child[RIGHT])) < 2);

for(i = 0; i < TREE_NUM_CHILDREN; i++) {
treeSanityCheck(root->child[i]);
}
}
}

#ifdef TEST_MAIN
int
main(int argc, char **argv)
{
int key;
int i;
const int n = 10;
const int randRange = 1000;
const int randTrials = 10000;
struct tree *root = TREE_EMPTY;

if(argc != 1) {
fprintf(stderr, "Usage: %s\n", argv);
return 1;
}

/* original test */
for(i = 0; i < n; i++) {
assert(!treeContains(root, i));
treeInsert(&root, i);
assert(treeContains(root, i));
treeSanityCheck(root);
#ifdef PRINT_AFTER_OPERATIONS
treePrint(root);
puts("---");
#endif
}

/* check ranks */
for(i = 0; i < n; i++) {
assert(treeRank(root, i) == i);
assert(treeUnrank(root, i) == i);
}

treeSanityCheck(root);

/* now delete everything */
for(i = 0; i < n; i++) {
assert(treeContains(root, i));
treeDelete(&root, i);
assert(!treeContains(root, i));
treeSanityCheck(root);
#ifdef PRINT_AFTER_OPERATIONS
treePrint(root);
puts("---");
#endif
}

treeSanityCheck(root);
treeDestroy(&root);

/* random test */
srand(1);

for(i = 0; i < randTrials; i++) {
treeInsert(&root, rand() % randRange);
treeDelete(&root, rand() % randRange);
}

treeSanityCheck(root);
treeDestroy(&root);

#ifdef TEST_USE_STDIN
while(scanf("%d", &key) == 1) {
/* insert if positive, delete if negative */
if(key > 0) {
treeInsert(&root, key);
assert(treeContains(root, key));
} else if(key < 0) {
treeDelete(&root, -key);
assert(!treeContains(root, key));
}
/* else ignore 0 */

#ifdef PRINT_AFTER_OPERATIONS
treePrint(root);
puts("---");
#endif
}

treeSanityCheck(root);

treeDestroy(&root);
#endif /* TEST_USE_STDIN */
return 0;
}
#endif /* TEST_MAIN */


examples/trees/AVL/tree.c

This Makefile will compile and run some demo code in tree.c if run with make test.

(An older implementation can be found in the directory examples/trees/oldAvlTree.)