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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cassert>
using namespace std;
class Skill {
private:
char *name;
char *description;
int level;
public:
Skill(){ }
~Skill(){ }
Skill(char * name, char* desc, int level)
{
this->name = name;
this->description = desc;
this->level = level;
}
char* getName(){ return this->name;}
char* getDescription() { return this->description;}
int getLevel(){ return this->level;}
};
struct TreeNode {
Skill item; // The data in this node.
TreeNode *left; // Pointer to the left subtree.
TreeNode *right; // Pointer to the right subtree.
TreeNode *middle; // Pointer to the right subtree.
TreeNode (Skill sk)
{
item = sk;
left = NULL;
right = NULL;
middle = NULL;
}
};
class SkillTree {
private:
TreeNode* root;
char Title[100];
public:
SkillTree ():root(NULL) { }
~SkillTree (){ }
SkillTree(char ch[]):root(NULL)
{
strcpy(this->Title, ch);
}
bool Empty()
{
if (this->root==NULL)
return true;
return false;
}
TreeNode* treeContains( TreeNode *root, char* parentName )
{
if (root==NULL) {
return NULL;
}
if ( strcmp(parentName, root->item.getName())==0 ) {
return root;
}
if ( root->left!=NULL && strcmp(parentName,root->left->item.getName())==0)
{
return treeContains( root->left, parentName );
}
if (root->middle !=NULL && strcmp(parentName , root->middle->item.getName())==0) {
return treeContains( root->middle, parentName );
}
if (root->right!=NULL && strcmp(parentName , root->right->item.getName())==0) {
return treeContains( root->right, parentName );
}
return root;
}
void AddSkill(char* name,char* desc,int level)
{
Skill item(name, desc,level);
if(root==NULL)
{
this->root = new TreeNode(item);
}
}
void AddSkill(char* name,char* desc,int level,char* parentName)
{
Skill item(name, desc,level);
TreeNode* parent = treeContains(root, parentName);
if (parent!=NULL)
{
if ( parent->left == NULL )
parent->left = new TreeNode(item);
else if ( parent->middle == NULL )
parent->middle = new TreeNode(item);
else if ( parent->right == NULL )
parent->right = new TreeNode(item);
}
}
void inorderPrint( TreeNode *root ) {
if ( root != NULL )
{
cout<<root->item.getName() << " -- " <<root->item.getDescription() <<" [Lvl: " <<root->item.getLevel() <<"]\n";
inorderPrint( root->left );
inorderPrint( root->middle );
inorderPrint( root->right );
}
}
void Display(ostream& out)
{
out<<"Skill Tree: "<< Title <<"\n";
if (Empty())
{
out<<" Empty\n";
}
else
{
inorderPrint(root);
}
}
};
int main ()
{
SkillTree student("Student");
student.Display(cout) ;
student.AddSkill("Alphabet","Mastery of letters and sounds",0);
student.Display(cout);
student.AddSkill("Reading","The ability to read all manner of written material",1,"Alphabet");
student.AddSkill("Writing","The ability to put your thoughts on paper",1,"Alphabet");
student.Display(cout);
student.AddSkill("Speed Reading Level 1","Read any text twice as fast as normal",5,"Reading");
student.AddSkill("Speed Reading Level 2","Read any text four times as fast as normal",10,"Speed Reading Level 1");
student.AddSkill("Memorization","Memorize average sized texts",10,"Reading");
student.AddSkill("Massive Memorization","Memorize large sized texts",20,"Memorization");
student.AddSkill("Spell Writing","The ability to write spells",5,"Writing");
student.AddSkill("History","The ability to write (and rewrite) history",10,"Writing");
student.AddSkill("Written Creation","The ability to write things into reality",20,"History");
student.Display(cout);
system("pause");
}
|