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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#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·rmchild(bio·Node* parent, bio·Node* child)
{
bio·Node *it, *prev;
enum {
error·nil,
error·notfound,
};
prev = nil;
for (it = parent->child[0]; it != nil && it != child; it = it->sibling) {
prev = it;
}
if (it == nil) return error·notfound;
return error·nil;
}
// -----------------------------------------------------------------------
// subtree statistics
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 editing
error
phylo·ladderize(bio·Node *root)
{
int i;
error err;
bio·Node *child;
double dists[50];
if (!root->nchild) return 0;
Assert(root->nchild < arrlen(dists));
for (i = 0, child = root->child[0]; child != nil; child = child->sibling, i++) {
if (err = phylo·ladderize(child), err) {
errorf("ladderize: failure at '%s'", child->name);
return 1;
}
dists[i] = child->dist;
}
return 0;
}
|