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;
char menu;
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;
switch(menu)
{
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;
}
}
}