Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

Implementation

Program gif gives the code for the LLRotation method of the AVLTree class. This code implements the LL rotation shown in Figure gif. The purpose of the LLRotation method is to perform an LL rotation at the root of a given AVL tree instance.

   program20284
Program: AVLTree class LLRotation method.

The rotation is simply a sequence of variable manipulations followed by two height adjustments. Notice the rotation is done in such a way so that the the given AVLTree instance remains the root of the tree. This is done so that if the tree has a parent, it is not necessary to modify the contents of the parent.

The AVLTree class also requires an RRRotation method to implement an RR rotation. The implementation of that method follows directly from Program gif. Clearly, the running time for the single rotations is O(1).

Program gif gives the implementation for the LRRotation method of the AVLTree class. This double rotation is trivially implemented as a sequence of two single rotations. As above, the method for the complementary rotation is easily derived from the given code. The running time for each of the double rotation methods is also O(1).

   program20300
Program: AVLTree class LRRotation method.

When an imbalance is detected, it is necessary to correct the imbalance by doing the appropriate rotation. The code given in Program gif takes care of this. The Balance method tests for an imbalance using the BalanceFactor property. The balance test itself takes constant time. If the node is balanced, only a constant-time height adjustment is needed.

   program20312
Program: AVLTree class Balance method.

Otherwise, the Balance method of the AVLTree class determines which of the four cases has occurred, and invokes the appropriate rotation to correct the imbalance. To determine which case has occurred, the Balance method calls the BalanceFactor property get accessor at most twice. Therefore, the time for selecting the case is constant. In all only one rotation is done to correct the imbalance. Therefore, the running time of this method is O(1).

The Insert method for AVL trees is inherited from the BinarySearchTree class (see Program gif). The Insert method calls AttachKey to do the actual insertion. The AttachKey method is overridden in the AVLTree class as shown in Program gif.

   program20334
Program: AVLTree class AttachKey method.

The very last thing that the Insert method does is to call the Balance method. which has also been overridden as shown in Program gif. As a result the Insert method adjusts the heights of the nodes along the insertion path and does a rotation when an imbalance is detected. Since the height of an AVL tree is guaranteed to be tex2html_wrap_inline59121, the time for insertion is simply tex2html_wrap_inline59121.


next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.