-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.cpp
242 lines (215 loc) · 7.21 KB
/
node.cpp
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
/***************************************************************************
* Copyright (C) 2006 by BUI Quang Minh, Steffen Klaere, Arndt von Haeseler *
* *
* 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. *
***************************************************************************/
#include "node.h"
//#include <sys/time.h>
//#include <time.h>
#include <math.h>
//#define INFINITY 1000000000
/*********************************************
class Node
*********************************************/
Node::Node(int aid) {
id = aid;
//name = NULL;
height = -1;
}
Node::Node(int aid, int aname) {
id = aid;
char str[20];
sprintf(str, "%d", aname);
name = str;
height = -1;
}
Node::Node(int aid, const char *aname) {
id = aid;
if (aname)
name = aname;
height = -1;
}
bool Node::isLeaf() {
return neighbors.size() <= 1;
}
bool Node::isInCherry() {
if (this->isLeaf()) {
if (neighbors[0]->node->isCherry()) {
return true;
} else {
return false;
}
} else {
return false;
}
}
bool Node::isCherry() {
int num_leaves = 0;
for (NeighborVec::iterator it = neighbors.begin(); it != neighbors.end(); it++)
if ((*it)->node->isLeaf()) num_leaves++;
return (num_leaves > 1);
}
int Node::degree() {
return neighbors.size();
}
/** 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 *Node::calcHeight(Node *dad) {
if (isLeaf() && dad != NULL) {
// if a leaf, but not the root
height = 0;
highestNei = NULL;
return this;
}
// scan through all children
height = -INFINITY;
Node *lowestLeaf = NULL;
for (NeighborVec::iterator it = neighbors.begin(); it != neighbors.end(); it++)
if ((*it)->node != dad) {
Node *leaf = (*it)->node->calcHeight(this);
if ((*it)->node->height + (*it)->length > height) {
height = (*it)->node->height + (*it)->length;
highestNei = (*it);
lowestLeaf = leaf;
}
}
return lowestLeaf;
}
int Node::calDist(Node* partner, Node* dad, int curLen) {
if ( this->isLeaf() && this != partner && dad != NULL )
return 0;
if ( this->isLeaf() && dad == NULL ) {
return this->neighbors[0]->node->calDist(partner, this, 1);
} else {
Node* left = NULL;
Node* right = NULL;
for (NeighborVec::iterator it = neighbors.begin(); it != neighbors.end(); it++) {
if ((*it)->node != dad) {
if (left == NULL)
left = (*it)->node;
else
right = (*it)->node;
}
}
curLen++;
// cout << left->id << endl;
// cout << right->id << endl;
int sumLeft = 0;
int sumRight = 0;
if ( left->isLeaf() ) {
if ( left == partner)
{
//cout << " I found you baby" << endl;
return curLen;
}
}
else {
sumLeft = left->calDist(partner, this, curLen);
}
if ( right->isLeaf() ) {
if ( right == partner) {
//cout << " I found you baby" << endl;
return curLen;
}
}
else {
sumRight = right->calDist(partner, this, curLen);
}
return sumRight + sumLeft;
}
}
/**
efficient longest path algorithm
*/
double Node::longestPath2(Node* &node1, Node* &node2) {
// step 1: find the farthest leaf from this node (as a leaf)
assert(isLeaf());
node1 = calcHeight();
// step 2: find the farthest leaf from node1
node2 = node1->calcHeight();
return node1->height;
}
Neighbor *Node::findNeighbor(Node *node) {
int size = neighbors.size();
for (int i = 0; i < size; i++)
if (neighbors[i]->node == node) return neighbors[i];
/*
for (NeighborVec::iterator it = neighbors.begin(); it != neighbors.end(); it ++)
if ((*it)->node == node)
return (*it);*/
cout << "ERROR : Could not find neighbors of node " << node->id << endl;
assert(0);
return NULL;
}
NeighborVec::iterator Node::findNeighborIt(Node *node) {
for (NeighborVec::iterator it = neighbors.begin(); it != neighbors.end(); it++)
if ((*it)->node == node)
return it;
assert(0);
return neighbors.end();
}
void Node::addNeighbor(Node *node, double length, int id) {
neighbors.push_back(new Neighbor(node, length, id));
}
void Node::updateNeighbor(NeighborVec::iterator nei_it, Neighbor *newnei) {
assert(nei_it != neighbors.end());
*nei_it = newnei;
}
void Node::updateNeighbor(NeighborVec::iterator nei_it, Neighbor *newnei, double newlen) {
assert(nei_it != neighbors.end());
*nei_it = newnei;
newnei->length = newlen;
}
void Node::updateNeighbor(Node *node, Neighbor *newnei) {
NeighborVec::iterator nei_it = findNeighborIt(node);
assert(nei_it != neighbors.end());
*nei_it = newnei;
}
void Node::updateNeighbor(Node *node, Neighbor *newnei, double newlen) {
NeighborVec::iterator nei_it = findNeighborIt(node);
assert(nei_it != neighbors.end());
*nei_it = newnei;
newnei->length = newlen;
}
void Node::updateNeighbor(Node* node, Node *newnode, double newlen) {
for (NeighborVec::iterator it = neighbors.begin(); it != neighbors.end(); it++)
if ((*it)->node == node) {
(*it)->node = newnode;
(*it)->length = newlen;
break;
}
}
double Node::updateNeighbor(Node* node, Node *newnode) {
for (NeighborVec::iterator it = neighbors.begin(); it != neighbors.end(); it++)
if ((*it)->node == node) {
(*it)->node = newnode;
return (*it)->length;
}
return -1;
}
void Node::deleteNode() {
NeighborVec::reverse_iterator it;
for (it = neighbors.rbegin(); it != neighbors.rend(); it++)
delete (*it);
neighbors.clear();
}
Node::~Node() {
deleteNode();
}