Code Fragment: RBTree1



template <typename Key, typename Element>
class RBTree : public BinarySearchTree<Key, Element, RBItem<Key,Element> > {
protected: 						// local types
  typedef RBItem<Key, Element> 		Item;		// a tree node item
  typedef BinarySearchTree<Key, Element, Item>  BST;	// base search tree
  typedef BST::BTPosition 		BTPosition;	// a tree position
public: 						// public types
  typedef BST::Position 		Position;	// a position
protected: 						// local utilities
  Color color(const BTPosition& p) const {		// get position's color
    if (T.isExternal(p)) return BLACK;			// externals are black
    return p.element().color();
  }
  bool isRed(const BTPosition& p) const 		// is p red?
    { return color(p) == RED; }
  bool isBlack(const BTPosition& p) const 		// is p black?
    { return color(p) == BLACK; }
  void setRed(const BTPosition& p)			// make p red 
    { if (T.isInternal(p)) p.element().setColor(RED); }
  void setBlack(const BTPosition& p)			// make p black
    { if (T.isInternal(p)) p.element().setColor(BLACK); }
  void setColor(const BTPosition& p, Color color)	// set p's color
    { if (T.isInternal(p)) p.element().setColor(color); }
  bool hasTwoExternalChildren(const BTPosition& p) const // 2 external children?
    { return (T.isExternal(T.leftChild(p)) &&
    	      T.isExternal(T.rightChild(p))); }
  bool hasRedChild(const BTPosition& p) const 		// does p have red child?
    { return (isRed(T.leftChild(p)) || isRed(T.rightChild(p))); }
  // ... (other utilities omitted)
public:
  RBTree() : BST() { }					// constructor
  // ... (insert insertItem() and removeElement() here)
};