AVL Trees: Tutorial and C++ Implementation

 

Copyright © 1989-1997 by Brad Appleton, All rights reserved.


The following discussion is part of a freely available public domain AVL
tree library written in C++. The full C++ source code distribution may
be found in AvlTrees.tar.gz (21KB, gzipped tar file).
[a plain old K&R C version is available in libavl.tar.gz (25KB, gzipped tar file)]


AVL Trees
An AVL tree is a special type of binary tree that is always "partially" balanced. The criteria that is used to determine the "level" of "balanced-ness" is the difference between the heights of subtrees of a root in the tree. The "height" of tree is the "number of levels" in the tree. Or to be more formal, the height of a tree is defined as follows:
  1. The height of a tree with no elements is 0
  2. The height of a tree with 1 element is 1
  3. The height of a tree with > 1 element is equal to 1 + the height of its tallest subtree.

An AVL tree is a binary tree in which the difference between the height of the right and left subtrees (or the root node) is never more than one.

The idea behind maintaining the "AVL-ness" of an AVL tree is that whenever we insert or delete an item, if we have "violated" the "AVL-ness" of the tree in anyway, we must then restore it by performing a set of manipulations (called "rotations") on the tree. These rotations come in two flavors: single rotations and double rotations (and each flavor has its corresponding "left" and "right" versions).

An example of a single rotation is as follows: Suppose I have a tree that looks like this:

Now I insert the item "a" and get the resulting binary tree:

Now, this resulting tree violates the "AVL criteria", the left subtree has a height of 2 but the right subtree has a height of 0 so the difference in the two heights is "2" (which is greater than 1). SO what we do is perform a "single rotation" (or RR for a single right rotation, or LL for a single left rotation) on the tree (by rotating the "c" element down clockwise to the right) to transform it into the following tree:

This tree is now balanced.

An example of a "double rotation" (or RL for a double right rotation, or LR for a double left rotation) is the following: Suppose I have a tree that looks like this:

Now I insert the item "b" and get the resulting binary tree:

This resulting tree also violates the "AVL criteria" so we fix it by first rotating "c" down to the right (so we get "a-b-c"), and then rotating "a" down to the left so that the tree is transformed into this:

In order to detect when a "violation" of the AVL criteria occurs we need to have each node keep track of the difference in height between its right and left subtrees. We call this "difference" the "balance" factor and define it to be the height of the right subtree minus the height of the left subtree of a tree. So as long as the "balance" factor of each node is never >1 or <-1 we have an AVL tree. As soon as the balance factor of a node becomes 2 (or -2) we need to perform one or more rotations to ensure that the resultant tree satisfies the AVL criteria.

Implementing AVL Trees in C++
Before we begin our AVL tree implementation in C++, lets assume we have a template class named "Comparable" defined as follows:

   1:   // cmp_t is an enumeration type indicating the result of a
   2:     // comparison.
   3:     enum  cmp_t {
   4:        MIN_CMP = -1,   // less than
   5:        EQ_CMP  = 0,    // equal to
   6:        MAX_CMP = 1     // greater than
   7:     };
   8:   
   9:     // Class "Comparable" corresponds to an arbitrary comparable element
  10:     // with a keyfield that has an ordering relation. The template parameter
  11:     // KeyType is the "type" of the keyfield
  12:     //
  13:     template <class KeyType>
  14:     class Comparable {
  15:     private:
  16:        KeyType  myKey;
  17:   
  18:     public:
  19:        Comparable(KeyType  key) : myKey(key) {};
  20:   
  21:        // Use default copy-ctor, assignment, & destructor
  22:   
  23:           // Compare this item against the given key & return the result
  24:        cmp_t Compare(KeyType  key) const;
  25:   
  26:          // Get the key-field of an item
  27:        KeyType Key() const { return  myKey; }
  28:     };

Like the "Comparable" class, our AVL tree will also be a template class parameterized by a KeyType:

   1:     // Class AvlNode represents a node in an AVL tree. The template parameter
   2:     // KeyType is the "type" of the keyfield
   3:     //
   4:     template <class KeyType>
   5:     class AvlNode {
   6:     private:
   7:        Comparable<KeyType> * myData;         // Data field
   8:        AvlNode<KeyType>    * mySubtree[2];   // Subtree pointers
   9:        short                 myBal;          // Balance factor
  10:   
  11:        // ... many details omitted
  12:     };
Calculating New Balances After a Rotation
To calculate the new balances after a single left rotation; assume we have the following case:

The left is what the tree looked like BEFORE the rotation and the right is what the tree looks like after the rotation. Capital letters are used to denote single nodes and lowercase letters are used to denote subtrees.

The "balance" of a tree is the height of its right subtree less the height of its left subtree. Therefore, we can calculate the new balances of "A" and "B" as follows (ht is the height function):

subtracting the second equation from the first yields:

canceling out the ht(a) terms and adding OldBal(A) to both sides yields:

Noting that max(x, y) - z = max(x-z, y-z), we get:

But ht(c) - ht(b) is OldBal(B) so we get:

Thus, for A, we get the equation:

To calculate the Balance for B we perform a similar computation:

subtracting the second equation from the first yields:

canceling, and adding OldBal(B) to both sides gives:

