# Thread: Problem for Binary Tree code , Help

1. ## Problem for Binary Tree code , Help

Code:
```The error seems like the root from the class Tree can't read in the class Tree_node
and the root simulation follows ,
follows are the error message:
Tree_node in class Tree does not name a type
as follows : shows the root can't use

////Binary Search Tree
//
#include <iostream>
#include <string>
using namespace std;

class Tree{
Tree_node *root;

};
class Tree_node:public Tree{

public:
Tree_node(){root=NULL;};
~Tree_node(){delete [] root;};
void insert(string name);
void print_Name(Tree_node *tree);

private:
string name;
Tree_node *left;
Tree_node *right;
};

void Tree_node::insert(string new_name)
{
Tree_node* temp = new Tree_node;
temp->name = new_name;
temp->left = NULL;
temp->right = NULL;
Tree_node* parents;

if (root==NULL) {

root  = temp;
}
else
{
Tree_node* curnt;
curnt = root;
while(curnt)
{
parents = curnt;
if (temp->name>curnt->name)
curnt = curnt->right;
else
curnt = curnt->left;

}
if (temp->name>parents->name)
parents->right = temp;
else
parents->left=temp;

}

}

void Tree_node::print_Name(Tree_node *tree)
{
if (tree==NULL) return;

if (tree!=NULL)
{
if(tree->left)
{
print_Name(tree->left);
}

else {
print_Name(tree->right);

}

cout<<"  " << tree->name<<" ";
}
}

int main(){
Tree_node t;
string tmp;
while(true)
{
cout<<"Followings are the Menu for Sort the Name\n";
cout<<"---------------------------------------------"<<endl;
cout<<"a. Insert the names."<<endl;
cout<<"b. Print out the names in Alphabetically order."<<endl;
cout<<endl<<endl;
cout<<"Enter the choice:   "<<endl;

{
case 1:
cout<<"Enter name to be inserted:";
cin>>tmp;
t.insert(tmp);
break;
case 2:
cout<<"Alphabetically Order"<<endl;
t.print_Name(tmp);
break;
}
}
}```

2. what about some explanation, errors you get etc?

//yeah, the edit helped

3. Originally Posted by std10093
what about some explanation, errors you get etc?

//yeah, the edit helped

Any thoughts of that ?
I put the insert method inside of the Tree class

4. Tree will have to be implemented further down, on a line past Tree_node. And since a Tree is implemented in terms of Tree_node I think you are meant to use Tree in the main() function. Tree_node is not meant to be the interface of your data structure.

I do not think inheritance is necessary.

5. Lets start with fixing line 29. It has the wrong version of delete.
delete[] matches new[] and delete matches new.

6. Hmm, looking at this in more detail, its a lot worse than that.
You don't seem to understand polymorphism. You currently have it that every Tree_node is a Tree, but what you need to have is either Tree_node and Tree are totally separate classes, or it could be the case that Tree contains Tree_node.
Then think about which methods belong in which class.

You're not going to get anything at all working until you get that right.