aboutsummaryrefslogtreecommitdiff
path: root/sys/libbio/phylo.c
blob: c1ace7ac415234af66c4db3fbab5ff07c9fd118f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <u.h>
#include <libn.h>
#include <libbio.h>

// -----------------------------------------------------------------------
// subtree manipulation methods

error
phylo·addchild(bio·Node* parent, bio·Node* child)
{
    bio·Node *it, *sibling;
    switch (parent->nchild) {
    case 1:
        parent->child[0]->sibling = child;
    case 0:
        parent->child[parent->nchild++] = child;
    break;

    default:
        sibling = parent->child[1];
        for (it = parent->child[1]->sibling; it != nil; it = it->sibling) {
            sibling = it;
        }
        sibling->sibling = child;
        parent->nchild++;
    }

    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