Code Fragment: RBTree2



public:
  void insertItem(const Key& k, const Element& e) {	// insert (key,element)
    BTPosition z = inserter(k, e);			// insert in base tree
    if (T.isRoot(z))
      setBlack(z);					// root is always black
    else 
      remedyDoubleRed(z);				// rebalance if needed
  }
protected:
  void remedyDoubleRed(const BTPosition& z) {		// fix double-red z
    BTPosition v = T.parent(z);				// v is z's parent
    if (T.isRoot(v) || isBlack(v)) return;		// v is black, all ok
    							// z, v are double-red
    if (isBlack(T.sibling(v)))  {			// Case 1: restructuring
      v = T.restructure(z);
      setBlack(v);					// top vertex now black
      setRed(T.leftChild(v)); setRed(T.rightChild(v));	// children are red
    }  
    else  {						// Case 2: recoloring
      setBlack(v);					// make v black
      setBlack(T.sibling(v));				// ..and its sibling
      BTPosition u = T.parent(v);			// u is v's parent
      if (T.isRoot(u)) return;
      setRed(u);					// make u red
      remedyDoubleRed(u);				// may need to fix u now
    }
  }