Date:2010-11-20 00:30:24 (9 years 23 days ago)
Author:Werner Almesberger
Commit:a463773c12aacb11bc370cddd053f6cc7d08e68a
Message:qpkg: simplify jrb_delete_node and more whitespace cleanup

- jrb.c (recolor, jrb_delete_node, jrb_nblack, jrb_free_tree): add space
to while(...
- jrb.c (jrb_delete_node): flatten deletion of internal node
- rbtest.c (main): add test of jrb_delete_node
Files: qpkg/jrb.c (6 diffs)
qpkg/rbtest.c (2 diffs)

Change Details

qpkg/jrb.c
272272    JRB p, gp, s;
273273    int done = 0;
274274
275    while(!done) {
275    while (!done) {
276276        if (isroot(n)) {
277277            setblack(n);
278278            return;
...... 
360360
361361void jrb_delete_node(JRB n)
362362{
363    JRB s, p, gp;
364    char ir;
363    JRB s, p, gp, x, z;
364    char ir, il;
365365
366366    if (isint(n)) {
367367        fprintf(stderr, "Cannot delete an internal node: %p\n", n);
...... 
430430    n = s;
431431    p = n->parent;
432432    s = sibling(n);
433    while(isblack(p) && isblack(s) && isint(s) &&
433    while (isblack(p) && isblack(s) && isint(s) &&
434434        isblack(s->flink) && isblack(s->blink)) {
435435        setred(s);
436436        n = p;
...... 
447447        s = sibling(n);
448448    }
449449
450    {
451        JRB x, z; char il;
452
453        if (isext(s)) {
454            fprintf(stderr,
455                "DELETION ERROR: sibling not internal\n");
456            exit(1);
457        }
450    if (isext(s)) {
451        fprintf(stderr, "DELETION ERROR: sibling not internal\n");
452        exit(1);
453    }
458454
459        il = isleft(n);
460        x = il ? s->flink : s->blink;
461        z = sibling(x);
462
463        if (isred(z)) { /* Rotation 2.3f */
464            single_rotate(p, !il);
465            setblack(z);
466            if (isred(p))
467                setred(s);
468            else
469                setblack(s);
470            setblack(p);
471        } else if (isblack(x)) { /* Recoloring only (2.3c) */
472            if (isred(s) || isblack(p)) {
473                fprintf(stderr,
474                    "DELETION ERROR: 2.3c not quite right\n");
475                exit(1);
476            }
477            setblack(p);
478            setred(s);
479            return;
480        } else if (isred(p)) { /* 2.3d */
481            single_rotate(s, il);
482            single_rotate(p, !il);
483            setblack(x);
455    il = isleft(n);
456    x = il ? s->flink : s->blink;
457    z = sibling(x);
458
459    if (isred(z)) { /* Rotation 2.3f */
460        single_rotate(p, !il);
461        setblack(z);
462        if (isred(p))
484463            setred(s);
485            return;
486        } else { /* 2.3e */
487            single_rotate(s, il);
488            single_rotate(p, !il);
489            setblack(x);
490            return;
464        else
465            setblack(s);
466        setblack(p);
467        return;
468    }
469    if (isblack(x)) { /* Recoloring only (2.3c) */
470        if (isred(s) || isblack(p)) {
471            fprintf(stderr,
472                "DELETION ERROR: 2.3c not quite right\n");
473            exit(1);
491474        }
475        setblack(p);
476        setred(s);
477        return;
492478    }
479    if (isred(p)) { /* 2.3d */
480        single_rotate(s, il);
481        single_rotate(p, !il);
482        setblack(x);
483        setred(s);
484    return;
485    }
486    /* 2.3e */
487    single_rotate(s, il);
488    single_rotate(p, !il);
489    setblack(x);
493490}
494491
495492
...... 
503500        exit(1);
504501    }
505502    nb = 0;
506    while(!ishead(n)) {
503    while (!ishead(n)) {
507504        if (isblack(n)) nb++;
508505        n = n->parent;
509506    }
...... 
537534        exit(1);
538535    }
539536
540    while(jrb_first(n) != jrb_nil(n))
537    while (jrb_first(n) != jrb_nil(n))
541538        jrb_delete_node(jrb_first(n));
542539
543540    free(n);
qpkg/rbtest.c
2121    JRB p;
2222
2323    INSERT("ab", "have");
24    INSERT("ac", "NOT");
2425    INSERT("d", "little");
2526    INSERT("c", "this");
2627    INSERT("b", "passed");
...... 
2930    INSERT("aa", "We");
3031    INSERT("ff", "!");
3132
33    p = jrb_find(tree, "ac", cmp);
34    jrb_delete_node(p);
35
3236    jrb_traverse(p, tree)
3337        printf("%s ", (char *) jrb_val(p));
3438    printf("\n");

Archive Download the corresponding diff file

Branches:
master



interactive