15.3 Insertion [ToC] [Index]     [Skip Back] [Skip Fwd]     [Prev] [Up] [Next]

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:

557. <PRB item insertion function 557> =
void **
prb_probe (struct prb_table *tree, void *item)
{ struct prb_node *p; /* Traverses tree looking for insertion point. */ struct prb_node *q; /* Parent of p; node at which we are rebalancing. */ struct prb_node *n; /* Newly inserted node. */ int dir; /* Side of q on which n is inserted. */ assert (tree != NULL && item != NULL); <Step 1: Search PBST tree for insertion point; pbst => prb 493> <Step 2: Insert PRB node 558> <Step 3: Rebalance after PRB insertion 559> return &n->prb_data; }

This code is included in 556.

See also:  [Cormen 1990], section 14.3.