From a6d9c8be245ef171423ddeb8466a27ad715012e0 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 28 Apr 2020 14:58:19 -0700 Subject: fix: finished remove child function --- sys/libbio/phylo.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'sys/libbio/phylo.c') diff --git a/sys/libbio/phylo.c b/sys/libbio/phylo.c index d03bc48..316537c 100644 --- a/sys/libbio/phylo.c +++ b/sys/libbio/phylo.c @@ -4,6 +4,8 @@ // ----------------------------------------------------------------------- // subtree manipulation methods +// NOTE: As of now these don't update nnode & nleaf stats. +// It is the caller's responsibility to refresh counts. error phylo·addchild(bio·Node* parent, bio·Node* child) @@ -26,22 +28,28 @@ SUCCESS: } error -phylo·rmchild(bio·Node* parent, bio·Node* child) +phylo·rmchild(bio·Node *parent, bio·Node *child) { bio·Node *it, *prev; enum { error·nil, error·notfound, + error·nochildren, }; prev = nil; - for (it = parent->child; it != nil && it != child; it = it->sibling) { + for (it = parent->child; it != nil; it = it->sibling) { + if (it == child) goto FOUND; prev = it; } - if (it == nil) return error·notfound; - - + return error·notfound; +FOUND: + if (prev == nil) { + parent->child = child->sibling; + } else { + prev->sibling = child->sibling; + } return error·nil; } -- cgit v1.2.1