-
Notifications
You must be signed in to change notification settings - Fork 7
/
SiblingPair.h
116 lines (100 loc) · 3.02 KB
/
SiblingPair.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
/*******************************************************************************
SiblingPair.h
Data structure for a sibling pair of a binary tree
Copyright 2012-2014 Chris Whidden
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_SIBLINGPAIR
#define INCLUDE_SIBLINGPAIR
#include <cstdio>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include "Node.h"
using namespace std;
class SiblingPair {
public:
Node *a;
Node *c;
int key;
int key2;
SiblingPair() {
a = NULL;
c = NULL;
key = -1;
key2 = -1;
}
SiblingPair(Node *A, Node *C) {
a = A;
c = C;
key = a->get_preorder_number();
if (c->get_preorder_number() < key)
key = c->get_preorder_number();
// a->get_preorder_number();
// if (c->get_preorder_number() < key)
// key = c->get_preorder_number();
}
bool operator< (const SiblingPair &sp) const {
return key < sp.key;
}
};
// TODO: binary only
void find_sibling_pairs_set_hlpr(Node *n,
set<SiblingPair> *sibling_pairs) {
Node *lchild = n->lchild();
Node *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 node's subtree
void append_sibling_pairs_set(Node *n,set<SiblingPair> *sibling_pairs) {
find_sibling_pairs_set_hlpr(n,sibling_pairs);
}
// find the sibling pairs in this node's subtree
set<SiblingPair> *find_sibling_pairs_set(Node *n) {
set<SiblingPair> *sibling_pairs = new set<SiblingPair>();
find_sibling_pairs_set_hlpr(n,sibling_pairs);
return sibling_pairs;
}
// return a set of the sibling pairs
set<SiblingPair> *find_sibling_pairs_set(Forest *f) {
set<SiblingPair> *sibling_pairs = new set<SiblingPair>();
for(int i = 0; i < f->num_components(); i++) {
Node *component = f->get_component(i);
append_sibling_pairs_set(component,sibling_pairs);
}
return sibling_pairs;
}
#endif