-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSiblingPair.cpp
73 lines (63 loc) · 1.98 KB
/
SiblingPair.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
#include "SiblingPair.hpp"
SiblingPair::SiblingPair()
{
a = NULL;
c = NULL;
key = -1;
key2 = -1;
}
SiblingPair::SiblingPair(SPRNode *A, SPRNode *C)
{
a = A;
c = C;
key = a->get_preorder_number();
if (c->get_preorder_number() < key)
key = c->get_preorder_number();
}
bool SiblingPair::operator< (const SiblingPair &sp) const
{
return key < sp.key;
}
void find_sibling_pairs_set_hlpr(SPRNode *n,
std::set<SiblingPair> *sibling_pairs) {
SPRNode *lchild = n->lchild();
SPRNode *rchild = n->rchild();
bool lchild_leaf = false;
bool rchild_leaf = false;
if (lchild != NULL) {
if (lchild->is_leaf())
lchild_leaf = true;
else
find_sibling_pairs_set_hlpr(lchild,sibling_pairs);
}
if (rchild != NULL) {
if (rchild->is_leaf())
rchild_leaf = true;
else
find_sibling_pairs_set_hlpr(rchild,sibling_pairs);
}
if (lchild_leaf && rchild_leaf) {
sibling_pairs->insert(SiblingPair(lchild,rchild));
//lchild->add_to_sibling_pairs(sibling_pairs, 1);
//rchild->add_to_sibling_pairs(sibling_pairs, 2);
}
}
// find the sibling pairs in this SPRNode's subtree
void append_sibling_pairs_set(SPRNode *n,std::set<SiblingPair> *sibling_pairs) {
find_sibling_pairs_set_hlpr(n,sibling_pairs);
}
// find the sibling pairs in this SPRNode's subtree
std::set<SiblingPair> *find_sibling_pairs_set(SPRNode *n) {
std::set<SiblingPair> *sibling_pairs = new std::set<SiblingPair>();
find_sibling_pairs_set_hlpr(n,sibling_pairs);
return sibling_pairs;
}
// return a set of the sibling pairs
std::set<SiblingPair> *find_sibling_pairs_set(Forest *f) {
std::set<SiblingPair> *sibling_pairs = new std::set<SiblingPair>();
for(int i = 0; i < f->num_components(); i++) {
SPRNode *component = f->get_component(i);
append_sibling_pairs_set(component,sibling_pairs);
}
return sibling_pairs;
}