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
|
#include "Main.h"
#include <iostream>
using namespace std;
//initialises alpha and production_hours
Main::Main()
{
alpha = "abcdefghijklmnopqrstuvwxyz";
int production_hours[2][9] = {{100, 200, 50, 25, 70, 120, 80, 60, 50},
{10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}};
}
void Main::setRoot(pTree tree)
{
pNode root_node = pNode("Ship", 0,0); //This is the root node
pNode* root_ptr = &root_node; // pointer to root node created for use in setRoot()
tree.setRoot(root_ptr);
}
void Main::buildTree(pNode* root)
{
root->setleft(createMine(0));
root->setRight(createMine(1));
}
//creates node with values according to i
//Rules:
//If the name is "mine b" only create a right child
//If the name is "mine e" create both left and right children, with diffent values for i
//If the name is "mine c,h,g,f,i then stop creating children! (these are the leaf nodes)
//Always return the pointer to the children as they are stored as they are stored in the parent node
pNode* Main::createMine(int i)
{
pNode node = pNode("Mine " + alpha[i], production_hours[0][i], production_hours[1][i]);
pNode* ptr = &node;
if(node.getName() == "Mine b")
{
node.setRight(createMine(i+3));
return ptr;
}
else if(node.getName() == "Mine e")
{
node.setleft(createMine(i+3));
node.setRight(createMine(i+4));
return ptr;
}
else if(node.getName() == "Mine c" || node.getName() == "Mine h" ||node.getName() == "Mine g"|| node.getName() == "Mine f" || node.getName() == "Mine i")
{
return ptr;
}
else
{
node.setleft(createMine(i+2));
node.setRight(createMine(i+3));
}
return ptr;
}
|