link LLRBinsert(link h, Item *item) { Key v = key(item); /* Insert a new node at the bottom*/ conflict = NULL; if (h == z) { return NEW(item, z, z, 1, 1); } if (less(v, key(h->item))) hl = LLRBinsert(hl, item); else if (eq(v, key(h->item))) { /* If the object we are trying to insert is already there, we opt to return a pointer to the existing item, so that the user may choose what to do (i.e. create a list of items) */ conflict = h->item; } else hr = LLRBinsert(hr, item); /* Enforce left-leaning condition */ if (hr->red && !hl->red) h = rotL(h); if (hl->red && hll->red) h = rotR(h); if (hl->red && hr->red) colorFlip(h); return fixNr(h); }
static link LLRBinsert(link h, EnglishWord newWord){ Key v = makeKey(newWord); int val = 0; if (compareNodes(h,z)) return NEW(newWord, z, z, 1); val = compareKeys(v,makeKey(h->englishWord)); if ( val < 0) hl = LLRBinsert(hl, newWord); else hr = LLRBinsert(hr, newWord); if (hr->red && !hl->red) h = rotL(h); if (hl->red && hll->red) h = rotR(h); if (hl->red && hr->red) colorFlip(h); return h; }
link STinsert(link head, Item *item) { head = LLRBinsert(head, item); head->red = 0; return head; }
static void STinsert(EnglishWord newWord){ head = LLRBinsert(head, newWord); head->red = 0; }