13.3 Insertion |
The only difference between this code and <BST item insertion function 33> is that we set n's parent pointer after insertion.
492. <PBST item insertion function 492> = void **
pbst_probe (struct pbst_table *tree, void *item)
{ struct pbst_node *p, *q; /* Current node in search and its parent. */ int dir; /* Side of q on which p is located. */ struct pbst_node *n; /* Newly inserted node. */ assert (tree != NULL && item != NULL); <Step 1: Search PBST tree for insertion point 493> <Step 2: Insert PBST node 494> return &n->pbst_data; }
This code is included in 491.
493. <Step 1: Search PBST tree for insertion point 493> = for (q = NULL, p = tree->pbst_root; p != NULL; q = p, p = p->pbst_link[dir])
{ int cmp = tree->pbst_compare (item, p->pbst_data, tree->pbst_param); if (cmp == 0) return &p->pbst_data; dir = cmp > 0; }
This code is included in 492 and 557.
494. <Step 2: Insert PBST node 494> = n = tree->pbst_alloc->libavl_malloc (tree->pbst_alloc, sizeof *p); if (n == NULL) return NULL; tree->pbst_count++; n->pbst_link[0] = n->pbst_link[1] = NULL; n->pbst_parent = q; n->pbst_data = item; if (q != NULL) q->pbst_link[dir] = n; else
tree->pbst_root = n;
This code is included in 492, 527, and 558.
See also: [Cormen 1990], section 13.3.