iCas iCas -4 years ago 105
C++ Question

Binary Search Tree Program says root does not name a type

I am trying to execute the binary search tree on my C++ compiler but when i input the following code on the compiler i get the error saying 9 [Error] 'root' does not name a type and 19 [Error] expected unqualified-id before '{' token.I tried changing the pointer root to integer but it gives a conversion error.

#include<iostream>
using namespace std;
struct bstnode{
int data;
bstnode*left;
bstnode*right;
};
bstnode* root;
root = 0;
bstnode*getnewnode(int data)
{
bstnode* newnode=new bstnode();
newnode->data=data;
newnode->left=newnode->right=NULL;
return newnode;

}
void insert(bstnode*root,int data);
{
{

if(root==NULL)
root=getnewnode(data);

}
else if(data<=root->data)
{
root->left=insert(root->left,data);
}
else
{
root->right=insert(root->right,root);
}
return root;
}
bool search(bstnode*root,int data)
{
if(root==NULL) return false;
else if(root->data=data) return true;
else if(data<=root->data) return search(root->left,data);
else return search(root->right,data);
}
int main()
{
bstnode*root=NULL;
root=insert(root,15);
root=insert(root,10);
root=insert(root,12);
int number;
cout<<"enter no";
cin>>no;


}

Answer Source

You need to define the bstnode* root = NULL; rather than the way you did above

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download