-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
105 lines (94 loc) · 3.28 KB
/
tests.cpp
File metadata and controls
105 lines (94 loc) · 3.28 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
#include <iostream>
#include <limits>
#include <fstream>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <unordered_map>
#include <iterator>
#include "phylotree.hpp"
#include "phylotree.cpp"
//Map 2. Map for down profiles of type <Node*, set<std::string>>
std::unordered_map<Node*, std::set<std::string>> down;
//Map 3. Map for boolean skip of type <Node*, bool>
std::unordered_map<Node*, std::set<std::string>> up;
std::unordered_map<Node*, bool> skip;
void build_up_profiles(Tree &t) {
//Function for up profiles
//Preorder traversal of tree
//write code here...
Node* n = t.get_root();
Node* root;
Traverse::PreOrder pre = Traverse::PreOrder(n);
//Start by finding the root of the tree
//Do so with simple preorder traversal
//and if statements.
for (; pre != pre.end(); ++pre) {
if (pre.operator*()->is_root()) {
root = pre.operator*();
skip[pre.operator*()] = true;
}
else if(pre.operator*()->is_leaf()) {
skip[pre.operator*()] = true;
}
else {
skip[pre.operator*()] = false;
}
}
std::list<Node*>* children = root->get_children();
for (auto it = children->begin(); it != children->end(); ++it) {
up[it.operator*()] = {};
skip[pre.operator*()] = true;
for (auto sibling_it = children->begin(); sibling_it != children->end(); ++sibling_it) {
if (it.operator*() != sibling_it.operator*()) {
/*
std::set_union(it.operator*()->up.begin(), it.operator*()->up.end(),sibling_it.operator*()->down.begin(),
sibling_it.operator*()->down.end(), std::back_inserter(it.operator*()->up));
//Would this be better?
up[it.operator*()].insert(up[it.operator*()].begin(), up[it.operator*()].end());
up[it.operator*()].insert(down[sibling_it.operator*()].begin(), down[sibling_it.operator*()].end());
*/
}
}
}
//Calculate the children profiles first then
//do the rest, just like the examples.
for (; pre != pre.end(); ++pre) {
std::string label = pre.operator*()->get_label();
if (!skip[pre.operator*()] == true) { // node.skip
Node* parent = pre.operator*()->get_parent();
//pre.operator* up profile = parent up profile
std::list<Node*>* children = parent->get_children();
for (auto it = children->begin(); it != children->end(); ++it) {
if (pre.operator*() != it.operator*()) {
//up profile of node is equal to node up U sibl down profile;
/*std::set_union(pre.operator*()->up.begin(), pre.operator*()->up.end(),it.operator*()->down.begin(),
it.operator*()->down.end(), std::back_inserter(pre.operator*()->up));
up[it.operator*()].insert(up[pre.operator*()].begin(),up[pre.operator*()].end());
up[it.operator*()].insert(down[it.operator*()].begin(), down[it.operator*()].end());
*/
}
}
}
}
}
void print(std::set<std::string> &s)
{
for (auto const &i: s) {
std::cout << i << " ";
}
std::cout<<"\n------\n";
}
int main() {
std::string W = " ";
std::string L = "((((A,B),C),((A,B),C)),D,E);";
Tree tree = Tree(L);
Node* n = tree.get_root();
build_up_profiles(tree);
Traverse::PostOrder pre = Traverse::PostOrder(n);
for(; pre != pre.end(); ++pre){
print(down[pre.operator*()]);
}
return 0;
}