This document describes details of the AVL tree implementation. An AVL 
tree is used in the region mapper to find the region an address belongs
to in case of a pagefault. A general description of the algorithms to
insert and remove elements to AVL trees can be found in [1] Donald Knuth:
"The Art of Computer Programming (Vol. 3)" and in [2] T. Ottmann,
P. Widmayer: "Algorithmen und Datenstrukturen"

  +---+     single tree node, x is the pointer variable in the insert/
  | x | y   remove function, y the balance factor (height right
  +---+     subtree - height left subtree)  

    +
   / \
  / x \     subtree x with a height of y
 +-----+
    y

Insert
======

These four situations can happen if a new node is inserted into a AVL
tree:

1.) Single rotation left:
    Subtree 3 has grown because of insert.

            +---+                                  +---+
            | t |                                  | t |
            +---+                                  +---+
              |                                      |
            +---+                                  +---+
            | s | +2                               | r | 0
            +---+                                  +---+
           /     \                                /     \
          /       \                              /       \
         +       +---+                        +---+       \
        / \      | r | +1                     | s | 0      \
       / 1 \     +---+            --->        +---+         \
      +-----+   /     \                      /     \         \
        h-1    /       \                    /       \         +
              +         +                  +         +       / \
             / \       / \                / \       / \     /   \
            / 2 \     /   \              / 1 \     / 2 \   /  3  \
           +-----+   /  3  \            +-----+   +-----+ +-------+
             h-1    +-------+             h-1       h-1       h 
                        h

2.) Single rotation right:
    Subtree 1 has grown because of insert.

                  +---+                             +---+
                  | t |                             | t |
                  +---+                             +---+
                    |                                 |
                  +---+                             +---+
                  | s | -2                          | r | 0
                  +---+                             +---+
                 /     \                           /     \
                /       \                         /       \
             +---+       +                       /       +---+
             | r | -1   / \                     /        | s | 0
             +---+     / 3 \      --->         /         +---+
            /     \   +-----+                 /         /     \
           /       \    h-1                  +         /       \
          +         +                       / \       +         +
         / \       / \                     /   \     / \       / \
        /   \     / 2 \                   /  1  \   / 2 \     / 3 \
       /  1  \   +-----+                 +-------+ +-----+   +-----+
      +-------+    h-1                       h       h-1       h-1
          h

3.) Double rotation left-right:
    Subtree 2 (1) or 3 (2) has grown because of insert.

                                 +---+
                                 | t |
                                 +---+
    s can be the right or the      |  
    left subtree of t            +---+
                                 | s | -2
                                 +---+
                                /     \
                               /       \
                              /         \
                           +---+         +
                           | r | +1     / \
                           +---+       / 4 \
                          /     \     +-----+
                         /       \      h-1
                        /         \
                       +        +---+
                      / \       | p | +1/-1
                     / 1 \      +---+
                    +-----+    /     \
                      h-1     /       \
                             +         +
                            / \       / \
                           / 2 \     / 3 \
                          +-----+   +-----+
                        (1) h-1       h-2
                        (2) h-2       h-1

                                   |
                                   |  left rotate p/r
                                   v

                                 +---+
                                 | t |
                                 +---+
                                   |
                                 +---+
                                 | s | -2
                                 +---+
                                /     \
                               /       \
                              /         \
                           +---+         +
                           | p | -2/-1  / \
                           +---+       / 4 \
                          /     \     +-----+
                         /       \      h-1
                        /         \
                     +---+         +
                     | r | 0/+1   / \                                
                     +---+       / 3 \
                    /     \     +-----+
                   /       \      h-2
                  +         +     h-1
                 / \       / \
                / 1 \     / 2 \
               +-----+   +-----+
                 h-1       h-1
                           h-2
                                   |
                                   |  right rotate p/s
                                   v

                                 +---+
                                 | t |
                                 +---+
                                   |
                                 +---+
                                 | p | 0
                                 +---+
                                /     \
                               /       \
                              /         \
                         +---+           +---+
                         | r | 0/-1      | s | +1/0
                         +---+           +---+
                        /     \         /     \ 
                       +       +       +       +
                      / \     / \     / \     / \
                     / 1 \   / 2 \   / 3 \   / 4 \
                    +-----+ +-----+ +-----+ +-----+
                      h-1     h-1     h-2     h-1
                              h-2     h-1

