G is known to be black since its former child p could

• 14

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 7 - 9 out of 14 pages.

Gis known to be black, since its former childPcould not have been red otherwise (without violating property 4). Then,the colors ofPandGare switched, and the resulting tree satisfies property 4 (both children of every red node are black). Property 5 (all pathsfrom any given node to its leaf nodes contain the same number of black nodes) also remains satisfied, since all paths that went through anyof these three nodes went throughGbefore, and now they all go throughP. In each case, this is the only black node of the three.voidinsert_case5(structnode*n){structnode*g=grandparent(n);n->parent->color=BLACK;g->color=RED;
Redblack tree8if(n==n->parent->left)rotate_right(g);elserotate_left(g);}Note that inserting is actuallyin-place,since all the calls above usetail recursion.RemovalIn a regular binary search tree when deleting a node with two non-leaf children, we find either the maximumelement in its left subtree (which is the in-order predecessor) or the minimum element in its right subtree (whichis the in-order successor) and move its value into the node being deleted (as shownhere). We then delete thenode we copied the value from, which must have fewer than two non-leaf children. (Non-leaf children, ratherthan all children, are specified here because unlike normal binary search trees, red–black trees have leaf nodesanywhere they can have them, so that all nodes are either internal nodes with two children or leaf nodes with,by definition, zero children. In effect, internal nodes having two leaf children in a red–black tree are like the leafnodes in a regular binary search tree.) Because merely copying a value does not violate any red–blackproperties, this reduces to the problem of deleting a node with at most one non-leaf child. Once we have solvedthat problem, the solution applies equally to the case where the node we originally want to delete has at mostone non-leaf child as to the case just considered where it has two non-leaf children.Therefore, for the remainder of this discussion we address the deletion of a node with at most one non-leaf child. Weuse the labelMto denote the node to be deleted;Cwill denote a selected child ofM, which we will also call "its child".IfMdoes have a non-leaf child, call that its child,C; otherwise, choose either leaf as its child,C.IfMis a red node, we simply replace it with its childC, which must be black by property 4. (This can onlyoccur whenMhas two leaf children, because if the red nodeMhad a black non-leaf child on one side butjust a leaf child on the other side, then the count of black nodes on both sides would be different, thus thetree would violate property 5.) All paths through the deleted node will simply pass through one less rednode, and both the deleted node's parent and child must be black, so property 3 (all leaves are black) andproperty 4 (both children of every red node are black) still hold.

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 14 pages?

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
black tree