But ht(a) - ht(b) is - (ht(b) - ht(a)) = -NewBal(A), so ...

Using the fact that min(x,y) = -max(-x, -y) we get:

So, for a single left rotation we have shown the the new balances for the nodes A and B are given by the following equations:

   1:     NewBal(A) = OldBal(A) - 1 - max(OldBal(B), 0)
   2:     NewBal(B) = OldBal(B) - 1 + min(NewBal(A), 0)

Now let us look at the case of a single right rotation. The case we will use is the same one we used for the single left rotation only with all the left and right subtrees switched around so that we have the mirror image of the case we used for our left rotation.

If we perform the same calculations that we made for the left rotation, we will see that the new balances for a single right rotation are given by the following equations:

   1:     NewBal(A) = OldBal(A) + 1 - min(OldBal(B), 0)
   2:     NewBal(B) = OldBal(B) + 1 + max(NewBal(A), 0)

Hence, C++ code for single left and right rotations would be:

   1:     // Indices into a subtree array
   2:     enum  dir_t { LEFT = 0, RIGHT = 1 };
   3:   
   4:        // Return the minumum of two numbers
   5:     inline int
   6:     MIN(int a, int b) { return  (a < b) ? a : b; }
   7:   
   8:        // Return the maximum of two numbers
   9:     inline int
  10:     MAX(int a, int b) { return  (a > b) ? a : b; }
  11:   
  12:        // Note that RotateLeft and RotateRight are *static* member
  13:        // functions because otherwise they would have to re-assign
  14:        // to the "this" pointer.
  15:   
  16:     template <class KeyType>
  17:     void
  18:     AvlNode<KeyType>::RotateLeft(AvlNode<KeyType> * & root) {
  19:       AvlNode<KeyType> * oldRoot = root;
  20:   
  21:             // perform rotation
  22:       root = root->mySubtree[RIGHT];
  23:       oldRoot->mySubtree[RIGHT] = root->mySubtree[LEFT];
  24:       root->mySubtree[LEFT] = oldRoot;
  25:   
  26:             // update balances
  27:       oldRoot->myBal -=  (1 + MAX(root->myBal, 0));
  28:       root->myBal    -=  (1 - MIN(oldRoot->myBal, 0));
  29:     }
  30:   
  31:   
  32:     template <class KeyType>
  33:     void
  34:     AvlNode<KeyType>::RotateRight(AvlNode<KeyType> * & root) {
  35:       AvlNode<KeyType> * oldRoot = root;
  36:   
  37:             // perform rotation
  38:       root = root->mySubtree[LEFT];
  39:       oldRoot->mySubtree[LEFT] = root->mySubtree[RIGHT];
  40:       root->mySubtree[RIGHT] = oldRoot;
  41:   
  42:             // update balances
  43:       oldRoot->myBal +=  (1 - MIN(root->myBal, 0));
  44:       root->myBal    +=  (1 + MAX(oldRoot->myBal, 0));
  45:     }

We can make this code more compact however by using only ONE rotate method which takes an additional parameter: the direction in which to rotate. Notice that I have defined LEFT, and RIGHT to be mnemonic constants to index into an array of subtrees. I can pass the constant LEFT or RIGHT to the rotation method and it can calculate the direction opposite the given direction by subtracting the given direction from the number one.

It does not matter whether LEFT is 0 or RIGHT is 0 as long as one of them is 0 and the other is 1. If this is the case, then:

and

Using this and the same type definitions as before (and the same inline functions), the C++ code for a single rotation becomes:

   1:     inline  dir_t
   2:     Opposite(dir_t dir) { return dir_t(1 - int(dir)); }
   3:   
   4:     // RotateOnce -- static member function that performs a single
   5:     //               rotation for the given direction.
   6:     //
   7:     template <class KeyType>
   8:     void
   9:     AvlNode<KeyType>::RotateOnce(AvlNode<KeyType> * & root, dir_t  dir) {
  10:       AvlNode<KeyType> * oldRoot  = root;
  11:       dir_t              otherDir = Opposite(dir);
  12:   
  13:             // rotate
  14:       root = tree->mySubtree[otherDir];
  15:       oldRoot->mySubtree[otherDir] = tree->mySubtree[dir];
  16:       root->mySubtree[dir] = oldRoot;
  17:   
  18:             // update balances
  19:       if (dir == LEFT)  {
  20:          oldRoot->myBal -=  (1 + MAX(root->myBal, 0));
  21:          root->myBal    -=  (1 - MIN(oldRoot->myBal, 0));
  22:       } else  /* dir == RIGHT */  {
  23:          oldRoot->myBal +=  (1 - MIN(root->myBal, 0) );
  24:          root->myBal    +=  (1 + MAX(oldRoot->myBal, 0));
  25:       } //else
  26:     }

We can compact this code even further if we play around with the equations for updating the balances. Let us use the fact that max(x,y) = -min(-x,-y):

for a left rotation

   1:  oldRoot->myBal -=  (1 + MAX(tree->myBal, 0));
   2:  tree->myBal    -=  (1 - MIN(oldRoot->myBal, 0));

