15.3 Insertion |

Inserting into a red-black tree is a problem whose form of solution should by now be familiar to the reader. We must now update parent pointers, of course, but the major difference here is that it is fast and easy to find the parent of any given node, eliminating any need for a stack.

Here's the function outline. The code for finding the insertion point is taken directly from the PBST code:

555. <PRB item insertion function 555> =void**prb_probe(structprb_table*tree,void*item)

{structprb_node*p; /* Traverses tree looking for insertion point. */structprb_node*q; /* Parent ofp; node at which we are rebalancing. */structprb_node*n; /* Newly inserted node. */intdir; /* Side ofqon whichnis inserted. */assert(tree!=NULL&&item!=NULL); <Step 1: Search PBST tree for insertion point; pbst => prb 491> <Step 2: Insert PRB node 556> <Step 3: Rebalance after PRB insertion 557>return&n->prb_data; }

This code is included in 554.

**See also:**
[Cormen 1990], section 14.3.