-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG1.cpp
More file actions
255 lines (219 loc) · 6.36 KB
/
G1.cpp
File metadata and controls
255 lines (219 loc) · 6.36 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
#include <iostream>
#include <vector>
#include <string>
struct BNode{
std::string val;
BNode* left;
BNode* right;
};
BNode* bt_node(std::string e,BNode* l,BNode* r){
BNode* tmp = new BNode;
tmp -> val = e;
tmp -> left = l;
tmp -> right = r;
return tmp;
}
// once a zero is hit it builds the branch off the tree in that location
BNode* hit_0(std::string const direction,std::vector<int> const ord,int counter){
std::string name;
char x01 = direction[ord[counter]];
name = "x" + std::to_string(ord[counter] + 1);
if((x01 == 'x')||(ord[counter]==-1)){
return bt_node("1",NULL,NULL);
}
if(x01 == '1'){
return bt_node(name,bt_node("0",NULL,NULL),hit_0(direction,ord,counter+1));
}
return bt_node(name,hit_0(direction,ord,counter+1),bt_node("0",NULL,NULL));
}
// moves through the existing tree until a zero is hit
BNode* Build_tree(std::string const direction,std::vector<int> const ord,BNode* t,int counter){
char x01 = direction[ord[counter]];
if(t -> val != "0"){
if(x01=='0'){
t -> left = Build_tree(direction,ord,t->left,counter+1);
return t;
}
if(x01=='1'){
t -> right = Build_tree(direction,ord,t->right,counter+1);
return t;
}
}
return hit_0(direction,ord,counter);
}
// works out the optimal order of the varibles in the tree
std::vector<int> order(std::vector<std::string> in){
std::vector <int> rank;
std::vector<int> out;
for(int i=0;i<in[0].size();i++){
int counter = 0;
for(int j=0;j<in.size();j++){
if(in[j][i]=='x'){
counter++;
}
}
rank.push_back(counter);
}
for(int a = 0; a < in[0].size();a++){
int min = -1, track;
for(int k=0;k<rank.size();k++){
if(((rank[k] < min)&&(rank[k]!=-1))||(min==-1)){
min = rank[k];
track = k;
}
}
rank[track]=-1;
out.push_back(track);
}
out.push_back(-1);
return out;
}
//this compresses the inputs with "don't cares as in the report"
std::vector<std::string> shorthand(std::vector<std::string> r){
std::vector<std::string> tmp;
for(int y=r[0].size()-1; y>=0;y--){
int i=0;
while((i<r.size())){
int j = i+1,check = 1;
while((j < r.size())&&(check!=-1)){
int x =0;
check=-1;
while((x != r[0].size())&&(check == -1)){
if((r[i][x]!=r[j][x])&&(x!=y)){
check = 0;
}
x++;
}
if(check == -1){
r[i][y]='x';
r[j]="";
}
j++;
if(r[j]==""){
j++;
}
}
tmp.push_back(r[i]);
i++;
if(r[i]==""){
i++;
}
}
r = tmp;
tmp.clear();
}
return r;
}
//builds the compressed tree
BNode* build_bt(const std::vector<std::string>& fvalues){
std::vector<int> first;
BNode* t;
t = bt_node("0",NULL,NULL);
if (fvalues.size() > 0){
std::vector<std::string> r = shorthand(fvalues);
first = order(r);
for(int i=0;i<r.size();i++){
t=Build_tree(r[i],first,t,0);
}
}
return t;
}
// do not alter the following function
// this function converts e.g. std::string "x3" to int 2
int label_to_idx(const std::string& label){
std::string out;
for(int i = 1; i < label.size(); i++){
out.push_back(label[i]);
}
return std::stoi(out) - 1;
}
void destroy_tree(BNode* T){
if(T != NULL){
destroy_tree(T->left);
destroy_tree(T->right);
delete T;
}
}
// do not alter the following function
std::string eval_bt(BNode* bt, const std::string& input){
if( (bt->left == NULL) && (bt->right == NULL) ){
return bt->val;
}
else{
int idx = label_to_idx(bt->val);
std::string input_idx;
input_idx.push_back(input[idx]);
if(input_idx == "0"){
return eval_bt(bt->left, input);
}
else{
return eval_bt(bt->right, input);
}
}
}
// do not alter the following function
int n_nodes_bt(BNode* t){
if(t == NULL){
return 0;
}
else{
return 1 + n_nodes_bt(t->left) + n_nodes_bt(t->right);
}
}
class BoolTree{
public:
BoolTree(const std::vector<std::string>& fvalues){
t = build_bt(fvalues);
}
std::string eval(const std::string& s){
return eval_bt(t, s);
}
int n_nodes(){
return n_nodes_bt(t);
}
~BoolTree(){
destroy_tree(t);
}
private:
BNode* t;
};
// the main provided below must work correctly
// with your implementation for the code above
// however you can change it as you wish for your own testing
// and your changes won't be considered for the assessment
// (in your submission you can also have an empty main or no main at all)
int main(){
std::vector<std::string> fv;
std::string row;
row = "11";
fv.push_back(row);
BoolTree ft1(fv);
// as in the second assignment we give as input only the rows
// of the truth table whose value is 1
// (this is an example with the boolean "and" function)
fv.clear();
row = "010";
fv.push_back(row);
row = "011";
fv.push_back(row);
row = "110";
fv.push_back(row);
row = "111";
fv.push_back(row);
BoolTree ft2(fv);
// this corresponds to the f(x1, x2, x3) example shown above
std::cout << ft1.n_nodes() << std::endl;
// we expect this to print 5
std::cout << ft2.n_nodes() << std::endl;
// if the algorithm is such that it builds the smallest possible corresponding tree
// for the boolean function we are considering
// then this will print 3 (see tree diagram in the example above)
std::cout << ft1.eval("01") << std::endl;
// this should print "0"
std::cout << ft1.eval("11") << std::endl;
// this should print "1"
std::cout << ft2.eval("001") << std::endl;
// this should print "0"
std::cout << ft2.eval("110") << std::endl;
// this should print "1"
}