From 4eabf5d72c6b01bbf11180280ef9d28d5fe587bf Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 28 Apr 2020 11:59:59 -0700 Subject: feat: added number of nodes & leafs to tree data structure --- sys/libbio/phylo.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'sys/libbio/phylo.c') diff --git a/sys/libbio/phylo.c b/sys/libbio/phylo.c index 374cd08..c5fd3ed 100644 --- a/sys/libbio/phylo.c +++ b/sys/libbio/phylo.c @@ -2,6 +2,9 @@ #include #include +// ----------------------------------------------------------------------- +// subtree manipulation methods + error phylo·addchild(bio·Node* parent, bio·Node* child) { @@ -25,3 +28,43 @@ phylo·addchild(bio·Node* parent, bio·Node* child) child->parent = parent; return 0; } + +error +phylo·countnodes(bio·Node *node, int *n) +{ + error err; + bio·Node *child; + + *n += 1; + for (child = node->child[0]; child != nil; child = child->sibling) { + if (err = phylo·countnodes(child, n), err) { + errorf("node count: failure at '%s'", child->name); + return 1; + } + } + + return 0; +} + +error +phylo·countleafs(bio·Node *node, int *n) +{ + error err; + bio·Node *child; + + if (node->nchild) { + *n += 1; + } + + for (child = node->child[0]; child != nil; child = child->sibling) { + if (err = phylo·countleafs(child, n), err) { + errorf("leaf count: failure at '%s'", child->name); + return 1; + } + } + + return 0; +} + +// ----------------------------------------------------------------------- +// tree statistics -- cgit v1.2.1