4.) Double rotation right-left:
    Subtree 2 (1) or 3 (2) has grown because of insert.

                                 +---+
                                 | t |
                                 +---+
    s can be the right or the      |
    left subtree of t            +---+
                                 | s | +2
                                 +---+
                                /     \
                               /       \
                              /         \
                             +         +---+
                            / \        | r | -1
                           / 1 \       +---+
                          +-----+     /     \
                            h-1      /       \
                                    /         \
                                 +---+         +
                                 | p | -1/+1  / \
                                 +---+       / 4 \
                                /     \     +-----+
                               /       \      h-1
                              +         +
                             / \       / \
                            / 2 \     / 3 \
                           +-----+   +-----+
                         (1) h-1       h-2
                         (2) h-2       h-1

                                   |
                                   |  right rotate p/r
                                   v

                                 +---+
                                 | t |
                                 +---+
                                   |
                                 +---+
                                 | s | +2
                                 +---+
                                /     \
                               /       \
                              /         \
                             +         +---+
                            / \        | p | +1/+2
                           / 1 \       +---+
                          +-----+     /     \
                            h-1      /       \
                                    /         \
                                   +         +---+
                                  / \        | r | +1/0
                                 / 2 \       +---+
                                +-----+     /     \
                                  h-1      /       \
                                  h-2     +         +
                                         / \       / \
                                        / 3 \     / 4 \
                                       +-----+   +-----+
                                         h-2       h-1
                                         h-1

                                   |
                                   |  left rotate p/s
                                   v

                                 +---+
                                 | t |
                                 +---+
                                   |
                                 +---+
                                 | p | 0
                                 +---+
                                /     \
                               /       \
                              /         \
                         +---+           +---+
                         | s | 0/-1      | r | +1/0
                         +---+           +---+
                        /     \         /     \
                       +       +       +       +
                      / \     / \     / \     / \
                     / 1 \   / 2 \   / 3 \   / 4 \
                    +-----+ +-----+ +-----+ +-----+
                      h-1     h-1     h-2     h-1
                              h-2     h-1

Remove
======

Removing an element from an AVL tree is a bit more difficult:

1.) Find the element to remove (q) in the tree. These three situations
    are possible: 

  a.) Both subtrees of q are NULL:

             +---+               +---+
             | p | b             | p | b +/- 1
             +---+               +---+
               |       -->         |  
             +---+               NULL
             | q |
             +---+
            /     \
           /       \
         NULL     NULL

      If the new balance factor of p got 0 (=> the other subtree of p 
      was already NULL) we must rebalance the tree.


  b.) Only one subtree of q is NULL. 
      Note: both subtrees of the valid subtree of q must be NULL
            (otherwise the tree would not be an AVL tree)


             +---+               +---+
             | p |               | p | b +/- 1
             +---+               +---+
               |      -->          |
             +---+               +---+
             | q |               | r |
             +---+               +---+
            /     \             /     \  
           /       \           /       \
         NULL    +---+       NULL     NULL
                 | r |
                 +---+
                /     \
               /       \
             NULL     NULL


  c.) Both subtrees of q are not NULL. In this case we replace q with
      the element r with the largest key in the left subtree (the so
      called 'symmetric predecessor' of q). Only the cases a.) or b.)
      can happen when removing r.

