aboutsummaryrefslogtreecommitdiff
path: root/sys/libbio/phylo.c
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-04-28 11:59:59 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-04-28 11:59:59 -0700
commit4eabf5d72c6b01bbf11180280ef9d28d5fe587bf (patch)
treec113ac3eb4244564c90d8e22fe2da8cc0407949e /sys/libbio/phylo.c
parent8c8c6a9253ec2876f67217a8f10755b4ad7f3ec1 (diff)
feat: added number of nodes & leafs to tree data structure
Diffstat (limited to 'sys/libbio/phylo.c')
-rw-r--r--sys/libbio/phylo.c43
1 files changed, 43 insertions, 0 deletions
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 <libn.h>
#include <libbio.h>
+// -----------------------------------------------------------------------
+// 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