/
Node.h
2480 lines (2261 loc) · 57.3 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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
Node.h
Data structure for a node of a binary tree
Contains methods to recursively work on a node's subtree
Copyright 2009-2014 Chris Whidden
cwhidden@dal.ca
http://kiwi.cs.dal.ca/Software/RSPR
March 3, 2014
Version 1.2.1
This file is part of rspr.
rspr 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 3 of the License, or
(at your option) any later version.
rspr 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 rspr. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#ifndef INCLUDE_NODE
#define INCLUDE_NODE
#define COPY_CONTRACTED
#include <cstdio>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <map>
#include <set>
#include "Forest.h"
using namespace std;
bool IGNORE_MULTI = false;
double REQUIRED_SUPPORT = 0.0;
struct StringCompare {
bool operator() (const string &a, const string &b) const {
return strcmp(a.c_str(), b.c_str()) < 0;
}
};
// representation of a component with no leaves
#define DEAD_COMPONENT "*"
//#define DEBUG_PROTECTED "@"
/*void find_sibling_pairs_hlpr(Node *node, list<Node *> &sibling_pairs);
void find_leaves_hlpr(Node *node, vector<Node *> &leaves);
void str_subtree_hlpr(string *s);
list<Node *> find_sibling_pairs(Node *node);
vector<Node *> find_leaves(Node *node);
*/
int stomini(string s);
class Forest;
class Node {
private:
//Node *lc; // left child
//Node *rc; // right child
list<Node *> children; // children
Node *p; // parent
list<Node *>:: iterator p_link; // location in parents list
Node *twin; // counterpart in another tree
string name; // label
int depth; //distance from root
int pre_num; // preorder number
int edge_pre_start;
int edge_pre_end;
int component_number;
list <Node *> active_descendants;
list <Node *> root_lcas;
list<list <Node *>::iterator> removable_descendants;
list <Node *>::iterator sibling_pair_loc;
int sibling_pair_status;
int num_clustered_children;
Forest *forest;
// TODO: contracted_list ?
Node *contracted_lc;
Node *contracted_rc;
bool contracted;
bool edge_protected;
int max_merge_depth;
bool allow_sibling;
int lost_children;
double support;
double support_normalization;
public:
Node() {
init(NULL, NULL, NULL, "", 0);
}
Node(string n) {
init(NULL, NULL, NULL, n, 0);
}
Node(string n, int d) {
init(NULL, NULL, NULL, n, d);
}
Node(Node *lc, Node *rc, Node *p, string n, int d) {
init(lc, rc, p, n, d);
}
void init(Node *lc, Node *rc, Node *p, string n, int d) {
// this->lc = lc;
// this->rc = rc;
this->p = p;
this->name = string(n);
this->twin = NULL;
this->depth = d;
this->pre_num = -1;
this->edge_pre_start = -1;
this->edge_pre_end = -1;
this->component_number = -2;
this->active_descendants = list <Node *>();
this->root_lcas = list <Node *>();
this->removable_descendants = list< list<Node *>::iterator>();
this->sibling_pair_loc = list<Node *>::iterator();
this->sibling_pair_status = 0;
this->num_clustered_children = 0;
this->forest = NULL;
this->contracted_lc = NULL;
this->contracted_rc = NULL;
this->contracted = false;
this->edge_protected = false;
this->allow_sibling = true;
this->lost_children = 0;
this->max_merge_depth = -1;
this->support = -1;
this->support_normalization = -1;
this->children = list<Node *>();
if (lc != NULL)
add_child(lc);
if (rc != NULL)
add_child(rc);
}
// copy constructor
Node(const Node &n) {
p = NULL;
name = n.name.c_str();
twin = n.twin;
depth = n.depth;
// depth = 0;
pre_num = n.pre_num;
edge_pre_start = n.edge_pre_start;
edge_pre_end = n.edge_pre_end;
component_number = n.component_number;
this->active_descendants = list <Node *>();
this->removable_descendants = list< list<Node *>::iterator>();
this->root_lcas = list <Node *>();
//sibling_pair_loc = n.sibling_pair_loc;
//sibling_pair_status = n.sibling_pair_status;
this->sibling_pair_loc = list<Node *>::iterator();
this->sibling_pair_status = 0;
this->num_clustered_children = 0;
this->forest = NULL;
list<Node *>::const_iterator c;
this->children = list<Node *>();
for(c = n.children.begin(); c != n.children.end(); c++) {
add_child(new Node(**c));
}
#ifdef COPY_CONTRACTED
if (n.contracted_lc == NULL)
contracted_lc = NULL;
else
contracted_lc = new Node(*(n.contracted_lc), this);
if (n.contracted_rc == NULL)
contracted_rc = NULL;
else
contracted_rc = new Node(*(n.contracted_rc), this);
this->contracted = n.contracted;
#else
this->contracted_lc = n.contracted_lc;
this->contracted_rc = n.contracted_rc;
this->contracted = n.contracted;
#endif
this->edge_protected = n.edge_protected;
this->allow_sibling = n.allow_sibling;
this->lost_children = n.lost_children;
this->max_merge_depth = n.max_merge_depth;
this->support = n.support;
this->support_normalization = n.support_normalization;
}
Node(const Node &n, Node *parent) {
p = parent;
name = n.name.c_str();
twin = n.twin;
if (p != NULL)
depth = p->depth+1;
else
depth = n.depth;
pre_num = n.pre_num;
edge_pre_start = n.edge_pre_start;
edge_pre_end = n.edge_pre_end;
component_number = n.component_number;
this->active_descendants = list <Node *>();
this->removable_descendants = list< list<Node *>::iterator>();
this->root_lcas = list <Node *>();
//sibling_pair_loc = n.sibling_pair_loc;
//sibling_pair_status = n.sibling_pair_status;
this->sibling_pair_loc = list<Node *>::iterator();
this->sibling_pair_status = 0;
this->num_clustered_children = 0;
this->forest = NULL;
this->children = list<Node *>();
list<Node *>::const_iterator c;
for(c = n.children.begin(); c != n.children.end(); c++) {
add_child(new Node(**c));
}
#ifdef COPY_CONTRACTED
if (n.contracted_lc == NULL)
contracted_lc = NULL;
else
contracted_lc = new Node(*(n.contracted_lc), this);
if (n.contracted_rc == NULL)
contracted_rc = NULL;
else
contracted_rc = new Node(*(n.contracted_rc), this);
this->contracted = n.contracted;
#else
this->contracted_lc = n.contracted_lc;
this->contracted_rc = n.contracted_rc;
this->contracted = n.contracted;
#endif
this->edge_protected = n.edge_protected;
this->allow_sibling = n.allow_sibling;
this->lost_children = n.lost_children;
this->max_merge_depth = n.max_merge_depth;
this->support = n.support;
this->support_normalization = n.support_normalization;
}
// TODO: clear_parent function
~Node() {
list<Node *>::iterator c = children.begin();
while(c!= children.end()) {
Node *n = *c;
c++;
n->cut_parent();
}
cut_parent();
active_descendants.clear();
root_lcas.clear();
removable_descendants.clear();
#ifdef COPY_CONTRACTED
if (contracted_lc != NULL) {
contracted_lc->delete_tree();
}
contracted_lc = NULL;
if (contracted_rc != NULL) {
contracted_rc->delete_tree();
}
contracted_rc = NULL;
#endif
}
// TODO: is this still useful?
/*
void fake_delete() {
if (lc != NULL) {
lc->p = NULL;
//lc = NULL;
}
if (rc != NULL) {
rc->p = NULL;
//rc = NULL;
}
if (p != NULL) {
p->delete_child(this);
//p = NULL;
}
active_descendants.clear();
root_lcas.clear();
removable_descendants.clear();
}
*/
// delete a subtree
void delete_tree() {
list<Node *>::iterator c = children.begin();
while(c!= children.end()) {
Node *n = *c;
c++;
n->delete_tree();
}
#ifdef COPY_CONTRACTED
if (contracted_lc != NULL) {
contracted_lc->delete_tree();
}
contracted_lc = NULL;
if (contracted_rc != NULL) {
contracted_rc->delete_tree();
}
contracted_rc = NULL;
#endif
delete this;
}
// cut edge between parent and child
// should really be cut_child, no deleting occurs
// TODO: is this useful? The only reason for this would be to
// be sure it works when the child is not correctly set
void delete_child(Node *n) {
n->cut_parent();
}
// TODO: make sure this doesn't break things with >2 children
// add a child
void add_child(Node *n) {
if (n->p != NULL)
n->cut_parent();
n->p_link = children.insert(children.end(),n);
n->p = this;
n->depth = depth+1;
n->contracted = false;
}
// TODO: make sure this doesn't break things with >2 children
// add a child
void add_child_keep_depth(Node *n) {
if (n->p != NULL)
n->cut_parent();
n->p_link = children.insert(children.end(),n);
n->p = this;
n->contracted = false;
}
/* TODO: need new method of putting a child in a specific spot
in the children list
maybe adjacent to an iterator?
*/
// insert a child before the given sibling
void insert_child(Node *sibling, Node *n) {
if (n->p != NULL)
n->cut_parent();
n->p_link = children.insert(sibling->p_link, n);
n->depth = depth+1;
n->p = this;
n->contracted = false;
}
// insert a child before the given sibling
void insert_child_keep_depth(Node *sibling, Node *n) {
if (n->p != NULL)
n->cut_parent();
n->p_link = children.insert(sibling->p_link, n);
n->p = this;
n->contracted = false;
}
/* dangerous, not relevant to multifurcating
Node *set_lchild(Node *n) {
this->lc = n;
if (n != NULL) {
n->p = this;
n->depth = depth+1;
}
return lc;
}
Node *set_lchild_keep_depth(Node *n) {
this->lc = n;
if (n != NULL) {
n->p = this;
}
return lc;
}
Node *set_rchild(Node *n) {
this->rc = n;
if (n != NULL) {
n->p = this;
n->depth = depth+1;
}
return rc;
}
Node *set_rchild_keep_depth(Node *n) {
this->rc = n;
if (n != NULL) {
n->p = this;
}
return rc;
}
*/
// potentially dangerous
/* Node *set_parent(Node *n) {
p = n;
return p;
}
*/
Node *set_twin(Node *n) {
twin = n;
return twin;
}
Node *set_name(string n) {
name = string(n);
}
int set_depth(int d) {
depth = d;
return depth;
}
void fix_depths() {
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->depth = depth+1;
(*c)->fix_depths();
}
}
int set_preorder_number(int p) {
pre_num = p;
return pre_num;
}
int set_edge_pre_start(int p) {
edge_pre_start= p;
return edge_pre_start;
}
int set_edge_pre_end(int p) {
edge_pre_end= p;
return edge_pre_end;
}
void copy_edge_pre_interval(Node *n) {
if (n->edge_pre_start > -1) {
edge_pre_start = n->edge_pre_start;
}
if (n->edge_pre_end > -1) {
edge_pre_end = n->edge_pre_end;
}
}
int set_component_number(int c) {
component_number = c;
}
list<Node *>& get_children() {
return children;
}
Node *get_contracted_lc() {
return contracted_lc;
}
Node *get_contracted_rc() {
return contracted_rc;
}
Node *set_contracted_lc(Node *n) {
contracted_lc = n;
}
Node *set_contracted_rc(Node *n) {
contracted_rc = n;
}
bool is_protected() {
return edge_protected;
}
bool is_contracted() {
return contracted;
}
void protect_edge() {
edge_protected = true;
}
void unprotect_edge() {
edge_protected = false;
}
void unprotect_subtree() {
unprotect_edge();
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->unprotect_edge();
}
}
void protect_supported_edges() {
if (support > 0)
edge_protected = true;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->protect_supported_edges();
}
}
bool can_be_sibling() {
return allow_sibling;
}
void disallow_siblings() {
allow_sibling = false;
}
void disallow_siblings_subtree() {
allow_sibling = false;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->disallow_siblings_subtree();
}
}
void allow_siblings() {
allow_sibling = true;
}
void allow_siblings_subtree() {
allow_sibling = true;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->allow_siblings_subtree();
}
}
int num_lost_children() {
return lost_children;
}
int get_max_merge_depth() {
return max_merge_depth;
}
void set_max_merge_depth(int d) {
max_merge_depth = d;
}
int count_lost_children_subtree() {
int lost_children_count = lost_children;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
lost_children_count += (*c)->count_lost_children_subtree();
}
return lost_children_count;
}
int count_lost_subtree() {
int lost_children_count = (lost_children > 0) ? 1 : 0;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
lost_children_count += (*c)->count_lost_subtree();
}
return lost_children_count;
}
void lost_child() {
lost_children++;
}
void no_lost_children() {
lost_children = 0;
}
void no_lost_children_subtree() {
lost_children = 0;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->no_lost_children_subtree();
}
}
double get_support() {
return support;
}
double set_support(double s) {
support = s;
}
double a_inc_support() {
#pragma omp atomic
support += 1;
}
double a_dec_support() {
#pragma omp atomic
support -= 1;
}
double get_support_normalization() {
return support_normalization;
}
double set_support_normalization(double s) {
support_normalization = s;
}
double a_inc_support_normalization() {
#pragma omp atomic
support_normalization += 1;
}
double a_dec_support_normalization() {
#pragma omp atomic
support_normalization -= 1;
}
void normalize_support() {
if (support_normalization != 0)
support /= support_normalization;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->normalize_support();
}
}
int get_component_number() {
return component_number;
}
void increase_clustered_children() {
num_clustered_children++;
}
void decrease_clustered_children() {
num_clustered_children--;
}
int set_num_clustered_children(int c) {
num_clustered_children = c;
}
int get_num_clustered_children() {
return num_clustered_children;
}
list <Node *> *get_active_descendants() {
return &active_descendants;
}
list <Node *> *get_root_lcas(){
return &root_lcas;
}
int get_sibling_pair_status(){
return sibling_pair_status;
}
int set_sibling_pair_status(int s){
sibling_pair_status = s;
}
void set_forest(Forest *f) {
forest = f;
}
void set_forest_rec(Forest *f) {
forest = f;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->set_forest_rec(f);
}
}
Forest *get_forest() {
return forest;
}
list<list <Node *>::iterator> *get_removable_descendants() {
return &removable_descendants;
}
void initialize_component_number(int value) {
component_number = value;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->initialize_component_number(value);
}
}
void initialize_active_descendants(list <Node *> value) {
active_descendants = value;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->initialize_active_descendants(value);
}
}
void initialize_root_lcas(list <Node *> value) {
root_lcas = value;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->initialize_root_lcas(value);
}
}
void initialize_removable_descendants(list<list <Node *>::iterator> value) {
removable_descendants = value;
list<Node *>::iterator c;
for(c = children.begin(); c != children.end(); c++) {
(*c)->initialize_removable_descendants(value);
}
}
/* contract:
* if this node has a parent and one child then contract it out
* if this node has a parent and no child then contract it out
* parent will have one child so contract it as well.
* if this node has no parent and one child then take the
* child's children and contract it out
* return the first degree two parent found or NULL if there
* was no contraction
*/
// TODO: check handling twins for interior nodes
Node *contract(bool remove) {
Node *parent = p;
Node *child;
Node *ret = NULL;
// contract out this node and give child to parent
if (parent != NULL) {
//cout << p->str_subtree() << endl;
if (children.size() == 1) {
child = children.front();
if (this == parent->children.back()) {
parent->add_child_keep_depth(child);
child->set_depth(depth);
}
else {
list<Node *>::iterator sib = p_link;
sib++;
Node *sibling = *sib;
parent->insert_child_keep_depth(sibling, child);
child->set_depth(depth);
}
child->copy_edge_pre_interval(this);
if (edge_protected && !child->is_protected())
child->protect_edge();
cut_parent();
if (remove)
delete this;
ret = parent;
}
else if (children.empty()) {
cut_parent();
ret = parent->contract(remove);
if (remove)
delete this;
//this->fake_delete();
}
else
ret = this;
}
// if no parent then take children of single child and remove it
else {
// dead component or singleton, will be cleaned up by the forest
if (children.empty()) {
if (str() == "")
name = DEAD_COMPONENT;
}
if (children.size() == 1) {
child = children.front();
child->cut_parent();
/* cluster hack - if we delete a cluster node then
* we may try to use it later. This only happens once
* per cluster so we can spend linear time to update
* the forest
*/
if (child->num_clustered_children > 0) {
delete_child(child);
if (remove)
delete this;
//this->fake_delete();
ret = child;
}
else {
// if child is a leaf then get rid of this so we don't lose refs
// problem: if the child is not c, then we want to copy
// otherwise we don't
// copy other parameters and join the twin
//to this if the child is a label
Node *new_lc = child->lchild();
Node *new_rc = child->rchild();
if (child->is_leaf()) {
if (child->get_twin() != NULL) {
set_twin(child->get_twin());
child->get_twin()->set_twin(this);
}
name = child->get_name().c_str();
// name = child->str();
}
child->cut_parent();
list<Node *>::iterator c = child->children.begin();
while(c!= child->children.end()) {
Node *new_child = *c;
c++;
new_child->cut_parent();
add_child(new_child);
}
if (child->contracted_lc != NULL)
contracted_lc = child->contracted_lc;
if (child->contracted_rc != NULL)
contracted_rc = child->contracted_rc;
pre_num = child->get_preorder_number();
if (remove) {
child->contracted_lc = NULL;
child->contracted_rc = NULL;
delete child;
}
ret = this;
}
}
}
return ret;
}
Node *contract() {
return contract(false);
}
// TODO: binary only
/* contract_sibling_pair:
* if this node has two child leaves then contract them out
* return true if contracted, otherwise false
*/
bool contract_sibling_pair() {
if (lchild() != NULL && lchild()->is_leaf()
&& rchild() != NULL && rchild()->is_leaf()) {
#ifdef DEBUG
string new_name = "<" + lchild()->str() + "," + rchild()->str() + ">";
#else
string new_name = "(" + lchild()->str() + "," + rchild()->str() + ")";
#endif
set_name(new_name);
lchild()->cut_parent();
rchild()->cut_parent();
return true;
}
return false;
}
// TODO: binary only
bool contract_sibling_pair_undoable() {
if (lchild() != NULL && lchild()->is_leaf()
&& rchild() != NULL && rchild()->is_leaf()) {
/*
#ifdef DEBUG
string new_name = "<" + lc->str() + "," + rc->str() + ">";
#else
string new_name = "(" + lc->str() + "," + rc->str() + ")";
#endif
set_name(new_name);
*/
Node *lc = lchild();
Node *rc = rchild();
contracted_lc = lc;
contracted_rc = rc;
rc->cut_parent();
lc->cut_parent();
contracted_lc->contracted = true;
contracted_rc->contracted = true;
edge_protected = false;
return true;
}
return false;
}
/* contract_sibling_pair_undoable
* works with multifurcating trees
* returns NULL for no contract, otherwise returns the contracted
* parent of the nodes
*/
Node *contract_sibling_pair_undoable(Node *child1, Node *child2) {
if (child1->parent() != this ||
child2->parent() != this)
return NULL;
if (children.size() == 2) {
contract_sibling_pair_undoable();
return this;
}
else {
Node *new_child = new Node();
// buggy
// new_child->set_preorder_number(pre_num);
if (child1->get_preorder_number() < child2->get_preorder_number()) {
new_child->set_preorder_number(child1->get_preorder_number());
}
else {
new_child->set_preorder_number(child2->get_preorder_number());
}
add_child(new_child);
new_child->add_child(child1);
new_child->add_child(child2);
edge_protected = false;
//new_child->contract_sibling_pair_undoable();
return new_child;
}
}
// TODO: binary only
void undo_contract_sibling_pair() {
// hacky, might hide problems
if (contracted_lc != NULL)
add_child(contracted_lc);
if (contracted_rc != NULL)
add_child(contracted_rc);
contracted_lc = NULL;
contracted_rc = NULL;
}
void fix_contracted_order() {
if (twin != NULL && twin->contracted_lc->twin != contracted_lc) {
Node *swap = contracted_lc;
contracted_lc = contracted_rc;
contracted_rc = swap;
}
}
// cut the edge between this node and its parent
void cut_parent() {
if (p != NULL) {
// TODO hacky: fix this to use a multi list for contractions
if (!contracted) {
p->children.erase(p_link);
}
else {
if (p->contracted_lc == this)
p->contracted_lc = NULL;
if (p->contracted_rc == this)
p->contracted_rc = NULL;
}
p = NULL;
p_link = children.end();
}
}
// caution: destructive
void contract_node() {
if (p == NULL || is_leaf())
return;
list<Node *>::iterator c = children.begin();
while(c!= children.end()) {
Node *n = *c;
c++;
p->add_child(n);
}
#ifdef COPY_CONTRACTED
if (contracted_lc != NULL) {
p->add_child(contracted_lc);
}
if (contracted_rc != NULL) {
p->add_child(contracted_rc);
}
#endif
delete this;
}
Node *parent() {
return p;
}
inline Node *lchild() {
if (children.empty())
return NULL;
else
return children.front();
}
Node *rchild() {
if (children.empty())
return NULL;
list<Node *>::iterator c = ++(children.begin());
if (c == children.end())
return NULL;
else
return *c;
}
Node *get_twin() {
return twin;
}
int get_depth() {
return depth;
}
int get_preorder_number() {
return pre_num;
}
int get_edge_pre_start() {
return edge_pre_start;
}
int get_edge_pre_end() {
return edge_pre_end;
}
string str() {
string s = "";
str_hlpr(&s);
return s;
}
string get_name() {
return name;
}
void str_hlpr(string *s) {
if (!name.empty())
*s += name.c_str();
if (contracted_lc != NULL || contracted_rc != NULL) {
#ifdef DEBUG_CONTRACTED
*s += "<";
#else
*s += "(";
#endif
if (contracted_lc != NULL) {
contracted_lc->str_c_subtree_hlpr(s);