for a right rotation

   1:  oldRoot->myBal +=  (1 - MIN(tree->myBal, 0));
   2:  tree->myBal    +=  (1 + MAX(oldRoot->myBal, 0));

Using the above rule to change all occurrences of "MIN" to "MAX" these equations become:

for a left rotation

   1:  oldRoot->myBal -=  (1 + MAX( +(tree->myBal), 0));
   2:  tree->myBal    -=  (1 + MAX( -(oldRoot->myBal), 0));

for a right rotation

   1:  oldRoot->myBal +=  (1 + MAX( -(tree->myBal), 0));
   2:  tree->myBal    +=  (1 + MAX( +(oldRoot->myBal), 0));

Note that the difference between updating the balances for our right and left rotations is only the occurrence of a '+' where we would like to see a '-' in the assignment operator, and the sign of the first argument to the MAX function. If we had a function that would map LEFT to +1 and RIGHT to -1 we could multiply by the result of that function to update our balances. Such a function is

"f" maps 0 to 1 and maps 1 to -1. This function will not map LEFT and RIGHT to the same value regardless of which is 1 and which is 0 however. If we wish our function to have this property then we can multiply (1 - 2x) by (RIGHT - LEFT) so that the result "switches" signs accordingly depending upon whether LEFT is 0 or RIGHT is 0. This defines a new function "g":

If LEFT = 0 and RIGHT = 1 then:

If LEFT = 1 and RIGHT = 0 then:

So, as desired, the function "g" maps LEFT to +1 and RIGHT to -1 regardless of which is 0 and which is 1.

Now, if we introduce a new variable called "factor" and assign it the value "g(dir)", we may update the balances in our rotation method without using a conditional statement:

for a rotation in the dir direction

   1:  oldRoot->myBal -=  factor * (1 + MAX(factor * tree->myBal, 0));
   2:  tree->myBal    +=  factor * (1 + MAX(factor * oldRoot->myBal, 0));

Using this, the new code for our rotation method becomes:

   1:     // RotateOnce -- static member function that performs a single
   2:     //               rotation for the given direction.
   3:     //               Return 1 if the tree height changes due to rotation,
   4:     //               otherwise return 0.
   5:     //
   6:     template <class KeyType>
   7:     void
   8:     AvlNode<KeyType>::RotateOnce(AvlNode<KeyType> * & root, dir_t  dir) {
   9:       AvlNode<KeyType> * oldRoot  = root;
  10:       dir_t     otherDir = Opposite(dir);
  11:       short     factor   = (RIGHT - LEFT) * (1 - (2 * dir));
  12:   
  13:             // rotate
  14:       root = tree->mySubtree[otherDir];
  15:       oldRoot->mySubtree[otherDir] = tree->mySubtree[dir];
  16:       root->mySubtree[dir] = oldRoot;
  17:   
  18:             // update balances
  19:       oldRoot->myBal -=  factor * (1 + MAX(factor * root->myBal, 0));
  20:       root->myBal    +=  factor * (1 + MAX(factor * oldRoot->myBal, 0));
  21:     }

However, although this second version of "rotate" is more compact and doesn't require the use of a conditional test on the variable "dir", It may actually run slower than our first version of "rotate" because the time required to make the "test" may well be less than the time required to perform the additional multiplications and subtractions.

Now a double rotation can be implemented as a series of single rotations:

   1:     // RotateTwice -- static member function to rotate a given node
   2:     //                for the given direction and then the opposite
   3:     //                direction to restore the balance of an AVL tree
   4:     //                Return 1 if the tree height changes due to rotation,
   5:     //                otherwise return 0.
   6:     //
   7:     template <class KeyType>
   8:     void
   9:     AvlNode<KeyType>::RotateTwice(AvlNode<KeyType> * & root, dir_t  dir) {
  10:         dir_t   otherDir = Opposite(dir);
  11:         RotateOnce(root->mySubtree[otherDir], otherDir);
  12:         RotateOnce(root, dir);
  13:     }
another Method for Calculating Balances After Rotation
One may use a different method than the one described above which is perhaps simpler. Note however that the method for updating balances described above works regardless of what numbers the balance factor may contain (as long as they are correct -- it works, no matter how imbalanced). If we take into account some of the conditions that cause a rotation, we have more information to work with (like that the node to be rotated has a balance of +2 or -2 etc..)

For a single LL rotation we have one of two possibilities:

Balance Factors

Before Rotation
After Rotation

case 1:
A = +2
B = +1
A = 0
B = 0

case 2:
A = +2
B = 0
A = +1
B = -1

so in either case NewB = OldB -1 and newA = -newB so we get A = - (--B) for a single left rotation.

For a single RR rotation the possibilities are (The picture is a mirror image of the LL one -- swap all right and left kids of each node)

Balance Factors

Before Rotation
After Rotation

case 1:
A = -2
B = -1
A = 0
B = 0

case 2:
A = -2
B = 0
A = -1
B = +1

so in either case NewB = OldB +1 and newA = -newB so we get A = - (++B) for a single left rotation.

