-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
446 lines (424 loc) · 9.7 KB
/
tree.cpp
File metadata and controls
446 lines (424 loc) · 9.7 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <string>
#include <vector>
#include <map>
#include "tree.h"
using std::string;
using std::cout;
using std::endl;
//--------------------------------------------------
// constructors
tree::tree(): mu(0.0),v(0),c(0),p(0),l(0),r(0) {}
tree::tree(double m): mu(m),v(0),c(0),p(0),l(0),r(0) {}
tree::tree(const tree& n): mu(0.0),v(0),c(0),p(0),l(0),r(0) { cp(this,&n); }
//--------------------------------------------------
//operators
tree& tree::operator=(const tree& rhs)
{
if(&rhs != this) {
tonull(); //kill left hand side (this)
cp(this,&rhs); //copy right hand side to left hand side
}
return *this;
}
//--------------------------------------------------
//public functions
// find bottom node pointer given x
//--------------------
tree::tree_cp tree::bn(double *x,xinfo& xi)
{
if(l==0) return this; //bottom node
if(x[v] < xi[v][c]) {
return l->bn(x,xi);
} else {
return r->bn(x,xi);
}
}
//--------------------
//find region for a given variable
void tree::rg(size_t v, int* L, int* U)
{
if(p==0) { //no parent
return;
}
if(p->v == v) { //does my parent use v?
if(this == p->l) { //am I left or right child
if((int)(p->c) <= (*U)) *U = (p->c)-1;
} else {
if((int)(p->c) >= *L) *L = (p->c)+1;
}
}
p->rg(v,L,U);
}
//--------------------
//tree size
size_t tree::treesize() const
{
if(l==0) return 1; //if bottom node, tree size is 1
else return (1+l->treesize()+r->treesize());
}
//--------------------
size_t tree::nnogs() const
{
if(!l) return 0; //bottom node
if(l->l || r->l) { //not a nog
return (l->nnogs() + r->nnogs());
} else { //is a nog
return 1;
}
}
size_t tree::nuse(size_t v)
{
npv nds;
this->getnodes(nds);
size_t nu=0; //return value
for(size_t i=0;i!=nds.size();i++) {
if(nds[i]->l && nds[i]->v==v) nu+=1;
}
return nu;
}
//--------------------
size_t tree::nbots() const
{
if(l==0) { //if a bottom node
return 1;
} else {
return l->nbots() + r->nbots();
}
}
//--------------------
//depth of node
size_t tree::depth() const
{
if(!p) return 0; //no parents
else return (1+p->depth());
}
//--------------------
// node id
size_t tree::nid() const
//recursion up the tree
{
if(!p) return 1; //if you don't have a parent, you are the top
if(this==p->l) return 2*(p->nid()); //if you are a left child
else return 2*(p->nid())+1; //else you are a right child
}
//--------------------
//node type
char tree::ntype() const
{
//t:top, b:bottom, n:no grandchildren, i:internal
if(!p) return 't';
if(!l) return 'b';
if(!(l->l) && !(r->l)) return 'n';
return 'i';
}
//--------------------
//get bottom nodes
//recursion down the tree
void tree::getbots(npv& bv)
{
if(l) { //have children
l->getbots(bv);
r->getbots(bv);
} else {
bv.push_back(this);
}
}
//--------------------
//get nog nodes
//recursion down the tree
void tree::getnogs(npv& nv)
{
if(l) { //have children
if((l->l) || (r->l)) { //have grandchildren
if(l->l) l->getnogs(nv);
if(r->l) r->getnogs(nv);
} else {
nv.push_back(this);
}
}
}
//--------------------
//get all nodes
//recursion down the tree
void tree::getnodes(npv& v)
{
v.push_back(this);
if(l) {
l->getnodes(v);
r->getnodes(v);
}
}
void tree::getnodes(cnpv& v) const
{
v.push_back(this);
if(l) {
l->getnodes(v);
r->getnodes(v);
}
}
//--------------------
//add children to bot node nid
bool tree::birth(size_t nid,size_t v, size_t c, double ml, double mr)
{
tree_p np = getptr(nid);
if(np==0) {
cout << "error in birth: bottom node not found\n";
return false; //did not find note with that nid
}
if(np->l) {
cout << "error in birth: found node has children\n";
return false; //node is not a bottom node
}
//add children to bottom node np
tree_p l = new tree;
l->mu=ml;
tree_p r = new tree;
r->mu=mr;
np->l=l;
np->r=r;
np->v = v; np->c=c;
l->p = np;
r->p = np;
return true;
}
//--------------------
//is the node a nog node
bool tree::isnog() const
{
bool isnog=true;
if(l) {
if(l->l || r->l) isnog=false; //one of the children has children.
} else {
isnog=false; //no children
}
return isnog;
}
//--------------------
//kill children of nog node nid
bool tree::death(size_t nid, double mu)
{
tree_p nb = getptr(nid);
if(nb==0) {
cout << "error in death, nid invalid\n";
return false;
}
if(nb->isnog()) {
delete nb->l;
delete nb->r;
nb->l=0;
nb->r=0;
nb->v=0;
nb->c=0;
nb->mu=mu;
return true;
} else {
cout << "error in death, node is not a nog node\n";
return false;
}
}
//--------------------
//add children to bot node *np
void tree::birthp(tree_p np,size_t v, size_t c, double ml, double mr)
{
tree_p l = new tree;
l->mu=ml;
tree_p r = new tree;
r->mu=mr;
np->l=l;
np->r=r;
np->v = v; np->c=c;
l->p = np;
r->p = np;
}
//--------------------
//kill children of nog node *nb
void tree::deathp(tree_p nb, double mu)
{
delete nb->l;
delete nb->r;
nb->l=0;
nb->r=0;
nb->v=0;
nb->c=0;
nb->mu=mu;
}
//--------------------
//print out tree(pc=true) or node(pc=false) information
//uses recursion down
void tree::pr(bool pc) const
{
size_t d = depth();
size_t id = nid();
size_t pid;
if(!p) pid=0; //parent of top node
else pid = p->nid();
string pad(2*d,' ');
string sp(", ");
if(pc && (ntype()=='t'))
cout << "tree size: " << treesize() << endl;
//cout << pad << "(id,parent): " << id << sp << pid;
cout << pad << "id: " << id;
cout << sp << "(v,c): " << v << sp << c;
cout << sp << "mu: " << mu;
cout << sp << "type: " << ntype();
cout << sp << "depth: " << depth();
//cout << sp << "pointer: " << this << endl;
cout << endl;
if(pc) {
if(l) {
l->pr(pc);
r->pr(pc);
}
}
}
//--------------------------------------------------
//private functions
//--------------------
//copy tree o to tree n
void tree::cp(tree_p n, tree_cp o)
//assume n has no children (so we don't have to kill them)
//recursion down
{
if(n->l) {
cout << "cp:error node has children\n";
return;
}
n->mu = o->mu;
n->v = o->v;
n->c = o->c;
if(o->l) { //if o has children
n->l = new tree;
(n->l)->p = n;
cp(n->l,o->l);
n->r = new tree;
(n->r)->p = n;
cp(n->r,o->r);
}
}
//--------------------
//cut back to one node
void tree::tonull()
{
size_t ts = treesize();
while(ts>1) { //if false ts=1
npv nv;
getnogs(nv);
for(size_t i=0;i<nv.size();i++) {
delete nv[i]->l;
delete nv[i]->r;
nv[i]->l=0;
nv[i]->r=0;
}
ts = treesize();
}
mu=0.0;
v=0;c=0;
p=0;l=0;r=0;
}
//--------------------------------------------------
//functions
//--------------------
//output operator
std::ostream& operator<<(std::ostream& os, const tree& t)
{
tree::cnpv nds;
t.getnodes(nds);
os << nds.size() << endl;
for(size_t i=0;i<nds.size();i++) {
os << nds[i]->nid() << " ";
os << nds[i]->getv() << " ";
os << nds[i]->getc() << " ";
os << nds[i]->getm() << endl;
}
return os;
}
//--------------------
//input operator
std::istream& operator>>(std::istream& is, tree& t)
{
size_t tid,pid; //tid: id of current node, pid: parent's id
std::map<size_t,tree::tree_p> pts; //pointers to nodes indexed by node id
size_t nn; //number of nodes
t.tonull(); // obliterate old tree (if there)
//read number of nodes----------
is >> nn;
if(!is) {
//cout << ">> error: unable to read number of nodes" << endl;
return is;
}
//read in vector of node information----------
std::vector<node_info> nv(nn);
for(size_t i=0;i!=nn;i++) {
is >> nv[i].id >> nv[i].v >> nv[i].c >> nv[i].m;
if(!is) {
//cout << ">> error: unable to read node info, on node " << i+1 << endl;
return is;
}
}
//first node has to be the top one
pts[1] = &t; //careful! this is not the first pts, it is pointer of id 1.
t.setv(nv[0].v); t.setc(nv[0].c); t.setm(nv[0].m);
t.p=0;
//now loop through the rest of the nodes knowing parent is already there.
for(size_t i=1;i!=nv.size();i++) {
tree::tree_p np = new tree;
np->v = nv[i].v; np->c=nv[i].c; np->mu=nv[i].m;
tid = nv[i].id;
pts[tid] = np;
pid = tid/2;
// set pointers
if(tid % 2 == 0) { //left child has even id
pts[pid]->l = np;
} else {
pts[pid]->r = np;
}
np->p = pts[pid];
}
return is;
}
std::ostream& operator<<(std::ostream& os, const xinfo& xi)
{
os << xi.size() << endl;
for(size_t i=0;i<xi.size();i++) {
os << xi[i].size() << endl;
for(size_t j=0;j<xi[i].size();j++)
os << xi[i][j];
os << endl;
}
return os;
}
std::istream& operator>>(std::istream& is, xinfo& xi)
{
size_t xin;
size_t vecdn;
xi.resize(0); // reset old xinfo (if there)
is >> xin;
if(!is) {
//cout << ">> error: unable to read size of xinfo" << endl;
return is;
}
std::vector<double> vec_d;
double vecdelem;
for(size_t i=0;i<xin;i++) {
is >> vecdn;
for(size_t j=0;j<vecdn;j++) {
is >> vecdelem;
vec_d.push_back(vecdelem);
}
xi.push_back(vec_d);
vec_d.resize(0);
}
return is;
}
//--------------------
// get pointer for node from its nid
tree::tree_p tree::getptr(size_t nid)
{
if(this->nid() == nid) return this; //found it
if(l==0) return 0; //no children, did not find it
tree_p lp = l->getptr(nid);
if(lp) return lp; //found on left
tree_p rp = r->getptr(nid);
if(rp) return rp; //found on right
return 0; //never found it
}