File: treefunc.c

package info (click to toggle)
treetool 2.0.2a-4
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 1,172 kB
  • ctags: 2,694
  • sloc: ansic: 26,504; makefile: 215
file content (303 lines) | stat: -rw-r--r-- 5,027 bytes parent folder | download | duplicates (5)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <stdio.h>

#ifdef MEMDBG
#include "memdbg.h"
#endif

#include "treestruct.h"

treenode parent(n)
treenode n;
{
	list old;
	treebranch branch;
	treenode p=NULL;

	if(notnode(n))
		return(NULL);

	old=listnode(n->branches);

	startlist(n->branches);
	while((branch=listnext(n->branches))!=NULL)
		if(branch->up!=n)
		{
			p=branch->up;
			break;
		}
	setnode(n->branches, old);
	return(p);
}

int treeleaf(n)
/* returns 1 if it's a leaf node */
/* true if all branches point to parents */
treenode n;
{
	treebranch b;

	startlist(n->branches);
	while((b=listnext(n->branches))!=NULL)
		if(b->up==n)
			return(0);
	return(1);
}

int treechildren(n)
/* returns number of descendants of node n */
treenode n;
{
	static int i;
	static treebranch b;

	startlist(n->branches);
	i=0;
	while((b=listnext(n->branches))!=NULL)
		if(b->down!=n)
			i++;
	return(i);
}

int treeleaves(n)
/* returns number of leaves under node n */
treenode n;
{
	int i=0;
	list c;

	tforsubtree(n->branches, c)
	{
		if(treeleaf(subtree(c)))
			i++;
		else
			i+=treeleaves(subtree(c));
	}
	return(i);
}

treebranch getparentandbranch(n, p)
/* like parent, but returns 0 only if no parent pointer exists */
treenode n, *p;
{
	list old;
	treebranch branch;
	treebranch result=NULL;

	if(notnode(n))
		return(NULL);

	old=listnode(n->branches);

	startlist(n->branches);
	while((branch=listnext(n->branches))!=NULL)
		if(branch->up!=n)
		{
			*p=branch->up;
			result=branch;
			break;
		}
	setnode(n->branches, old);
	return(result);
}

treebranch branchtonode1(n, l, up)
/* at node n, looking for node l, came from node up */
treenode n, l, up;
{
	list old=listnode(n->branches);
	treebranch found=NULL;
	treenode tmp;
	treebranch branch;

	startlist(n->branches);
	while(found==NULL && (branch=listnext(n->branches))!=NULL)
	{
		tmp=othernode(n, branch);
		if(tmp==l)
			found=branch;
		else if(tmp!=up && tmp!=NULL)
		{
			if(branchtonode1(tmp, l, n)!=NULL)
				found=branch;
		}
	}
	setnode(n->branches, old);
	return(branch);
}

treebranch branchtonode(a, b)
treenode a, b;
/* returns the branch that heads off from a in the direction of b */
/* returns NULL if they aren't in the same tree */
{
	if(notnode(a) || notnode(b))
		return(NULL);
	
	if(tree_get_tree(a) != tree_get_tree(b))
		return(NULL);
	
	return(branchtonode1(a, b, NULL));
}

int ascendant(a, b)
/* returns 1 if a is the ascendant of b */
treenode a, b;
{
	treenode tmp;

	if(notnode(a) || notnode(b))
		return(0);
	
	if(tree_get_tree(a) != tree_get_tree(b))
		return(0);
	
	tmp=b;
	do
	{
		tmp=parent(tmp);
		if(tmp==a)
			return(1);
	} while(tmp!=NULL);
	return(0);
}

int descendant(a, b)
/* returns 1 if a is the descendant of b */
treenode a, b;
{
	return(ascendant(b, a));
}

int parentdistance(n, distance)
treenode n;
double *distance;
{
	list old;
	treebranch branch;
	int result;

	if(notnode(n))
		return(NULL);

	old=listnode(n->branches);

	result=0;
	startlist(n->branches);
	while((branch=listnext(n->branches))!=NULL)
		if(branch->up!=n)
		{
			result=branch->specified;
			if(branch->specified)
				*distance=branch->distance;
			else
				*distance=tree_get_tree(branch)->unspecdist;
			break;
		}
	setnode(n->branches, old);
	return(result);
}

int branchdistance(b, dist)
treebranch b;
double *dist;
{
	int result;

	result=b->specified;
	if(b->specified)
		*dist=b->distance;
	else
		*dist=tree_get_tree(b)->unspecdist;
	return(result);
}

int setparentdistance(n, distance)
/* sets the distance from n to it's parent */
treenode n;
double distance;
{
	list old;
	treebranch branch;
	int result;

	if(notnode(n))
		return(NULL);

	old=listnode(n->branches);

	result=0;
	startlist(n->branches);
	while((branch=listnext(n->branches))!=NULL)
		if(branch->up!=n)
		{
			branch->specified=1;
			branch->distance=distance;
			result=1;
			break;
		}
	setnode(n->branches, old);
	return(result);
}

double searchdistance(n, up, l, sp)
/* search links from n for l, not following those that go to up or NULL */
/* recursively add up the distances */
treenode n, up, l;
int *sp;
{
	treenode o;
	treebranch b;
	double result;
	int tmp;

	*sp=0;
	result=0.0;
	startlist(n->branches);
	while((b=listnext(n->branches))!=NULL)
	{
		o=othernode(n, b);
		if(o==l)
		{
			*sp=1;
			if(b->specified)
				result=b->distance;
			else
				result=tree_get_tree(b)->unspecdist;
			break;
		}
		if(o!=NULL && o!=up)
		{
			if(b->specified)
				result=b->distance+searchdistance(o, n, l, &tmp);
			else
				result=tree_get_tree(b)->unspecdist+
					searchdistance(o, n, l, &tmp);
			if(tmp)
			{
				*sp=1;
				break;
			}
		}
	}
	return(result);
}

int nodedistance(a, b, distance)
/* returns the distance from a to b.  returns 1 iff a and b are on same tree,
	and distance is specified */
/* returns -1:not found, 0: not specified, 1: specified */
treenode a, b;
double *distance;
{
	int result;
	double d;

	if(notnode(a) || notnode(b))
		return(0);
	if(tree_get_tree(a) != tree_get_tree(b))
		return(0);
	
	d=searchdistance(a, NULL, b, &result);
	*distance=d;
	return(result);
}