This means that we can use the following to update balances:

   1:        // Use mnemonic constants for indicating a change in height
   2:     enum height_effect_t { HEIGHT_NOCHANGE = 0, HEIGHT_CHANGE = 1 };
   3:   
   4:     // RotateOnce -- static member function that performs a single
   5:     //               rotation for the given direction.
   6:     //               Return 1 if the tree height changes due to rotation,
   7:     //               otherwise return 0.
   8:     //
   9:     template <class KeyType>
  10:     int
  11:     AvlNode<KeyType>::RotateOnce(AvlNode<KeyType> * & root, dir_t dir)
  12:     {
  13:        dir_t  otherDir = Opposite(dir);
  14:        AvlNode<KeyType> * oldRoot = root;
  15:   
  16:           // See if otherDir subtree is balanced. If it is, then this
  17:           // rotation will *not* change the overall tree height.
  18:           // Otherwise, this rotation will shorten the tree height.
  19:        int  heightChange = (root->mySubtree[otherDir]->myBal == 0)
  20:                               ? HEIGHT_NOCHANGE
  21:                               : HEIGHT_CHANGE;
  22:   
  23:           // assign new root
  24:        root = oldRoot->mySubtree[otherDir];
  25:   
  26:           // new-root exchanges it's "dir" subtree for it's parent
  27:        oldRoot->mySubtree[otherDir] = root->mySubtree[dir];
  28:        root->mySubtree[dir] = oldRoot;
  29:   
  30:           // update balances
  31:        oldRoot->myBal = -((dir == LEFT) ? --(root->myBal) : ++(root->myBal));
  32:   
  33:        return  heightChange;
  34:     }

We get an even nicer scenario when we look at LR and RL rotations. For a double LR rotation we have one of three possibilities:

Balance Factors

Before Rotation
After Rotation

case 1:
A = +2
B = +1
C = -1
A = -1
B = 0
C = 0

case 2:
A = +2
B = 0
C = -1
A = 0
B = 0
C = 0

case 3:
A = +2
B = -1
C = -1
A = 0
B = 0
C = +1

So we get, in all three cases:

   1:       newA = -max( oldB, 0 )
   2:       newC = -min( oldB, 0 )
   3:       newB = 0

Now for a double RL rotation we have the following possibilities (again, the picture is the mirror image of the LR case):

Balance Factors

Before Rotation
After Rotation

case 1:
A = -2
B = +1
C = +1
A = 0
B = 0
C = -1

case 2:
A = -2
B = 0
C = +1
A = 0
B = 0
C = 0

case 3:
A = -2
B = -1
C = +1
A = +1
B = 0
C = 0

So we get, in all three cases:

   1:       newA = -min( oldB, 0 )
   2:       newC = -max( oldB, 0 )
   3:       newB = 0

This is exactly the mirror image of what we had for the LR case: The nodes A and C in the newly rotated result simply exchanged balance factors with one another between the RL case and the LR case. What this means is that in each case, the new balance factor of the new left subtree is the same, and the new balance factor of the new right subtree is the same:

   1:       new(left)  = -max( oldB, 0 )
   2:       new(right) = -min( oldB, 0 )
   3:       new(root)  = 0

So now we can write the code for a double rotation as follows:

   1:     // RotateTwice -- static member function to rotate a given node
   2:     //                twice for the given direction in order to
   3:     //                restore the balance of an AVL tree.
   4:     //                Return 1 if the tree height changes due to rotation,
   5:     //                otherwise return 0.
   6:     //
   7:     template <class KeyType>
   8:     int
   9:     AvlNode<KeyType>::RotateTwice(AvlNode<KeyType> * & root, dir_t dir)
  10:     {
  11:        dir_t  otherDir = Opposite(dir);
  12:        AvlNode<KeyType> * oldRoot = root;
  13:        AvlNode<KeyType> * oldOtherDirSubtree = root->mySubtree[otherDir];
  14:   
  15:           // assign new root
  16:        root = oldRoot->mySubtree[otherDir]->mySubtree[dir];
  17:   
  18:           // new-root exchanges it's "dir" subtree for it's grandparent
  19:        oldRoot->mySubtree[otherDir] = root->mySubtree[dir];
  20:        root->mySubtree[dir] = oldRoot;
  21:   
  22:           // new-root exchanges it's "other-dir" subtree for it's parent
  23:        oldOtherDirSubtree->mySubtree[dir] = root->mySubtree[otherDir];
  24:        root->mySubtree[otherDir] = oldOtherDirSubtree;
  25:   
  26:           // update balances
  27:        root->mySubtree[LEFT]->myBal  = -MAX(root->myBal, 0);
  28:        root->mySubtree[RIGHT]->myBal = -MIN(root->myBal, 0);
  29:        root->myBal = 0;
  30:   
  31:           // A double rotation always shortens the overall height of the tree
  32:        return  HEIGHT_CHANGE;
  33:     }

