// Computer Science 367, Section 3, Fall 1997 // Instructor: Michael Siff (siff@cs.wisc.edu) // // // FILE: red-black.h // // // TEMPLATE CLASS PROVIDED: RBT - a class template // for a red-black tree of Items indexed by their Keys. It is a derived // class template from the binary search tree class template BST. // // The template parameter Item is the data type of the items stored in // the red-black tree. It may be any of the C++ built-in types (int, // char, etc.), or a class with a default constructor, a copy // constructor, and an assignment operator. // The template parameter Key is the data type of the priority key // associated with each item. It may be any C++ built-in type that has a // comparison operator (<), or a class with a copy constructor, // assignment operator, equality(==), inequality(!=), and comparison(<). // // // CONSTRUCTORS for the RBT template class: // RBT() // Postcondition: The red-black tree has been initialized as an // empty tree. // RBT(const RBT& source) // Postcondition: The red-black tree has been initialized as a // complete copy of source. (This is the copy constructor.) // // // DESTRUCTOR for the RBT template class: // ~RBT() // Postcondition: The memory allocated to store the red-black tree // has been freed. // // // MODIFICATION MEMBER FUNCTIONS for the RBT template class: // void operator=(const RBT& source) // Postcondition: This tree has been assigned as a complete copy of // source. // bool insert(const Item& v, const Key& k) // Postcondition: If an item of key k is not already in the tree, v has // been inserted into the tree with key k and true is // returned. Otherwise, the tree remains the same and false is returned. // bool remove(const Key& v) // Postcondition: If an item of key k was in the tree, it has been // deleted from the tree and true is returned. Otherwise, the tree // remains the same and false is returned. // // // CONSTANT MEMBER FUNCTIONS for the RBT template class: // bool isEmpty() const // Postcondition: true if and only if the tree is empty. // bool search(const Key& k, Item& returnVal) const // Postcondition: If an item of key k is in the tree, returnVal is // assigned that item and true is returned. Otherwise, returnVal // remains the same and false is returned. // size_t size() const // Postcondition: the return value is the number of nodes in the binary // search tree. // size_t height() const // Precondition: the tree is not empty. // Postcondition: the return value is the height of the red-black tree. #ifndef RED_BLACK_TREE_H #define RED_BLACK_TREE_H #include #include "bst.h" enum color {RED, BLACK}; template class RBT : public BST, Key> { public: // Constructors RBT(); RBT(const RBT& source); // copy constructor // Destructor ~RBT(); // Constant member functions bool search(const Key& k, Item& returnVal) const; // Modification member functions bool insert(const Item& v, const Key& k); bool remove(const Key& v); private: typedef BinaryTree,Key> > BT; protected: // Protected member functions Item itemOf(const pair,Key>& ick) const; color colorOf(BT *node) const; void setColor(BT *node, color c); BT *rotateLeft(BT *node); BT *rotateRight(BT *node); void fixInsert(BT *node); void fixRemove(BT *x, BT *p); }; #include "red-black.C" #endif