diff --git a/.gitignore b/.gitignore index 6628a4a..e9bd4e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,13 @@ /hw1/hw1pr1/*.o +/hw1/hw1pr1/main /hw1/hw1pr2/*.o +/hw1/hw1pr2/main /hw1/hw1pr3/*.o +/hw1/hw1pr3/main /hw2/hw2pr1/*.o +/hw1/hw2pr1/main /hw2/hw2pr2/*.o +/hw1/hw2pr2/main /hw2/hw2pr3/*.o +/hw1/hw2pr3/main diff --git a/hw2/hw2pr1/.hw2pr1_data.txt b/hw2/hw2pr1/.hw2pr1_data.txt new file mode 100644 index 0000000..d96b905 --- /dev/null +++ b/hw2/hw2pr1/.hw2pr1_data.txt @@ -0,0 +1 @@ +((3,12,8),(2,4,6),(14,5,2)) diff --git a/hw2/hw2pr1/hw2pr1.cpp b/hw2/hw2pr1/hw2pr1.cpp index 225a674..3c1f73a 100644 --- a/hw2/hw2pr1/hw2pr1.cpp +++ b/hw2/hw2pr1/hw2pr1.cpp @@ -7,36 +7,147 @@ #include #include #include +#include +#include #include "node.h" using namespace std; -node makeTree(string input, node n, int level) +// Perform min/max on this tree. +void minmax(node *n) { - char tempc = input[0]; - input = input.substr(1); - - if(tempc == '(') + if(n->getLevel() % 2 == 0) { + int max = n->getValue(); + if(n->getChildren().size() > 0) + { + for(int c = 0; c < n->getChildren().size(); c++) + { + minmax(n->getChildAt(c)); + + if(n->getChildAt(c)->getValue() > max) + { + max = n->getChildAt(c)->getValue(); + } + } + } + n->setValue(max); + cout << n->getValue() << endl; } + else + { + int min = n->getValue(); + if(n->getChildren().size() > 0) + { + for(int c = 0; c < n->getChildren().size(); c++) + { + minmax(n->getChildAt(c)); + + if(n->getChildAt(c)->getValue() < min) + { + min = n->getChildAt(c)->getValue(); + } + } + } + n->setValue(min); + cout << n->getValue() << endl; + } +} + +// This is mostly just a simple debug function so I can see +// if my tree is being formed properly. +void printTree(node n) +{ + cout << n.getValue() << " " << n.getLevel() << " "; + if(n.getChildren().size() != 0) + { + cout << "\n|\n"; + for(node c : n.getChildren()) + { + printTree(c); + } + cout << endl; + } +} + +tuple makeTree(string input, node n, int level) +{ + char tempc; + string tempn = ""; + while(input.length() != 0) { - // If the char is a digit. - if(tempc) + tempc = input[0]; + if(input.length() > 0) { + input = input.substr(1); + } + else + { + input = ""; + } + + cout << "testing" << endl; + // If the char is a number. + if(isdigit(tempc)) + { + tempn += tempc; } // If the char is a comma. else if(tempc == ',') { + if(tempn.length() > 0) + { + node tempnode; + tempnode.setLevel(level); + tempnode.setValue(stoi(tempn)); + tempnode.setParent(&n); + n.addChild(tempnode); + tempn = ""; + } } // If the char is a close paranthesis. else if(tempc == ')') { + if(tempn.length() > 0) + { + node tempnode; + tempnode.setLevel(level); + tempnode.setValue(stoi(tempn)); + tempnode.setParent(&n); + n.addChild(tempnode); + tempn = ""; + } + tuple tempt(n, input); + return tempt; + } + + // If the char is an open paranthesis. + else if(tempc == '(') + { + node tempnode; + tempnode.setLevel(level); + if(level % 2 == 0) + { + tempnode.setValue(INT_MIN); + } + else + { + tempnode.setValue(INT_MAX); + } + tempnode.setParent(&n); + tuple tempt = makeTree(input, tempnode, level + 1); + tempnode = get<0>(tempt); + input = get<1>(tempt); + n.addChild(tempnode); } } + + tuple output(n, input); + return output; } int main(int argc, char **argv) @@ -74,6 +185,16 @@ int main(int argc, char **argv) cin >> input; } - cout << input << endl; + node root; + root.setLevel(0); + root.setValue(INT_MIN); + tuple tree; + tree = makeTree(input.substr(1), root, 1); + root = get<0>(tree); + + minmax(&root); + + printTree(root); + return 0; } diff --git a/hw2/hw2pr1/hw2pr1_data.txt b/hw2/hw2pr1/hw2pr1_data.txt index d96b905..f5cb9f2 100644 --- a/hw2/hw2pr1/hw2pr1_data.txt +++ b/hw2/hw2pr1/hw2pr1_data.txt @@ -1 +1 @@ -((3,12,8),(2,4,6),(14,5,2)) +((3,8,(7,(3,0,7),(8,8,2))),(4,(7,9,8),8),(((3,6,4),2,6),((9,2,9),4,7,(6,4,5)),4,(6,4,5))) diff --git a/hw2/hw2pr1/makefile b/hw2/hw2pr1/makefile index da787b0..1abb8b0 100644 --- a/hw2/hw2pr1/makefile +++ b/hw2/hw2pr1/makefile @@ -1,5 +1,13 @@ -all: main.o -main.o: hw2pr1.cpp - g++ -std=c++17 hw2pr1.cpp -o hw2pr1.o +all: main + +main: hw2pr1.o node.o + g++ -std=c++17 hw2pr1.o node.o -o main + +hw2pr1.o: hw2pr1.cpp + g++ -std=c++17 -c hw2pr1.cpp + +node.o: node.cpp node.h + g++ -std=c++17 -c node.cpp + clean: - rm -rf *.o + rm -rf *.o *.gch main diff --git a/hw2/hw2pr1/node.cpp b/hw2/hw2pr1/node.cpp new file mode 100644 index 0000000..b43a372 --- /dev/null +++ b/hw2/hw2pr1/node.cpp @@ -0,0 +1,19 @@ +#include "node.h" + +using namespace std; + +node::node() +{ + parent = NULL; + level = 0; + value = 0; +} + +node::node(node* n) +{ + parent = n->getParent(); + level = n->getLevel(); + value = n->getValue(); + children = n->getChildren(); +} + diff --git a/hw2/hw2pr1/node.h b/hw2/hw2pr1/node.h index ca4fedf..9f8e8f4 100644 --- a/hw2/hw2pr1/node.h +++ b/hw2/hw2pr1/node.h @@ -10,16 +10,21 @@ class node private: node* parent; int level; + int value; vector children; public: + node(); + node(node *n); void setParent(node* p) { parent = p; }; node* getParent() { return parent; }; void setLevel(int l) { level = l; }; int getLevel() { return level; }; + void setValue(int v) { value = v; }; + int getValue() { return value; }; void setChildren(vector c) { children = c; }; void addChild(node n) { children.push_back(n); }; vector getChildren() { return children; }; - node getChildrenAt(int i) { return children.at(i); }; + node* getChildAt(int i) { return &children.at(i); }; }; #endif