Now that we have the rotation methods written, we just need to worry about when to call them. One helpful item is a method called balance() which is called when a node gets too heavy on a particular side:

   1:        // Use mnemonic constants for valid balance-factor values
   2:     enum balance_t { LEFT_HEAVY = -1, BALANCED = 0, RIGHT_HEAVY = 1 };
   3:   
   4:        // Return true if the tree is too heavy on the left side
   5:     inline static int
   6:     LEFT_IMBALANCE(short bal) { return (bal < LEFT_HEAVY); }
   7:   
   8:        // Return true if the tree is too heavy on the right side
   9:     inline static int
  10:     RIGHT_IMBALANCE(short bal) { return (bal > RIGHT_HEAVY); }
  11:   
  12:     // Rebalance -- static member function to rebalance a (sub)tree
  13:     //              if it has become imbalanced.
  14:     //              Return 1 if the tree height changes due to rotation,
  15:     //              otherwise return 0.
  16:     template <class KeyType>
  17:     int
  18:     AvlNode<KeyType>::ReBalance(AvlNode<KeyType> * & root) {
  19:        int  heightChange = HEIGHT_NOCHANGE;
  20:   
  21:        if (LEFT_IMBALANCE(root->myBal)) {
  22:              // Need a right rotation
  23:           if (root->mySubtree[LEFT]->myBal  ==  RIGHT_HEAVY) {
  24:                 // RL rotation needed
  25:              heightChange = RotateTwice(root, RIGHT);
  26:           } else {
  27:                 // RR rotation needed
  28:              heightChange = RotateOnce(root, RIGHT);
  29:           }
  30:        } else if (RIGHT_IMBALANCE(root->myBal)) {
  31:              // Need a left rotation
  32:           if (root->mySubtree[RIGHT]->myBal  ==  LEFT_HEAVY) {
  33:                 // LR rotation needed
  34:              heightChange = RotateTwice(root, LEFT);
  35:           } else {
  36:                 // LL rotation needed
  37:              heightChange = RotateOnce(root, LEFT);
  38:           }
  39:        }
  40:   
  41:        return  heightChange;
  42:     }

This method helps but now comes the hard part (in my humble opinion), figuring out when the height of the current subtree has changed.

Determining When the Height of the Current Subtree has Changed
After we have inserted or deleted a node from the current subtree, we need to determine if the total height of the current tree has changed so that we may pass the information up the recursion stack to previous instantiations of the insertion and deletion methods. Let us first consider the case of an insertion. The simplest case is at the point where the insertion occurred. Since we have created a node where one did not previously exist, we have increased the height of the inserted node from 0 to 1. Therefore we need to pass the value 1 (I will use "1" for TRUE and "0" for FALSE) back to the previous level of recursion to indicate the increase in the height of the current subtree.

The remaining cases for an insertion are almost as simple. If a 0 (FALSE) was the "height-change-indicator" passed back by inserting into a subtree of the current level, then there is no height change at the current level. It is true that the structure of one of the subtrees may have changed due to an insertion and/or rotation, but since the height of the subtree did not change, neither did the height of the current level.

If the current level is balanced after inserting the node (but before attempting any rotations) then we just made one subtree equal in height to the other. Therefore the overall height of the current level remains unchanged and a 0 is returned.

Before we write the code for an insertion, we still need a method to compare items while we traverse the tree. Normally, we expect this Compare() method to return a strcmp() type result (<0, ==0, or >0 for <,==,> respectively). We will be a little sneaky and write our own Compare() method which will use the Compare() method of the supplied KeyType, and take an additional parameter describing whether we want to actually compare the values of the two items, or if we just want to traverse towards the maximal or minimal element of the tree. We can use the enumeration values of the cmp_t type (EQ_CMP, MIN_CMP, MAX_CMP) to indicate the type of comparison that is desired. This extra Compare() method of ours doesnt help much for insertion, but it will be a big help for deletion (or searching) when we need to find the minimal or maximal element in a subtree:

   1:     // Compare -- Perform a comparison of the given key against the given
   2:     //            item using the given criteria (min, max, or equivalence
   3:     //            comparison). Returns:
   4:     //               EQ_CMP if the keys are equivalent
   5:     //               MIN_CMP if this key is less than the item's key
   6:     //               MAX_CMP if this key is greater than item's key
   7:     //
   8:     template <class KeyType>
   9:     cmp_t
  10:     AvlNode<KeyType>::Compare(KeyType key, cmp_t cmp) const
  11:     {
  12:        switch (cmp) {
  13:           case EQ_CMP :  // Standard comparison
  14:              return  myData->Compare(key);
  15:   
  16:           case MIN_CMP :  // Find the minimal element in this tree
  17:              return  (mySubtree[LEFT] == NULL) ? EQ_CMP : MIN_CMP;
  18:   
  19:           case MAX_CMP :  // Find the maximal element in this tree
  20:              return  (mySubtree[RIGHT] == NULL) ? EQ_CMP : MAX_CMP;
  21:        }
  22:     }

