-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.h
381 lines (313 loc) · 9.53 KB
/
node.h
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/***************************************************************************
* Copyright (C) 2006 by BUI Quang Minh, Steffen Klaere, Arndt von Haeseler *
* minh.bui@univie.ac.at *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef NODE_H
#define NODE_H
#include <vector>
#include <string>
#include <set>
#include <map>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
//#include <sys/time.h>
//#include <time.h>
#include <math.h>
#include "ncl/ncl.h"
#include "tools.h"
using namespace std;
/*--------------------------------------------------------------*/
class Node;
/**
Neighbor list of a node in the tree
*/
class Neighbor {
public:
/**
the other end of the branch
*/
Node *node;
/**
branch length
*/
double length;
/**
branch ID
*/
int id;
/**
construct class with a node and length
@param anode the other end of the branch
@param alength length of branch
*/
Neighbor(Node *anode, double alength) {
node = anode;
length = alength;
id = -1;
}
/**
construct class with a node and length
@param anode the other end of the branch
@param alength length of branch
@param id branch ID
*/
Neighbor(Node *anode, double alength, int aid) {
node = anode;
length = alength;
id = aid;
}
/**
construct class with another Neighbor
@param nei another Neighbor
*/
Neighbor(Neighbor *nei) {
node = nei->node;
length = nei->length;
id = nei->id;
}
/**
destructor
*/
virtual ~Neighbor() {
}
};
/**
Neighbor vector
*/
typedef vector<Neighbor*> NeighborVec;
/**
Node vector
*/
typedef vector<Node*> NodeVector;
/*--------------------------------------------------------------*/
/*--------------------------------------------------------------*/
/**
A Node in the tree
@author BUI Quang Minh, Steffen Klaere, Arndt von Haeseler
*/
class Node {
public:
/**
node id.
*/
int id;
/**
node name
*/
string name;
/**
list of neighbors
*/
NeighborVec neighbors;
/**
the height of subtree rooted at this node, used for greedy algorithm
*/
double height;
/**
child of maximal height of subtree rooted at this node, used for greedy algorithm
*/
Neighbor *highestNei;
/**
* List of closest leaves to the current node.
*/
NodeVector closestLeaves;
/**
constructor
*/
Node() {
id = -1;
height = -1;
};
/**
constructor
@param aid id of this node
*/
Node(int aid);
/**
constructor
@param aid id of this node
@param aname name of this node
*/
Node(int aid, int aname);
/**
constructor
@param aid id of this node
@param aname name of this node
*/
Node(int aid, const char *aname);
/**
destructor
*/
virtual ~Node();
/**
used for the destructor
*/
virtual void deleteNode();
/**
@return true of this is a leaf
*/
bool isLeaf();
/**
* @return TRUE if this node is a leaf in a cherry
*/
bool isInCherry();
/**
@return TRUE if this node is a cherry, FALSE otherwise
*/
bool isCherry();
/**
@return the number of adjacent nodes
*/
int degree();
/** calculate the height of the subtree rooted at this node,
given the dad. Also return the lowestLeaf.
@param dad the dad of this node
@return the leaf at the lowest level. Also modify the height, highestNei of this class.
*/
Node *calcHeight(Node *dad = NULL);
/**
* Calculate the distance between 2 nodes. Only for binary tree.
* @param parner the other node
* @return the distance
*/
int calDist(Node *parner, Node *dad = NULL, int curLen = 0);
/** calculate the longest path in the subtree (version 2: more efficient)
@param node1 the returned node1 of the one end of the path
@param node2 the returned node2 of the one end of the path
@return the length of the longest path
*/
double longestPath2(Node* &node1, Node* &node2);
/**
@param node the target node
@return the iterator to the neighbor that has the node. If not found, return NULL
*/
Neighbor *findNeighbor(Node *node);
/**
@param node the target node
@return the iterator to the neighbor that has the node. If not found, return neighbors.end()
*/
NeighborVec::iterator findNeighborIt(Node *node);
/**
update the neighbor node with the newnode
@param node old neighbor node
@param newnode new neighbor node
@param newlen new length applied for the corresponding branch
*/
void updateNeighbor(Node* node, Node *newnode, double newlen);
/**
update the neighbor node with the newnode
@param node old neighbor node
@param newnode new neighbor node
@return length applied for the corresponding branch
*/
double updateNeighbor(Node* node, Node *newnode);
/**
update the neighbor node with the newnode
@param nei_it iterator to the neighbor
@param newnei new neighbor
*/
void updateNeighbor(NeighborVec::iterator nei_it, Neighbor *newnei);
/**
update the neighbor node with the newnode
@param nei_it iterator to the neighbor
@param newnei new neighbor
@param newlen new branch length
*/
void updateNeighbor(NeighborVec::iterator nei_it, Neighbor *newnei, double newlen);
/**
update the neighbor node with the newnode
@param node old neighbor node
@param newnei new neighbor
*/
void updateNeighbor(Node *node, Neighbor *newnei);
/**
update the neighbor node with the newnode
@param node old neighbor node
@param newnei new neighbor
@param newlen new branch length
*/
void updateNeighbor(Node *node, Neighbor *newnei, double newlen);
/**
add a neighbor
@param node the neighbor node
@param length branch length
@param id branch ID
*/
virtual void addNeighbor(Node *node, double length, int id = -1);
};
/*
some macros to transverse neighbors of a node
*/
#define FOR_NEIGHBOR(mynode, mydad, it) \
for (it = (mynode)->neighbors.begin(); it != (mynode)->neighbors.end(); it++) \
if ((*it)->node != (mydad))
#define FOR_NEIGHBOR_IT(mynode, mydad, it) \
for (NeighborVec::iterator it = (mynode)->neighbors.begin(); it != (mynode)->neighbors.end(); it++) \
if ((*it)->node != (mydad))
#define FOR_NEIGHBOR_DECLARE(mynode, mydad, it) \
NeighborVec::iterator it; \
for (it = (mynode)->neighbors.begin(); it != (mynode)->neighbors.end(); it++) \
if ((*it)->node != (mydad))
/*--------------------------------------------------------------*/
/*--------------------------------------------------------------*/
/**
nodecmp, for pruning algorithm
*/
struct nodecmp {
/**
nodecmp, for pruning algorithm
*/
bool operator()(const Node* s1, const Node* s2) const {
return (s1->neighbors[0]->length) < (s2->neighbors[0]->length);
}
};
inline int nodenamecmp(const Node* a, const Node* b) {
return (a->name < b->name);
}
/**
set of leaves, sorted in ascending order by the length of the incident branch.
For pruning algorithm
*/
typedef multiset<Node*, nodecmp> LeafSet;
/*--------------------------------------------------------------*/
/*--------------------------------------------------------------*/
/**
map from leaf name to node class
*/
typedef map<const string, Node*> LeafMapName;
/*--------------------------------------------------------------*/
/*--------------------------------------------------------------*/
/**
neighborcmp, for greedy algorithm
*/
struct neighborcmp {
/**
neighborcmp, for greedy algorithm
*/
bool operator()(const Neighbor* s1, const Neighbor* s2) const {
return ((s1->length + s1->node->height) > (s2->length + s2->node->height));
}
};
/**
set of branches, sorted in descending order by the height of the corresponding subtree.
For greedy algorithm.
*/
typedef multiset<Neighbor*, neighborcmp> NeighborSet;
#endif