# Red Black Trees Deletion (3).docx - Red Black Trees...

• 14

This preview shows page 1 - 3 out of 14 pages.

Red Black Trees Deletion The deletion of nodes in the red-black tree starts with identifying if the node is of degree 0, degree 1, or degree 2. No matter what the case maybe, the programmer should always consider the edge case of the root being deleted and adjust their code accordingly. Deleting nodes of degree 2 If the node is of degree 2, then the programmer should swap the node’s value with that of its inorder successor, like it would with a naïve binary search tree. Then the programmer would consider removing the new node from its location of the tree. Here is an example of a situation when a node is of degree 2 and the switch that will occur. When the node is swapped with its inorder successor, the programmer knows that the new node is either a node of degree 0 or a node of degree 1 and can then apply the rules of those nodes to its deletion. When deleting a node, the node to be deleted is often indicated as the letter v and the node that will be replacing it is often indicated by the letter u . However, in these nodes, the deleting node will be called nodeToRemove and the replacing node will be called child. Deleting when u or v is red When the nodeToRemove, v, or the child of the nodeToRemove, u, is red then the deletion is straight forward. The child of the nodeToRemove will take the place of the nodeToRemove and will become black.
The programmer could write a method in the RedBlackTree. The method would receive the node to remove and make sure that its child, regardless of whether it is the left or right child, is removed from the node. The child, which is also sent up through the method, would then make sure it becomes a black node then would be returned through the method so that it can be attached to the appropriate node. private RedBlackNode simpleCaseDeletion(RedBlackNode nodeToRemove, RedBlackNode child) { nodeToRemove.setLeft(null); nodeToRemove.setRight(null); if(child != null) child.setColor(1); return child; } In the case of removing a red leaf, all the programmer has to do is to make sure the parent that is pointing to the red leaf instead points to a null. Deleting when u and v are black Deleting a nodeToRemove and its child which are both black involves the introduction of the double black node. The double black node occurs when one branch is heavier than the other branch. The best way to consider a double black node in relationship to the other two nodes is that of a closed set or {red, black, double black} or {0, 1, 2}. A null is considered a black node, although it is not counted as part of the black height. When the nodeToRemove, v, is black and has no children, then it is considered replaced by the null children which are both black nodes creating a double black node as seen below: The red black tree height is no longer distributed evenly. The left branch is of black height 0 and the right branch is of black height one. A restructuring needs to take place so that it is evenly distributed. To