### Problem in a Tree!

Hello friends, here I put everything in the main, easy to compile, I am implementing one exercise that requires one tree, this tree should only have 3 nodes, the fact is that I have one function (treeContains ()) that given 1 char * name, I have to bucar node I tell you, the fact is that I looking bad, I have one data output is correct and shows me all otherwise erroneous data, which can be??

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156`` ``````#include #include #include #include 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<item.getName() << " -- " <item.getDescription() <<" [Lvl: " <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"); }``````

and my expected output:
Skill Tree: Student
Empty
Skill Tree: Student
- Alphabet -- Mastery of letters and sounds [Lvl: 0]
Skill Tree: Student
- Alphabet -- Mastery of letters and sounds [Lvl: 0]
- Reading -- The ability to read all manner of written material [Lvl: 1]
- Writing -- The ability to put your thoughts on paper [Lvl: 1]
Skill Tree: Student
- Alphabet -- Mastery of letters and sounds [Lvl: 0]
- Reading -- The ability to read all manner of written material [Lvl: 1]
- Speed Reading Level 1 -- Read any text twice as fast as normal [Lvl: 5]
- Speed Reading Level 2 -- Read any text four times as fast as normal [Lvl: 10]
- Memorization -- Memorize average sized texts [Lvl: 10]
- Massive Memorization -- Memorize large sized texts [Lvl: 20]
- Writing -- The ability to put your thoughts on paper [Lvl: 1]
- Spell Writing -- The ability to write spells [Lvl: 5]
- History -- The ability to write (and rewrite) history [Lvl: 10]
- Written Creation -- The ability to write things into reality [Lvl: 20]

regards rooster
Topic archived. No new replies allowed.