static JRB jrb_insert_b(JRB n, Jval key, Jval val) { JRB newleft, newright, newnode, p; if (ishead(n)) { if (n->parent == n) { /* Tree is empty */ mk_new_ext(newnode, key, val); insert(newnode, n); n->parent = newnode; newnode->parent = n; setroot(newnode); return newnode; } else { mk_new_ext(newright, key, val); insert(newright, n); newleft = newright->blink; setnormal(newleft); mk_new_int(newleft, newright, newleft->parent, isleft(newleft)); p = rprev(newright); if (!ishead(p)) setlext(p, newright); return newright; } } else { mk_new_ext(newleft, key, val); insert(newleft, n); setnormal(n); mk_new_int(newleft, n, n->parent, isleft(n)); p = lprev(newleft); if (!ishead(p)) setrext(p, newleft); return newleft; } }
Rb_node rb_insert_b(Rb_node n, char *key, char *val) { Rb_node newleft, newright, newnode, p; if (ishead(n)) { if (n->p.root == n) { /* Tree is empty */ mk_new_ext(newnode, key, val); insert(newnode, n); n->p.root = newnode; newnode->p.parent = n; setroot(newnode); return newnode; } else { mk_new_ext(newright, key, val); insert(newright, n); newleft = newright->c.list.blink; setnormal(newleft); mk_new_int(newleft, newright, newleft->p.parent, isleft(newleft)); p = rprev(newright); if (!ishead(p)) p->k.lext = newright; return newright; } } else { mk_new_ext(newleft, key, val); insert(newleft, n); setnormal(n); mk_new_int(newleft, n, n->p.parent, isleft(n)); p = lprev(newleft); if (!ishead(p)) p->v.rext = newleft; return newleft; } }
static void mk_new_int(JRB l, JRB r, JRB p, int il) { JRB newnode; newnode = (JRB) calloc(1, sizeof(struct jrb_node)); setint(newnode); setred(newnode); setnormal(newnode); newnode->flink = l; newnode->blink = r; newnode->parent = p; setlext(newnode, l); setrext(newnode, r); l->parent = newnode; r->parent = newnode; setleft(l); setright(r); if (ishead(p)) { p->parent = newnode; setroot(newnode); } else if (il) { setleft(newnode); p->flink = newnode; } else { setright(newnode); p->blink = newnode; } recolor(newnode); }
static void mk_new_int(Rb_node l, Rb_node r, Rb_node p, int il) { Rb_node newnode; newnode = (Rb_node) malloc(sizeof(struct rb_node)); setint(newnode); setred(newnode); setnormal(newnode); newnode->c.child.left = l; newnode->c.child.right = r; newnode->p.parent = p; newnode->k.lext = l; newnode->v.rext = r; l->p.parent = newnode; r->p.parent = newnode; setleft(l); setright(r); if (ishead(p)) { p->p.root = newnode; setroot(newnode); } else if (il) { setleft(newnode); p->c.child.left = newnode; } else { setright(newnode); p->c.child.right = newnode; } recolor(newnode); }
static void single_rotate(JRB y, int l) { int rl, ir; JRB x, yp; char *tmp; ir = isroot(y); yp = y->parent; if (!ir) { rl = isleft(y); } if (l) { x = y->flink; y->flink = x->blink; setleft(y->flink); y->flink->parent = y; x->blink = y; setright(y); } else { x = y->blink; y->blink = x->flink; setright(y->blink); y->blink->parent = y; x->flink = y; setleft(y); } x->parent = yp; y->parent = x; if (ir) { yp->parent = x; setnormal(y); setroot(x); } else { if (rl) { yp->flink = x; setleft(x); } else { yp->blink = x; setright(x); } } }
static void single_rotate(Rb_node y, int l) { int rl, ir; Rb_node x, yp; ir = isroot(y); yp = y->p.parent; if (!ir) { rl = isleft(y); } if (l) { x = y->c.child.left; y->c.child.left = x->c.child.right; setleft(y->c.child.left); y->c.child.left->p.parent = y; x->c.child.right = y; setright(y); } else { x = y->c.child.right; y->c.child.right = x->c.child.left; setright(y->c.child.right); y->c.child.right->p.parent = y; x->c.child.left = y; setleft(y); } x->p.parent = yp; y->p.parent = x; if (ir) { yp->p.root = x; setnormal(y); setroot(x); } else { if (rl) { yp->c.child.left = x; setleft(x); } else { yp->c.child.right = x; setright(x); } } }