mirror of
git://projects.qi-hardware.com/wernermisc.git
synced 2024-11-25 21:15:56 +02:00
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
This commit is contained in:
parent
5a422a470d
commit
a463773c12
23
qpkg/jrb.c
23
qpkg/jrb.c
@ -360,8 +360,8 @@ static void single_rotate(JRB y, int l)
|
|||||||
|
|
||||||
void jrb_delete_node(JRB n)
|
void jrb_delete_node(JRB n)
|
||||||
{
|
{
|
||||||
JRB s, p, gp;
|
JRB s, p, gp, x, z;
|
||||||
char ir;
|
char ir, il;
|
||||||
|
|
||||||
if (isint(n)) {
|
if (isint(n)) {
|
||||||
fprintf(stderr, "Cannot delete an internal node: %p\n", n);
|
fprintf(stderr, "Cannot delete an internal node: %p\n", n);
|
||||||
@ -447,12 +447,8 @@ void jrb_delete_node(JRB n)
|
|||||||
s = sibling(n);
|
s = sibling(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
JRB x, z; char il;
|
|
||||||
|
|
||||||
if (isext(s)) {
|
if (isext(s)) {
|
||||||
fprintf(stderr,
|
fprintf(stderr, "DELETION ERROR: sibling not internal\n");
|
||||||
"DELETION ERROR: sibling not internal\n");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -468,7 +464,9 @@ void jrb_delete_node(JRB n)
|
|||||||
else
|
else
|
||||||
setblack(s);
|
setblack(s);
|
||||||
setblack(p);
|
setblack(p);
|
||||||
} else if (isblack(x)) { /* Recoloring only (2.3c) */
|
return;
|
||||||
|
}
|
||||||
|
if (isblack(x)) { /* Recoloring only (2.3c) */
|
||||||
if (isred(s) || isblack(p)) {
|
if (isred(s) || isblack(p)) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"DELETION ERROR: 2.3c not quite right\n");
|
"DELETION ERROR: 2.3c not quite right\n");
|
||||||
@ -477,19 +475,18 @@ void jrb_delete_node(JRB n)
|
|||||||
setblack(p);
|
setblack(p);
|
||||||
setred(s);
|
setred(s);
|
||||||
return;
|
return;
|
||||||
} else if (isred(p)) { /* 2.3d */
|
}
|
||||||
|
if (isred(p)) { /* 2.3d */
|
||||||
single_rotate(s, il);
|
single_rotate(s, il);
|
||||||
single_rotate(p, !il);
|
single_rotate(p, !il);
|
||||||
setblack(x);
|
setblack(x);
|
||||||
setred(s);
|
setred(s);
|
||||||
return;
|
return;
|
||||||
} else { /* 2.3e */
|
}
|
||||||
|
/* 2.3e */
|
||||||
single_rotate(s, il);
|
single_rotate(s, il);
|
||||||
single_rotate(p, !il);
|
single_rotate(p, !il);
|
||||||
setblack(x);
|
setblack(x);
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ int main(void)
|
|||||||
JRB p;
|
JRB p;
|
||||||
|
|
||||||
INSERT("ab", "have");
|
INSERT("ab", "have");
|
||||||
|
INSERT("ac", "NOT");
|
||||||
INSERT("d", "little");
|
INSERT("d", "little");
|
||||||
INSERT("c", "this");
|
INSERT("c", "this");
|
||||||
INSERT("b", "passed");
|
INSERT("b", "passed");
|
||||||
@ -29,6 +30,9 @@ int main(void)
|
|||||||
INSERT("aa", "We");
|
INSERT("aa", "We");
|
||||||
INSERT("ff", "!");
|
INSERT("ff", "!");
|
||||||
|
|
||||||
|
p = jrb_find(tree, "ac", cmp);
|
||||||
|
jrb_delete_node(p);
|
||||||
|
|
||||||
jrb_traverse(p, tree)
|
jrb_traverse(p, tree)
|
||||||
printf("%s ", (char *) jrb_val(p));
|
printf("%s ", (char *) jrb_val(p));
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user