public void insertItem(Object key, Object element) throws InvalidKeyException { super.insertItem(key, element); // may throw an InvalidKeyException Position posZ = actionPos; // start at the insertion position T.replaceElement(posZ, new RBTItem(key, element, Red)); if (T.isRoot(posZ)) setBlack(posZ); else remedyDoubleRed(posZ); } protected void remedyDoubleRed(Position posZ) { Position posV = T.parent(posZ); if (T.isRoot(posV)) return; if (!isPosRed(posV)) return; // we have a double red: posZ and posV if (!isPosRed(T.sibling(posV))) { // Case 1: trinode restructuring posV = ((RestructurableNodeBinaryTree) T).restructure(posZ); setBlack(posV); setRed(T.leftChild(posV)); setRed(T.rightChild(posV)); } else { // Case 2: recoloring setBlack(posV); setBlack(T.sibling(posV)); Position posU = T.parent(posV); if (T.isRoot(posU)) return; setRed(posU); remedyDoubleRed(posU); } }