// Textbook fragment 10.11 public Entry remove(Entry ent) throws InvalidEntryException { Entry toReturn = super.remove(ent); Position> posR = actionPos; if (toReturn != null) { if (wasParentRed(posR) || isRoot(posR) || isPosRed(posR)) setBlack(posR); else remedyDoubleBlack(posR); } return toReturn; } protected void remedyDoubleBlack(Position> posR) { Position> posX, posY, posZ; boolean oldColor; posX = parent(posR); posY = sibling(posR); if (!isPosRed(posY)) { posZ = redChild(posY); if (hasRedChild(posY)) { // Case 1: trinode restructuring oldColor = isPosRed(posX); posZ = restructure(posZ); setColor(posZ, oldColor); setBlack(posR); setBlack(left(posZ)); setBlack(right(posZ)); return; } setBlack(posR); setRed(posY); if (!isPosRed(posX)) { // Case 2: recoloring if (!isRoot(posX)) remedyDoubleBlack(posX); return; } setBlack(posX); return; } // Case 3: adjustment if (posY == right(posX)) posZ = right(posY); else posZ = left(posY); restructure(posZ); setBlack(posY); setRed(posX); remedyDoubleBlack(posR); }