CS 367 - Introduction to Data Structures - Section 3

Programming Assignment Three: AVL Trees

Due Thursday, December 4, 1997 by 2:30pm



Contents


The Assignment

The goal of this assignment is to implement an AVL tree class template as a derived class template from the binary search tree class template.

The assignment is intended to:


Requirements

This assignment is not meant to be lengthy. However, it involves several concepts that you may not have seen before so time should be taken to read all the related material (including related source code) and to read it carefully.

Specifically, you need to implement a fully documented AVL class template, much like the red-black class template mentioned in class and available on-line. For this assignment, you need not implement AVL remove. (You can do so for extra credit or if you choose to use AVL trees for the final project.) Your AVL class template interface should include public search (maybe overriding BST's search) and insert (definitely overriding BST's insert) functions, as well as any other member functions (public, protected, or private) you deem necessary. Use the interface in red-black.h as a model.

There are at least two ways in which you might represent AVL trees as a derived class of BST. One is to derive it simply as a subclass of BST<Item,Key> --- in other words storing nothing extra at each node. Another way stores at each node the height of the subtree rooted at that node. This means that AVL<Item,Key> could be implemented as a subclass of BST<pair<Item,size_t>,Key> --- much like RBT<Item,Key> is a subclass of BST<pair<Item.color>,Key>. (pair is a class template that represents a pair of two potentially different classes of data. It is defined in the C++ standard template library. You can see the definition here.) If you do not explicitly store the height of the subtree rooted at a node at each node you can always compute it by calling the underlying height function for binary trees. It is easier, but less efficient to do things this way. You are free to implement AVL trees either way. For extra credit you can do both implementations.


Helpful information

Read everything in this document as well as the related material referred to herein.

From the notes on AVL trees, what we have (or will have) discussed in class, and the handout on red-black trees, you should understand how single rotations work. These rotations have already been implemented in red-black.C. For this assignment, you may copy that code (rotateLeft and rotateRight) directly into your avl.C file. Note, however, that depending on how you choose to implement AVL trees (see above), you may well need to make small modifications to the AVL version of these two functions.

You should write functions to implement double rotations (for example doubleRotateLeft and doubleRotateRight). The double left rotation works as follows:
     (x)                    (y)   
    /   \  	           /   \  
  'a    (z)      ===>   (x)     (z)
        / \	        / \     / \
      (y) 'd	      'a  'b  'c  'd
      / \  	         
    'b  'c    	       
In the diagram, (x), (y), (z) represent three nodes in a binary search tree such that (x)'s key is less than (y)'s key which is less than (z)'s key. 'a, 'b', 'c, and 'd (read alpha, beta, gamma, delta) represent subtrees. (x)'s left child is the subtree 'a. So double left rotation around node (x) works by rotating (x) down and to the left, and (y) into the position formerly occupied by (x).

The double right rotation is symmetrtic with the double left:

        (z)                 (y)   
       /   \  	           /   \  
    (x)    'd    ===>   (x)     (z)
    / \       	        / \     / \
  'a  (y)     	      'a  'b  'c  'd
      / \     	         
    'b  'c    	       

Note that for all four types of rotations, the binary search tree property is preserved. Your task is to use these rotations so as to preserve the AVL property (for each node, the left and right subtrees differ in height by no more than one) in the face of insertions to the tree. The following is a rough outline of how AVL's insert might look:
template <class Item, class Key>
bool AVL<Item,Key>::insert(const Item& v, const Key& k)
{
  ...
  // make use of BST's protected _insert functon
  _insert(...);
  ...
  // apply rotations if necessart to restore AVL property
  fixInsert(...);
  ...
  return (...);
}

template <class Item, class Key>
void AVL<Item,Key>::fixInsert(...)
{
  // fix height for all ancestors of newly inserted node
  // and find "critical node" if there is one
  ...

  // if there is a critical node (the root of the smallest 
  // subtree that violates the AVL property), determine 
  // which kind of imbalance occurs and perform proper 
  // rotation to rectify the imbalance
  ...
    ... 
    // case left imbalance (critical node's left subtree
    // is two nodes taller than its right subtree)
      ...
      // case critical node's left subtree is left tall
        ...
	rotateRight(criticalNode);
        ...
      // case critical node's left subtree is right tall
        doubleRotateRight(criticalNode);
	...
    // case right imbalance
      ...
      // case critical node's right subtree is right tall
        rotateLeft(criticalNode);
	...
      // case critical node's right subtree is left tall
        doubleRotateLeft(criticalNode);
	...
}

Extra Credit

Extra credit for this assignment includes:

Any extra credit work must be well documented in a file you submit called EXTRA-CREDIT.

Extra credit is designed to make up points lost on previous assignments or the first exam. If you do not need the extra credit, please do not submit it (of occurs, feel free to try it anyway).

You might think about augmenting this program more substantially for a final project.


Grading

This assignment will be graded on an one-hundred point scale, distributed as follows:

For more information on grading policy, click here.

Program Three has been graded. You can find your grade in the directory

~cs367-3/Submissions/Three/<login>
where <login> is your login. The file is called grade. To view your grade file you can type:
cat ~cs367-3/Submissions/Three/<login>/grade
where <login> is your login.


Files

You need to create two files: avl.C and avl.h. By uncommenting the lines that make use of the AVL class template in bst-master.C you can then compile and run the bst-master program to check your work.

You should not need to modify any of the other files except Makefile where you should add avl.h and avl.C to the dependency list once you have created those files. Note that you can compile and run straight away --- what you will get is a program that simply compares regular binary search trees to red-black trees. Your job is to add an AVL class template so that AVLs can also be compared with the other two types of search trees. The files you need to do the assignment are located in the directory ~cs367-3/Assignments/Three/Files/.


How to get started


Submitting

What we need once you are done: two files: avl.h and avl.C. You should not need to (and are requested not to) submit additional files. Your compile grade hinges upon our ability to compile your avl.h and avl.C with the other files remaining intact. Copy the necessary files to the directory ~cs367-3/Submissions/Three/<login> where <login> is your login name. For example if your login name is foo, then from the directory where your modified bag.C file is located, type:

cp bag.C ~cs367-3/Submissions/Three/foo 

This is a paperless assignment. You need only turn in the program electronically. However, Marc will be able to examine your code more easily and provide better feedback to you if you do submit paper version of avl.h and avl.C.

If you do any extra credit, submit a file called EXTRA-CREDIT indicating what you do on top of the required work.


Solution

A compiled executable derived from the solution is available for you to try out. The solution executable can be found at:
~cs367-3/Solutions/Three/bst-master

The instructor's solution is now available.


Late Policy

The assignment is due on Thursday, December 4 at the start of class. No exceptions. PERIOD. The final exam and the final project will be right around the corner so you won't even want to consider trying to turn this in late, anyhow.