We are now ready to write the insertion method for our AVL tree:

   1:     // Insert -- Insert the given key into the given tree. Return the
   2:     //           node if it already exists. Otherwise return NULL to
   3:     //           indicate that the key was successfully inserted.
   4:     //           Upon return, the "change" parameter will be '1' if
   5:     //           the tree height changed as a result of the insertion
   6:     //           (otherwise "change" will be 0).
   7:     //
   8:     template <class KeyType>
   9:     Comparable<KeyType> *
  10:     AvlNode<KeyType>::Insert(Comparable<KeyType> *   item,
  11:                              AvlNode<KeyType>    * & root,
  12:                              int                   & change)
  13:     {
  14:           // See if the tree is empty
  15:        if (root == NULL) {
  16:              // Insert new node here
  17:           root = new AvlNode<KeyType>(item);
  18:           change =  HEIGHT_CHANGE;
  19:           return  NULL;
  20:        }
  21:   
  22:           // Initialize
  23:        Comparable<KeyType> * found = NULL;
  24:        int  increase = 0;
  25:   
  26:           // Compare items and determine which direction to search
  27:        cmp_t  result = root->Compare(item->Key());
  28:        dir_t  dir = (result == MIN_CMP) ? LEFT : RIGHT;
  29:   
  30:        if (result != EQ_CMP) {
  31:              // Insert into "dir" subtree
  32:           found = Insert(item, root->mySubtree[dir], change);
  33:           if (found)  return  found;   // already here - dont insert
  34:           increase = result * change;  // set balance factor increment
  35:        } else  {   // key already in tree at this node
  36:           increase = HEIGHT_NOCHANGE;
  37:           return  root->myData;
  38:        }
  39:   
  40:        root->myBal += increase;    // update balance factor
  41:   
  42:       // --------------------------------------------------------------------
  43:       // re-balance if needed -- height of current tree increases only if its
  44:       // subtree height increases and the current tree needs no rotation.
  45:       // --------------------------------------------------------------------
  46:   
  47:        change =  (increase && root->myBal)
  48:                       ? (1 - ReBalance(root))
  49:                       : HEIGHT_NOCHANGE;
  50:        return  NULL;
  51:     }

Deletion is more complicated than insertion. The height of the current level may decrease for two reasons: either a rotation occurred to decrease the height of a subtree (and hence the current level), or a subtree shortened in height resulting in a now balanced current level (subtree was "trimmed down" to the same size as the other). Just because a rotation has occurred however, does not mean that the subtree height has decreased. There is a special case where rotating preserves the current subtree height.

Suppose I have a tree as follows:

Deleting "A" results in the following (imbalanced) tree:

This type of imbalance cannot occur during insertion, only during deletion. Notice that the root has a balance of 2 but its heavy subtree has a balance of zero (the other case would be a -2 and a 0). Performing a single left rotation to restore the balance results in:

This tree has the same height as it did before it was rotated. Hence, we may determine if deletion caused the subtree height to change by seeing if one of the following occurred:

  1. If the new balance (after deletion) is zero and NO rotation took place.
  2. If a rotation took place but was not one of the special rotations mentioned above
    (a -2:0 or a 2:0 rotation).

For insertion, we only needed to check if a rotation occurred to see if the subtree height had changed. But for deletion we need to check all of the above. So for deletion of a node we have:

   1:     // Delete -- delete the given key from the given tree. Return NULL
   2:     //           if the key is not found in the tree. Otherwise return
   3:     //           a pointer to the node that was removed from the tree.
   4:     //           Upon return, the "change" parameter will be '1' if
   5:     //           the tree height changed as a result of the deletion
   6:     //           (otherwise "change" will be 0).
   7:     //
   8:     template <class KeyType>
   9:     Comparable<KeyType> *
  10:     AvlNode<KeyType>::Delete(KeyType              key,
  11:                              AvlNode<KeyType> * & root,
  12:                              int                & change,
  13:                              cmp_t                cmp)
  14:     {
  15:           // See if the tree is empty
  16:        if (root == NULL) {
  17:              // Key not found
  18:           change = HEIGHT_NOCHANGE;
  19:           return  NULL;
  20:        }
  21:   
  22:           // Initialize
  23:        Comparable<KeyType> * found = NULL;
  24:        int  decrease = 0;
  25:   
  26:           // Compare items and determine which direction to search
  27:        cmp_t  result = root->Compare(key, cmp);
  28:        dir_t  dir = (result == MIN_CMP) ? LEFT : RIGHT;
  29:   
  30:        if (result != EQ_CMP) {
  31:              // Delete from "dir" subtree
  32:           found = Delete(key, root->mySubtree[dir], change, cmp);
  33:           if (! found)  return  found;   // not found - can't delete
  34:           decrease = result * change;    // set balance factor decrement
  35:        } else  {   // Found key at this node
  36:           found = root->myData;  // set return value
  37:   
  38:           // ----------------------------------------------------------------
  39:           // At this point we know "result" is zero and "root" points to
  40:           // the node that we need to delete.  There are three cases:
  41:           //
  42:           //    1) The node is a leaf.  Remove it and return.
  43:           //
  44:           //    2) The node is a branch (has only 1 child). Make "root"
  45:           //       (the pointer to this node) point to the child.
  46:           //
  47:           //    3) The node has two children. Swap items with the successor
  48:           //       of "root" (the smallest item in its right subtree) and
  49:           //       delete the successor from the right subtree of "root".
  50:           //       The identifier "decrease" should be reset if the subtree
  51:           //       height decreased due to the deletion of the successor of
  52:           //       "root".
  53:           // ----------------------------------------------------------------
  54:   
  55:           if ((root->mySubtree[LEFT] == NULL) &&
  56:               (root->mySubtree[RIGHT] == NULL)) {
  57:                  // We have a leaf -- remove it
  58:              delete  root;
  59:              root = NULL;
  60:              change = HEIGHT_CHANGE;    // height changed from 1 to 0
  61:              return  found;
  62:           } else if ((root->mySubtree[LEFT] == NULL) ||
  63:                      (root->mySubtree[RIGHT] == NULL)) {
  64:                 // We have one child -- only child becomes new root
  65:              AvlNode<KeyType> * toDelete = root;
  66:              root = root->mySubtree[(root->mySubtree[RIGHT]) ? RIGHT : LEFT];
  67:              change = HEIGHT_CHANGE;    // We just shortened the subtree
  68:                 // Null-out the subtree pointers so we dont recursively delete
  69:              toDelete->mySubtree[LEFT] = toDelete->mySubtree[RIGHT] = NULL;
  70:              delete  toDelete;
  71:              return  found;
  72:           } else {
  73:                 // We have two children -- find successor and replace our
  74:                 // current data item with that of the successor
  75:              root->myData = Delete(key, root->mySubtree[RIGHT],
  76:                                    decrease, MIN_CMP);
  77:           }
  78:        }
  79:   
  80:        root->myBal -= decrease;       // update balance factor
  81:   
  82:        // -------------------------------------------------------------------
  83:        // Rebalance if necessary -- the height of current tree changes if one
  84:        // of two things happens: (1) a rotation was performed which changed
  85:        // the height of the subtree (2) the subtree height decreased and now
  86:        // matches the height of its other subtree (so the current tree now
  87:        // has a zero balance when it previously did not).
  88:        // -------------------------------------------------------------------
  89:        //change = (decrease) ? ((root->myBal) ? ReBalance(root)
  90:        //                                     : HEIGHT_CHANGE)
  91:        //                    : HEIGHT_NOCHANGE ;
  92:        if (decrease) {
  93:           if (root->myBal) {
  94:              change = ReBalance(root);  // rebalance and see if height changed
  95:           } else {
  96:              change = HEIGHT_CHANGE;    // balanced because subtree decreased
  97:           }
  98:        } else {
  99:           change = HEIGHT_NOCHANGE;
 100:        }
 101:   
 102:        return  found;
 103:     }

