diff --git a/.gitignore b/.gitignore index e9bd4e8..698e79e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,8 +6,8 @@ /hw1/hw1pr3/main /hw2/hw2pr1/*.o -/hw1/hw2pr1/main +/hw2/hw2pr1/main /hw2/hw2pr2/*.o -/hw1/hw2pr2/main +/hw2/hw2pr2/main /hw2/hw2pr3/*.o -/hw1/hw2pr3/main +/hw2/hw2pr3/main diff --git a/hw2/hw2pr1/hw2pr2.cpp b/hw2/hw2pr1/hw2pr2.cpp new file mode 100644 index 0000000..1c01bca --- /dev/null +++ b/hw2/hw2pr1/hw2pr2.cpp @@ -0,0 +1,223 @@ +// Name: Alexander Huddleston UIN: 223000555 +// CSCE 420 +// Due: 11:59 P.M. Monday, October 30, 2017 +// hw2pr1.cpp + +#include +#include +#include +#include +#include +#include +#include "node.h" + +using namespace std; + +// Perform min/max on this tree. +void minmax(node *n) +{ + + 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) +{ + bool negate = false; + char tempc; + string tempn = ""; + + while(input.length() != 0) + { + tempc = input[0]; + if(input.length() > 0) + { + input = input.substr(1); + } + else + { + input = ""; + } + + // If the char is a number. + if(isdigit(tempc)) + { + tempn += tempc; + } + + else if(tempc == '-') + { + negate = true; + } + + // If the char is a comma. + else if(tempc == ',') + { + if(tempn.length() > 0) + { + node tempnode; + tempnode.setLevel(level); + int negatemp = stoi(tempn); + if(negate) + { + negatemp = -negatemp; + } + tempnode.setValue(negatemp); + tempnode.setParent(&n); + n.addChild(tempnode); + tempn = ""; + negate = false; + } + } + + // If the char is a close paranthesis. + else if(tempc == ')') + { + if(tempn.length() > 0) + { + node tempnode; + tempnode.setLevel(level); + int negatemp = stoi(tempn); + if(negate) + { + negatemp = -negatemp; + } + tempnode.setValue(negatemp); + tempnode.setParent(&n); + n.addChild(tempnode); + tempn = ""; + negate = false; + } + 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) +{ + // Initialization of input string. + string input = ""; + + // For testing, I want to read from a file. + // Typing in an tree from the keyboard each execution is a waste of time. + if(argc > 1) + { + if(!((string)argv[1]).substr(0, 2).compare("-f")) + { + ifstream file; + string line; + try + { + file.open("hw2pr2_data.txt"); + } + catch(exception e) + { + cerr << "File not found, or could not be opened." << endl; + } + while(!file.eof()) + { + getline(file, line); + input += line; + } + } + } + + else + { + cout << "Please input a tree: "; + cin >> input; + } + + node root; + root.setLevel(0); + root.setValue(INT_MIN); + tuple tree; + tree = makeTree(input.substr(1), root, 1); + root = get<0>(tree); + + printTree(root); + minmax(&root); + + printTree(root); + + return 0; +} diff --git a/hw2/hw2pr1/hw2pr1_data.txt b/hw2/hw2pr1/hw2pr2_data.txt similarity index 100% rename from hw2/hw2pr1/hw2pr1_data.txt rename to hw2/hw2pr1/hw2pr2_data.txt diff --git a/hw2/hw2pr1/makefile b/hw2/hw2pr1/makefile index 1abb8b0..79fdb18 100644 --- a/hw2/hw2pr1/makefile +++ b/hw2/hw2pr1/makefile @@ -1,10 +1,10 @@ all: main -main: hw2pr1.o node.o - g++ -std=c++17 hw2pr1.o node.o -o main +main: hw2pr2.o node.o + g++ -std=c++17 hw2pr2.o node.o -o main -hw2pr1.o: hw2pr1.cpp - g++ -std=c++17 -c hw2pr1.cpp +hw2pr1.o: hw2pr2.cpp + g++ -std=c++17 -c hw2pr2.cpp node.o: node.cpp node.h g++ -std=c++17 -c node.cpp diff --git a/hw2/hw2pr1/.hw2pr1_data.txt b/hw2/hw2pr2/.hw2pr1_data.txt similarity index 100% rename from hw2/hw2pr1/.hw2pr1_data.txt rename to hw2/hw2pr2/.hw2pr1_data.txt diff --git a/hw2/hw2pr1/hw2pr1.cpp b/hw2/hw2pr2/hw2pr2.cpp similarity index 99% rename from hw2/hw2pr1/hw2pr1.cpp rename to hw2/hw2pr2/hw2pr2.cpp index 0812ef2..6208831 100644 --- a/hw2/hw2pr1/hw2pr1.cpp +++ b/hw2/hw2pr2/hw2pr2.cpp @@ -215,7 +215,7 @@ int main(int argc, char **argv) string line; try { - file.open("hw2pr1_data.txt"); + file.open("hw2pr2_data.txt"); } catch(exception e) { diff --git a/hw2/hw2pr2/hw2pr2_data.txt b/hw2/hw2pr2/hw2pr2_data.txt new file mode 100644 index 0000000..647ea82 --- /dev/null +++ b/hw2/hw2pr2/hw2pr2_data.txt @@ -0,0 +1 @@ +(((-2,-11,10),(2,15,13),(-1,-16,-16)),((-20,13,20),(-13,7,-19),(7,3,4)),((-20,-9,-14),(-4,-6,-12),(11,18,-3))) diff --git a/hw2/hw2pr2/makefile b/hw2/hw2pr2/makefile new file mode 100644 index 0000000..79fdb18 --- /dev/null +++ b/hw2/hw2pr2/makefile @@ -0,0 +1,13 @@ +all: main + +main: hw2pr2.o node.o + g++ -std=c++17 hw2pr2.o node.o -o main + +hw2pr1.o: hw2pr2.cpp + g++ -std=c++17 -c hw2pr2.cpp + +node.o: node.cpp node.h + g++ -std=c++17 -c node.cpp + +clean: + rm -rf *.o *.gch main diff --git a/hw2/hw2pr2/node.cpp b/hw2/hw2pr2/node.cpp new file mode 100644 index 0000000..de729d1 --- /dev/null +++ b/hw2/hw2pr2/node.cpp @@ -0,0 +1,23 @@ +#include "node.h" + +using namespace std; + +node::node() +{ + parent = NULL; + level = 0; + value = 0; + alpha = INT_MIN; + beta = INT_MAX; +} + +node::node(node* n) +{ + parent = n->getParent(); + level = n->getLevel(); + value = n->getValue(); + children = n->getChildren(); + alpha = n->getAlpha(); + beta = n->getBeta(); +} + diff --git a/hw2/hw2pr2/node.h b/hw2/hw2pr2/node.h new file mode 100644 index 0000000..48cfa65 --- /dev/null +++ b/hw2/hw2pr2/node.h @@ -0,0 +1,37 @@ +#ifndef NODE_H +#define NODE_H + +#include +#include + +using namespace std; + +class node +{ + private: + node* parent; + int level; + int value; + vector children; + int alpha; + int beta; + 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* getChildAt(int i) { return &children.at(i); }; + void setAlpha(int a) { alpha = a; }; + int getAlpha() { return alpha; }; + void setBeta(int b) { beta = b; }; + int getBeta() { return beta; }; +}; + +#endif