Finished implementation of problem 1.
This commit is contained in:
parent
b43727abb7
commit
254f7abfae
7 changed files with 174 additions and 14 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -1,7 +1,13 @@
|
||||||
/hw1/hw1pr1/*.o
|
/hw1/hw1pr1/*.o
|
||||||
|
/hw1/hw1pr1/main
|
||||||
/hw1/hw1pr2/*.o
|
/hw1/hw1pr2/*.o
|
||||||
|
/hw1/hw1pr2/main
|
||||||
/hw1/hw1pr3/*.o
|
/hw1/hw1pr3/*.o
|
||||||
|
/hw1/hw1pr3/main
|
||||||
|
|
||||||
/hw2/hw2pr1/*.o
|
/hw2/hw2pr1/*.o
|
||||||
|
/hw1/hw2pr1/main
|
||||||
/hw2/hw2pr2/*.o
|
/hw2/hw2pr2/*.o
|
||||||
|
/hw1/hw2pr2/main
|
||||||
/hw2/hw2pr3/*.o
|
/hw2/hw2pr3/*.o
|
||||||
|
/hw1/hw2pr3/main
|
||||||
|
|
1
hw2/hw2pr1/.hw2pr1_data.txt
Normal file
1
hw2/hw2pr1/.hw2pr1_data.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
((3,12,8),(2,4,6),(14,5,2))
|
|
@ -7,36 +7,147 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <tuple>
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
|
||||||
using namespace std;
|
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];
|
if(n->getLevel() % 2 == 0)
|
||||||
input = input.substr(1);
|
{
|
||||||
|
int max = n->getValue();
|
||||||
|
if(n->getChildren().size() > 0)
|
||||||
|
{
|
||||||
|
for(int c = 0; c < n->getChildren().size(); c++)
|
||||||
|
{
|
||||||
|
minmax(n->getChildAt(c));
|
||||||
|
|
||||||
if(tempc == '(')
|
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<node, string> makeTree(string input, node n, int level)
|
||||||
|
{
|
||||||
|
char tempc;
|
||||||
|
string tempn = "";
|
||||||
|
|
||||||
while(input.length() != 0)
|
while(input.length() != 0)
|
||||||
{
|
{
|
||||||
// If the char is a digit.
|
tempc = input[0];
|
||||||
if(tempc)
|
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.
|
// If the char is a comma.
|
||||||
else if(tempc == ',')
|
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.
|
// If the char is a close paranthesis.
|
||||||
else if(tempc == ')')
|
else if(tempc == ')')
|
||||||
{
|
{
|
||||||
|
if(tempn.length() > 0)
|
||||||
|
{
|
||||||
|
node tempnode;
|
||||||
|
tempnode.setLevel(level);
|
||||||
|
tempnode.setValue(stoi(tempn));
|
||||||
|
tempnode.setParent(&n);
|
||||||
|
n.addChild(tempnode);
|
||||||
|
tempn = "";
|
||||||
|
}
|
||||||
|
tuple<node, string> 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<node, string> tempt = makeTree(input, tempnode, level + 1);
|
||||||
|
tempnode = get<0>(tempt);
|
||||||
|
input = get<1>(tempt);
|
||||||
|
n.addChild(tempnode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tuple<node, string> output(n, input);
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
|
@ -74,6 +185,16 @@ int main(int argc, char **argv)
|
||||||
cin >> input;
|
cin >> input;
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << input << endl;
|
node root;
|
||||||
|
root.setLevel(0);
|
||||||
|
root.setValue(INT_MIN);
|
||||||
|
tuple<node, string> tree;
|
||||||
|
tree = makeTree(input.substr(1), root, 1);
|
||||||
|
root = get<0>(tree);
|
||||||
|
|
||||||
|
minmax(&root);
|
||||||
|
|
||||||
|
printTree(root);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)))
|
||||||
|
|
|
@ -1,5 +1,13 @@
|
||||||
all: main.o
|
all: main
|
||||||
main.o: hw2pr1.cpp
|
|
||||||
g++ -std=c++17 hw2pr1.cpp -o hw2pr1.o
|
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:
|
clean:
|
||||||
rm -rf *.o
|
rm -rf *.o *.gch main
|
||||||
|
|
19
hw2/hw2pr1/node.cpp
Normal file
19
hw2/hw2pr1/node.cpp
Normal file
|
@ -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();
|
||||||
|
}
|
||||||
|
|
|
@ -10,16 +10,21 @@ class node
|
||||||
private:
|
private:
|
||||||
node* parent;
|
node* parent;
|
||||||
int level;
|
int level;
|
||||||
|
int value;
|
||||||
vector<node> children;
|
vector<node> children;
|
||||||
public:
|
public:
|
||||||
|
node();
|
||||||
|
node(node *n);
|
||||||
void setParent(node* p) { parent = p; };
|
void setParent(node* p) { parent = p; };
|
||||||
node* getParent() { return parent; };
|
node* getParent() { return parent; };
|
||||||
void setLevel(int l) { level = l; };
|
void setLevel(int l) { level = l; };
|
||||||
int getLevel() { return level; };
|
int getLevel() { return level; };
|
||||||
|
void setValue(int v) { value = v; };
|
||||||
|
int getValue() { return value; };
|
||||||
void setChildren(vector<node> c) { children = c; };
|
void setChildren(vector<node> c) { children = c; };
|
||||||
void addChild(node n) { children.push_back(n); };
|
void addChild(node n) { children.push_back(n); };
|
||||||
vector<node> getChildren() { return children; };
|
vector<node> getChildren() { return children; };
|
||||||
node getChildrenAt(int i) { return children.at(i); };
|
node* getChildAt(int i) { return &children.at(i); };
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Reference in a new issue