Note how in the case of both subtrees of the deleted item being non-null, I only need one statement. This is due to the way AvlNode::Delete sets its parameters. The data pointer passed on entrance points to the deleted node's data on exit. So I just delete the minimal element of the right subtree, and steal its data as my-own (returning my former data item on exit).

And there we have it, the maintenance of AVL tree manipulations, the brunt of which is covered in 5 methods, none of which (except for delete which is about 1.5 pages) is greater than 1 normal page in length, including comments (and there are a lot). The main methods are:

All other methods are very small and easy to code. The only method still missing is the Search() method, and that is no different from a normal binary tree search:

   1:     // Search -- Look for the given key using the given comparison criteria,
   2:     //           return NULL if not found, otherwise return the item address.
   3:     template <class KeyType>
   4:     Comparable<KeyType> *
   5:     AvlNode<KeyType>::Search(KeyType            key,
   6:                              AvlNode<KeyType> * root,
   7:                              cmp_t              cmp)
   8:     {
   9:        cmp_t result;
  10:        while (root  &&  (result = root->Compare(key, cmp))) {
  11:           root = root->mySubtree[(result < 0) ? LEFT : RIGHT];
  12:        }
  13:        return  (root) ? root->myData : NULL;
  14:     }

And lets not forget the constructor and destructor:

   1:     template <class KeyType>
   2:     AvlNode<KeyType>::AvlNode(Comparable<KeyType> * item)
   3:        : myData(item), myBal(0)
   4:     {
   5:        myBal = 0 ;
   6:        mySubtree[LEFT] = mySubtree[RIGHT] = NULL ;
   7:     }
   8:   
   9:     template <class KeyType>
  10:     AvlNode<KeyType>::~AvlNode(void) {
  11:        if (mySubtree[LEFT])  delete  mySubtree[LEFT];
  12:        if (mySubtree[RIGHT]) delete  mySubtree[RIGHT];
  13:     }