2.) Adjust balance factors and rebalance the tree. 

    These situations can happen [2]:
   
    a.) Left subtree of a node got shorter (balance -> balance + 1) 

    a.1.)
             +---+   adjust      +---+             
             | t |   balance     | t |           Node p got balanced but   
             +---+   factor      +---+           its overall height 
               |      -->          |             decreased by one
             +---+               +---+           => must adjust and  
             | p | -1            | p | 0            rebalance t
             +---+               +---+ 
            /     \             /     \
           /       \           /       \
          +         +         +         +
         / \       / \       / \       / \
        / 1 \     / 2 \     / 1 \     / 2 \
       +-----+   +-----+   +-----+   +-----+
         h-1       h-1       h-1       h-1
	
    a.2.) 
             +---+   adjust      +---+             
             | t |   balance     | t |          Node p got out of
             +---+   factor      +---+          balance, but kept its  
               |      -->          |            height 
             +---+               +---+          => done
             | p | 0             | p | +1       
             +---+               +---+ 
            /     \             /     \
           /       \           /       \
          +         +         +         +
         / \       / \       / \       / \
        / 1 \     / 2 \     / 1 \     / 2 \
       +-----+   +-----+   +-----+   +-----+
         h-1        h        h-1        h

    a.3.)
             +---+
             | t |
             +---+
               | 
             +---+         The left subtree already was the shorter subtree     
             | p | +2      of node p. We need to rebalance the tree.
             +---+
            /     \
           /       \
          +         +
         / \       / \
        / 1 \     / 2 \
       +-----+   +-----+
         h-2        h

    Three different cases for rebalancing are possible (depending on
    the balance factor of the root node of the right subtree):

    a.3.1) r->b == 0
                                    +---+
                                    | t |
                                    +---+
           Note: the balance of q     |
           must be 0, otherwise     +---+
           the rebalancing would    | p | +2
           have stopped earlier     +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +---+
                            | q | 0         | r | 0
                            +---+           +---+
                           /     \         /     \
                          +       +       +       +
                         / \     / \     / \     / \
                        / 1 \   / 2 \   / 3 \   / 4 \
                       +-----+ +-----+ +-----+ +-----+
                         h-1     h-1     h+1     h+1
                                      
                                      |
                                      | left rotate r/p
                                      v

                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | r | -1
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +
                            | p | +1       / \
                            +---+         / 4 \
                           /     \       +-----+
                          /       \        h+1
                     +---+         +
                     | q | 0      / \
                     +---+       / 3 \
                    /     \     +-----+
                   /       \      h+1
                  +         +
                 / \       / \
                / 1 \     / 2 \
               +-----+   +-----+
                 h-1       h-1
          
           Done, the subtree of t is an AVL tree and kept its height
           (h+3).
 
    a.3.2) r->b == +1
                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | p | +2
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +---+
                            | q | 0         | r | +1
                            +---+           +---+
                           /     \         /     \
                          +       +       +       +
                         / \     / \     / \     / \
                        / 1 \   / 2 \   / 3 \   / 4 \
                       +-----+ +-----+ +-----+ +-----+
                         h-1     h-1      h      h+1
                                      
                                      |
                                      | left rotate r/p
                                      v

                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | r | 0
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +
                            | p | 0        / \
                            +---+         / 4 \
                           /     \       +-----+
                          /       \        h+1
                     +---+         +
                     | q | 0      / \
                     +---+       / 3 \ 
                    /     \     +-----+
                   /       \       h
                  +         +
                 / \       / \
                / 1 \     / 2 \
               +-----+   +-----+
                 h-1       h-1

           The subtree of t is an AVL tree again, but its height
           descreased by one (h+3 -> h+2), we must adjust and 
           rebalance t.

    a.3.3) r->b == -1
                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | p | +2
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +---+
                            | q | 0         | r | -1
                            +---+           +---+
                           /     \         /     \
                          +       +       /       + 
                         / \     / \    +---+    / \
                        / 1 \   / 2 \   | o |   / 5 \
                       +-----+ +-----+  +---+  +-----+
                         h-1     h-1   /     \    h    
                                      +       +
                                     / \     / \
                                    / 3 \   / 4 \
                                   +-----+ +-----+
                                      h       h   
                                      h      h-1
                                     h-1      h

                                      |
                                      | right rotate o/r
                                      v

                                    +---+
                                    | t |
                                    +---+
                                      | 
                                    +---+
                                    | p | +2
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +---+
                            | q | 0         | o | +1/+2
                            +---+           +---+
                           /     \         /     \
                          +       +       +       \  
                         / \     / \     / \     +---+
                        / 1 \   / 2 \   / 3 \    | r | +1/0
                       +-----+ +-----+ +-----+   +---+
                         h-1     h-1    h/h-1   /     \
                                               +       +
                                              / \     / \
                                             / 4 \   / 5 \
                                            +-----+ +-----+
                                             h-1/h     h

                                      |
                                      | left rotate p/o 
                                      v

                                    +---+
                                    | t |
                                    +---+
                                      | 
                                    +---+
                                    | o | 0
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +---+
                            | p | 0/-1      | r | +1/0
                            +---+           +---+
                           /     \         /     \
                          /       +       +       +
                       +---+     / \     / \     / \
                       | q | 0  / 3 \   / 4 \   / 5 \
                       +---+   +-----+ +-----+ +-----+
                      /     \   h/h-1   h-1/h     h
                     +       +
                    / \     / \
                   / 1 \   / 2 \
                  +-----+ +-----+
                    h-1     h-1

           New balance factors:
           
           o->b == 0:
             o->b = 0;
             p->b = 0;
             r->b = 0;

           o->b == -1
             o->b = 0;
             p->b = 0;
             r->b = +1;

           o->b == +1
             o->b = 0;
             p->b = -1;
             r->b = 0;             

           The subtree of t is an AVL tree again, but its height
           descreased by one (h+3 -> h+2), we must adjust and 
           rebalance t.

    b.) Right subtree of a node got shorter (balance -> balance - 1)

    b.1.)
             +---+   adjust      +---+             
             | t |   balance     | t |           Node p got balanced but   
             +---+   factor      +---+           its overall height 
               |      -->          |             decreased by one
             +---+               +---+           => must adjust and  
             | p | +1            | p | 0            rebalance t
             +---+               +---+ 
            /     \             /     \
           /       \           /       \
          +         +         +         +
         / \       / \       / \       / \
        / 1 \     / 2 \     / 1 \     / 2 \
       +-----+   +-----+   +-----+   +-----+
         h-1       h-1       h-1       h-1
	
    b.2.) 
             +---+   adjust      +---+             
             | t |   balance     | t |          Node p got out of
             +---+   factor      +---+          balance, but kept its  
               |      -->          |            height 
             +---+               +---+          => done
             | p | 0             | p | -1     
             +---+               +---+ 
            /     \             /     \
           /       \           /       \
          +         +         +         +
         / \       / \       / \       / \
        / 1 \     / 2 \     / 1 \     / 2 \
       +-----+   +-----+   +-----+   +-----+
          h        h-1        h        h-1

    b.3.) 
             +---+
             | t | 
             +---+
               |
             +---+         The right subtree already was the shorter subtree
             | p | -2      of node p. We need to rebalance the tree.
             +---+
            /     \
           /       \
          +         +
         / \       / \
        / 1 \     / 2 \
       +-----+   +-----+
          h        h-2 

    Three different cases for rebalancing are possible (depending on
    the balance factor of the root node of the left subtree):

    b.3.1) r->b == 0
                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | p | -2
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +---+
                            | r | 0         | q | 0
                            +---+           +---+
                           /     \         /     \
                          +       +       +       +
                         / \     / \     / \     / \
                        / 1 \   / 2 \   / 3 \   / 4 \
                       +-----+ +-----+ +-----+ +-----+
                         h+1     h+1     h-1     h-1

                                      |
                                      | right rotate r/p
                                      v

                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | r | +1
                                    +---+
                                   /     \
                                  /       \
                                 +         \
                                / \       +---+
                               / 1 \      | p | -1
                              +-----+     +---+
                                h+1      /     \
                                        +       \   
                                       / \     +---+ 
                                      / 2 \    | q | 0
                                     +-----+   +---+
                                       h+1    /     \
                                             +       +
                                            / \     / \
                                           / 3 \   / 4 \
                                          +-----+ +-----+
                                            h-1     h-1

           Done, the subtree of t is an AVL tree and kept its height
           (h+3).

    b.3.2) r->b == -1
                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | p | -2
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +---+
                            | r | -1        | q | 0
                            +---+           +---+
                           /     \         /     \
                          +       +       +       +
                         / \     / \     / \     / \
                        / 1 \   / 2 \   / 3 \   / 4 \
                       +-----+ +-----+ +-----+ +-----+
                         h+1      h      h-1     h-1

                                      |
                                      | right rotate r/p
                                      v

                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | r | 0 
                                    +---+
                                   /     \
                                  /       \
                                 +         \
                                / \       +---+
                               / 1 \      | p | 0 
                              +-----+     +---+
                                h+1      /     \
                                        +       \   
                                       / \     +---+ 
                                      / 2 \    | q | 0
                                     +-----+   +---+
                                        h     /     \
                                             +       +
                                            / \     / \
                                           / 3 \   / 4 \
                                          +-----+ +-----+
                                            h-1     h-1

           The subtree of t is an AVL tree again, but its height
           descreased by one (h+3 -> h+2), we must adjust and 
           rebalance t.

    b.3.3) r->b == +1
                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | p | -2
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +---+
                            | r | +1        | q | 0
                            +---+           +---+
                           /     \         /     \
                          +       \       +       +
                         / \    +---+    / \     / \
                        / 1 \   | o |   / 4 \   / 5 \
                       +-----+  +---+  +-----+ +-----+
                          h    /     \   h-1     h-1
                              +       +
                             / \     / \
                            / 2 \   / 3 \
                           +-----+ +-----+
                              h       h
                              h      h-1 
                             h-1      h

                                      |
                                      | left rotate o/r
                                      v

                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | p | -2
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +---+
                            | o | -2/-1     | q | 0
                            +---+           +---+
                           /     \         /     \
                          /       +       +       +
                       +---+     / \     / \     / \
                  0/-1 | r |    / 3 \   / 4 \   / 5 \
                       +---+   +-----+ +-----+ +-----+
                      /     \   h-1/h    h-1     h-1
                     +       +
                    / \     / \
                   / 1 \   / 2 \
                  +-----+ +-----+
                     h     h/h-1

                                      |
                                      | right rotate o/p
                                      v

                                    +---+
                                    | t |
                                    +---+
                                      |
                                    +---+
                                    | o | 0
                                    +---+
                                   /     \
                                  /       \
                                 /         \
                            +---+           +---+
                            | r | 0/-1      | p | +1/0
                            +---+           +---+
                           /     \         /     \
                          +       +       +       \  
                         / \     / \     / \     +---+
                        / 1 \   / 2 \   / 3 \    | q | 0
                       +-----+ +-----+ +-----+   +---+
                          h     h/h-1   h-1/h   /     \
                                               +       +
                                              / \     / \
                                             / 4 \   / 5 \
                                            +-----+ +-----+
                                              h-1     h-1

           New balance factors:

           o->b == 0:
             o->b = 0;
             r->b = 0;
             p->b = 0;

           o->b == -1:
             o->b = 0;
             r->b = 0;
             p->b = +1;

           o->b == +1:
             o->b = 0;
             r->b = -1;
             p->b = 0;

           The subtree of t is an AVL tree again, but its height
           descreased by one (h+3 -> h+2), we must adjust and 
           rebalance t.

    Rebalancing of the parent node stops at least if we found a node
    with a balance factor 0 (see a.2. resp. b.2.). We can use this to
    avoid the recursive procedure described in [2] by storing the last 
    ('deepest') node with balance 0 on the way down to the node to
    remove and use this repeatedly as start node to rebalance the
    parent of the removed node, then to rebalance the parent of the
    parent of the removed node and so on.
