COMP 171 Data Structures and Algorithms
2002 Fall Term, MWF 12:00-12:50, Rm 2503
Dr. Dekai WU, Rm 3539, 2358-6989, dekai@cs.ust.hk
Programming Assignment 2

In this programming assignment you will implement a dynamic dictionary ADT using a red-black tree.

You will have to hand in a physical printout of each of your programs. We will also electronically check your programs.

As usual, please remember to submit your work before the deadline. If you have any questions about this assignment, please send emails to shenyh@ust.hk.

DUE DATE

The deadline of the assignment is 12:00 noon, Wed 20 Nov 2002. Late assignments will not be accepted, since ACS will not collect them.

GRADING

Each function written should be adequately documented so that a reader will be able to understand what is being done. Your programs will be run on a standard test data set.

This assignment will account for 80 points in your final score. The general grading criteria for a program is as follows:

When your program does not run correctly on the test data, then your program will be graded by visual examination and the maximum score you can then get for Correctness is 30%.

ACS SUBMISSION

To avoid receiving zero credit for this assignment, please pay careful attention to the following instructions. We will electronically copy the programs from your account to ours for testing. To ensure that the copying routine performs properly you must do the following.

IMPLEMENTATION

You are required to program a red-black tree implementation of a dynamic dictionary that stores distinct numeric keys (which could be ints, floats, doubles, etc). Updating in the dictionary is allowed.

Use the following type declarations for the tree node. You are allowed to add any other public or protected member variables and functions as you wish to this class, so long as you implement at least these members correctly.

enum RBColor { RED, BLACK };

template <class T>
class RBTreeNode {
public:
  T key;
  RBTreeNode* leftchild;
  RBTreeNode* rightchild;
  RBTreeNode* parent;
  RBColor color;
};

You are required to program a red-black tree container class that supports three member functions. This class provides a Dictionary API. As such, you may not add any other public member variables or functions to this class (except, of course, constructors or destructors).

template <class T>
class RedBlackTree {
public:
  T* find(const T& x) const;
    // Search the tree to find x.  Return a pointer to the matching key,
    // or NULL if x is not found.
  bool insert(const T& x);
    // Insert x into the tree.  If x has been inserted before, then it
    // is NOT inserted again.  Return true iff this is the first time
    // that x is inserted.
  int erase(const T& x);
    // Delete x from the tree.  Return 1 if x was found and deleted,
    // or 0 if x was not found.
protected:
  RBTreeNode<T>* getRoot();
    // Return a pointer to the root of the red-black tree.
  RBTreeNode<T>* getSentinel();
    // Return a pointer to the node being used as a sentinel.
};

Rules to be observed:

  1. All insertion and deletion should be performed in the fashion as described in the textbook (and in lecture). Do not program according to any other textbook.
  2. During deletion, when you try to perform a rotation, you should check with the left sibling of a node to see whether rotation can be done. If the left sibling does not exist or rotation cannot be done, then check with the right sibling to see whether a rotation can be done.

A TEST DRIVER

You may find the following files helpful for your implementation and testing:

The files containing the test arrays:

These files containing arrays of different length to do your testing. The length of an array is always an exponent of 2. The files have the filename with the style "array*.txt" in which "*" represents the length of the array, i.e. the file "array32.txt" contains an array with the length of 32 and the file "array1024.txt" contains an array with the length of 1024.

The test driver:

The test driver uses one argument which is the length of the array you want to test your algorithm with.

The test driver is composed of four parts:

  1. Read an array of the length defined in the argument from the corresponding file.
  2. The sample code that inserts the keys of the array into an empty red black tree one by one and examines the red black property after each insertion.
  3. The sample code that tries to find a key in the red black tree.
  4. The sample code that erases the keys of the array from a red black tree one by one and examines the red black property after each erasion.

Testing your algorithm with the test driver:

Please follow the following steps to go on with the test:

We strongly suggest you begin your test with arrays of small size to find the problems with your algorithm.



Dekai WU, SHEN Yihai
dekai@cs.ust.hk, shenyh@ust.hk
Last update: 2002.11.15