Now that we have implemented most of the methods for AVL tree manipulations, we should probably finish the declaration that we started near the beginning of this discussion:

   1:     #include "Comparable.h"
   2:   
   3:     // Indices into a subtree array
   4:     //     NOTE: I would place this inside the AvlNode class but
   5:     //           when I do, g++ complains when I use dir_t. Even
   6:     //           when I prefix it with AvlNode:: or AvlNode<KeyType>::
   7:     //           (If you can get this working please let me know)
   8:     //
   9:     enum  dir_t { LEFT = 0, RIGHT = 1 };
  10:   
  11:     // AvlNode -- Class to implement an AVL Tree
  12:     //
  13:     template <class KeyType>
  14:     class AvlNode {
  15:     public:
  16:           // Max number of subtrees per node
  17:        enum  { MAX_SUBTREES = 2 };
  18:   
  19:        static  dir_t
  20:        Opposite(dir_t dir) {
  21:          return dir_t(1 - int(dir));
  22:        }
  23:   
  24:        // ----- Constructors and destructors:
  25:   
  26:        AvlNode(Comparable<KeyType>  * item=NULL);
  27:        virtual ~AvlNode(void);
  28:   
  29:        // ----- Query attributes:
  30:   
  31:           // Get this node's data
  32:        Comparable<KeyType> *
  33:        Data() const { return  myData; }
  34:   
  35:           // Get this node's key field
  36:        KeyType
  37:        Key() const { return  myData->Key(); }
  38:   
  39:           // Query the balance factor, it will be a value between -1 .. 1
  40:           // where:
  41:           //     -1 => left subtree is taller than right subtree
  42:           //      0 => left and right subtree are equal in height
  43:           //      1 => right subtree is taller than left subtree
  44:        short
  45:        Bal(void) const { return  myBal; }
  46:   
  47:           // Get the item at the top of the left/right subtree of this
  48:           // item (the result may be NULL if there is no such item).
  49:           //
  50:        AvlNode *
  51:        Subtree(dir_t dir) const { return  mySubtree[dir]; }
  52:   
  53:        // ----- Search/Insert/Delete
  54:        //
  55:        //   NOTE: These are all static functions instead of member functions
  56:        //         because most of them need to modify the given tree root
  57:        //         pointer. If these were instance member functions than
  58:        //         that would correspond to having to modify the 'this'
  59:        //         pointer, which is not allowed in C++. Most of the
  60:        //         functions that are static and which take an AVL tree
  61:        //         pointer as a parameter are static for this reason.
  62:   
  63:           // Look for the given key, return NULL if not found,
  64:           // otherwise return the item's address.
  65:        static Comparable<KeyType> *
  66:        Search(KeyType key, AvlNode<KeyType> * root, cmp_t cmp=EQ_CMP)
  67:   
  68:           // Insert the given key, return NULL if it was inserted,
  69:           // otherwise return the existing item with the same key.
  70:        static Comparable<KeyType> *
  71:        Insert(Comparable<KeyType> * item, AvlNode<KeyType> * & root) {
  72:           int  change;
  73:           return  Insert(item, root, change);
  74:        }
  75:   
  76:           // Delete the given key from the tree. Return the corresponding
  77:           // node, or return NULL if it was not found.
  78:        static Comparable<KeyType> *
  79:        Delete(KeyType key, AvlNode<KeyType> * & root, cmp_t cmp=EQ_CMP) {
  80:           int  change;
  81:           return  Delete(key, root, change, cmp);
  82:        }
  83:   
  84:     private:
  85:   
  86:        // ----- Private data
  87:   
  88:        Comparable<KeyType> * myData;  // Data field
  89:        AvlNode<KeyType>    * mySubtree[MAX_SUBTREES];   // Subtree pointers
  90:        short                 myBal;   // Balance factor
  91:   
  92:        // ----- Routines that do the *real* insertion/deletion
  93:   
  94:           // Insert the given key into the given tree. Return the node if
  95:           // it already exists. Otherwise return NULL to indicate that
  96:           // the key was successfully inserted.  Upon return, the "change"
  97:           // parameter will be '1' if the tree height changed as a result
  98:           // of the insertion (otherwise "change" will be 0).
  99:        static Comparable<KeyType> *
 100:        Insert(Comparable<KeyType> *   item,
 101:               AvlNode<KeyType>    * & root,
 102:               int                   & change);
 103:   
 104:           // Delete the given key from the given tree. Return NULL if the
 105:           // key is not found in the tree. Otherwise return a pointer to the
 106:           // node that was removed from the tree.  Upon return, the "change"
 107:           // parameter will be '1' if the tree height changed as a result
 108:           // of the deletion (otherwise "change" will be 0).
 109:        static Comparable<KeyType> *
 110:        Delete(KeyType              key,
 111:               AvlNode<KeyType> * & root,
 112:               int                & change,
 113:               cmp_t                cmp=EQ_CMP);
 114:   
 115:        // Routines for rebalancing and rotating subtrees
 116:   
 117:           // Perform an XX rotation for the given direction 'X'.
 118:           // Return 1 if the tree height changes due to rotation,
 119:           // otherwise return 0.
 120:        static int
 121:        RotateOnce(AvlNode<KeyType> * & root, dir_t dir);
 122:   
 123:           // Perform an XY rotation for the given direction 'X'
 124:           // Return 1 if the tree height changes due to rotation,
 125:           // otherwise return 0.
 126:        static int
 127:        RotateTwice(AvlNode<KeyType> * & root, dir_t dir);
 128:   
 129:           // Rebalance a (sub)tree if it has become imbalanced
 130:        static int
 131:        ReBalance(AvlNode<KeyType> * & root);
 132:   
 133:           // Perform a comparison of the given key against the given
 134:           // item using the given criteria (min, max, or equivalence
 135:           // comparison). Returns:
 136:           //    EQ_CMP if the keys are equivalent
 137:           //    MIN_CMP if this key is less than the item's key
 138:           //    MAX_CMP if this key is greater than item's key
 139:        cmp_t
 140:        Compare(KeyType key, cmp_t cmp=EQ_CMP) const;
 141:   
 142:     private:
 143:           // Disallow copying and assignment
 144:        AvlNode(const AvlNode<KeyType> &);
 145:        AvlNode & operator=(const AvlNode<KeyType> &);
 146:     };

转载于:https://my.oschina.net/lyr/blog/61005

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值