forked from Gregable/pq-trees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pqtest.cc
170 lines (148 loc) · 3.83 KB
/
pqtest.cc
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
// This file is part of the PQ Tree library.
//
// The PQ Tree library 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.
//
// The PQ Tree Library 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 the PQ Tree Library. If not, see <http://www.gnu.org/licenses/>.
#include <assert.h>
#include <iostream>
#include <set>
#include "pqnode.h"
#include "pqtree.h"
string ReadableType(PQNode::PQNode_types type) {
if (type == PQNode::leaf) {
return "leaf";
} else if (type == PQNode::pnode) {
return "P-Node";
} else if (type == PQNode::qnode) {
return "Q-Node";
}
return "unknown";
}
void ReduceBy(const set<int>& reduce_set, PQTree* tree) {
cout << "Reducing by set { ";
for (set<int>::iterator i = reduce_set.begin(); i != reduce_set.end(); ++i)
cout << *i << " ";
cout << "}" << endl;
assert (tree->Reduce(reduce_set));
cout << tree->Print() << endl;
}
void TestBed1() {
set<int> S;
for (int i = 1; i < 9; i++)
S.insert(i);
PQTree tree(S);
cout << "PQ Tree with 8 elements and no reductions" << endl;
cout << tree.Print() << endl;
S.clear();
S.insert(4);
S.insert(3);
ReduceBy(S, &tree);
S.clear();
S.insert(6);
S.insert(4);
S.insert(3);
ReduceBy(S, &tree);
S.clear();
S.insert(4);
S.insert(3);
S.insert(5);
ReduceBy(S, &tree);
S.clear();
S.insert(4);
S.insert(5);
ReduceBy(S, &tree);
S.clear();
S.insert(2);
S.insert(6);
ReduceBy(S, &tree);
S.clear();
S.insert(1);
S.insert(2);
ReduceBy(S, &tree);
S.clear();
S.insert(4);
S.insert(5);
ReduceBy(S, &tree);
// Lets actually explore the tree manually
cout << endl;
PQNode* root = tree.Root();
cout << "Root Type: " << ReadableType(root->Type()) << endl;
vector<PQNode*> children;
root->Children(&children);
for (int i = 0; i < children.size(); ++i) {
PQNode* child = children[i];
cout << "Child " << i + 1 << " Type: " << ReadableType(child->Type());
if (child->Type() == PQNode::leaf) {
cout << " Value: " << child->LeafValue() << endl;
} else {
cout << endl;
vector<PQNode*> grandchildren;
child->Children(&grandchildren);
for (int j = 0; j < grandchildren.size(); ++j) {
PQNode* grandchild = grandchildren[j];
cout << "GrandChild " << j + 1 << " Type: "
<< ReadableType(grandchild->Type());
if (grandchild->Type() == PQNode::leaf)
cout << " Value: " << grandchild->LeafValue();
cout << endl;
}
}
}
cout << endl;
// Now, we perform a reduction that will fail.
cout << "Reducing by set {5, 3} - will fail" << endl;
S.clear();
S.insert(5);
S.insert(3);
cout << tree.Reduce(S) << endl;
cout << tree.Print() << endl;
}
void TestBed2() {
set<int> S;
for (int i = 0; i < 6; i++)
S.insert(i);
PQTree tree(S);
S.clear();
S.insert(4);
S.insert(1);
ReduceBy(S, &tree);
S.clear();
S.insert(3);
S.insert(0);
S.insert(2);
S.insert(5);
S.insert(4);
ReduceBy(S, &tree);
S.clear();
S.insert(0);
S.insert(2);
S.insert(5);
S.insert(4);
ReduceBy(S, &tree);
S.clear();
S.insert(2);
S.insert(5);
ReduceBy(S, &tree);
S.clear();
S.insert(0);
S.insert(2);
ReduceBy(S, &tree);
}
int main(int argc, char **argv) {
cout << "Test Bed 1:" << endl;
cout << "-----------------" << endl;
TestBed1();
cout << endl << endl;
cout << "Test Bed 2:" << endl;
cout << "-----------------" << endl;
TestBed2();
}