From a463773c12aacb11bc370cddd053f6cc7d08e68a Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Fri, 19 Nov 2010 20:30:24 -0300 Subject: [PATCH] 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 --- qpkg/jrb.c | 87 +++++++++++++++++++++++++-------------------------- qpkg/rbtest.c | 4 +++ 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/qpkg/jrb.c b/qpkg/jrb.c index 71e84ae..2046cef 100644 --- a/qpkg/jrb.c +++ b/qpkg/jrb.c @@ -272,7 +272,7 @@ static void recolor(JRB n) JRB p, gp, s; int done = 0; - while(!done) { + while (!done) { if (isroot(n)) { setblack(n); return; @@ -360,8 +360,8 @@ static void single_rotate(JRB y, int l) void jrb_delete_node(JRB n) { - JRB s, p, gp; - char ir; + JRB s, p, gp, x, z; + char ir, il; if (isint(n)) { fprintf(stderr, "Cannot delete an internal node: %p\n", n); @@ -430,7 +430,7 @@ void jrb_delete_node(JRB n) n = s; p = n->parent; s = sibling(n); - while(isblack(p) && isblack(s) && isint(s) && + while (isblack(p) && isblack(s) && isint(s) && isblack(s->flink) && isblack(s->blink)) { setred(s); n = p; @@ -447,49 +447,46 @@ void jrb_delete_node(JRB n) s = sibling(n); } - { - JRB x, z; char il; - - if (isext(s)) { + if (isext(s)) { + fprintf(stderr, "DELETION ERROR: sibling not internal\n"); + exit(1); + } + + il = isleft(n); + x = il ? s->flink : s->blink; + z = sibling(x); + + if (isred(z)) { /* Rotation 2.3f */ + single_rotate(p, !il); + setblack(z); + if (isred(p)) + setred(s); + else + setblack(s); + setblack(p); + return; + } + if (isblack(x)) { /* Recoloring only (2.3c) */ + if (isred(s) || isblack(p)) { fprintf(stderr, - "DELETION ERROR: sibling not internal\n"); + "DELETION ERROR: 2.3c not quite right\n"); exit(1); } - - il = isleft(n); - x = il ? s->flink : s->blink; - z = sibling(x); - - if (isred(z)) { /* Rotation 2.3f */ - single_rotate(p, !il); - setblack(z); - if (isred(p)) - setred(s); - else - setblack(s); - setblack(p); - } else if (isblack(x)) { /* Recoloring only (2.3c) */ - if (isred(s) || isblack(p)) { - fprintf(stderr, - "DELETION ERROR: 2.3c not quite right\n"); - exit(1); - } - setblack(p); - setred(s); - return; - } else if (isred(p)) { /* 2.3d */ - single_rotate(s, il); - single_rotate(p, !il); - setblack(x); - setred(s); - return; - } else { /* 2.3e */ - single_rotate(s, il); - single_rotate(p, !il); - setblack(x); - return; - } + setblack(p); + setred(s); + return; } + if (isred(p)) { /* 2.3d */ + single_rotate(s, il); + single_rotate(p, !il); + setblack(x); + setred(s); + return; + } + /* 2.3e */ + single_rotate(s, il); + single_rotate(p, !il); + setblack(x); } @@ -503,7 +500,7 @@ int jrb_nblack(JRB n) exit(1); } nb = 0; - while(!ishead(n)) { + while (!ishead(n)) { if (isblack(n)) nb++; n = n->parent; } @@ -537,7 +534,7 @@ void jrb_free_tree(JRB n) exit(1); } - while(jrb_first(n) != jrb_nil(n)) + while (jrb_first(n) != jrb_nil(n)) jrb_delete_node(jrb_first(n)); free(n); diff --git a/qpkg/rbtest.c b/qpkg/rbtest.c index c7e62af..e0bed09 100644 --- a/qpkg/rbtest.c +++ b/qpkg/rbtest.c @@ -21,6 +21,7 @@ int main(void) JRB p; INSERT("ab", "have"); + INSERT("ac", "NOT"); INSERT("d", "little"); INSERT("c", "this"); INSERT("b", "passed"); @@ -29,6 +30,9 @@ int main(void) INSERT("aa", "We"); INSERT("ff", "!"); + p = jrb_find(tree, "ac", cmp); + jrb_delete_node(p); + jrb_traverse(p, tree) printf("%s ", (char *) jrb_val(p)); printf("\n");