I am getting an error in *insert_nodes function:
error: expected constructor, destructor, or type conversion before ‘(‘ token
I also have a problem in the same function, it sais that i have to redeclare the ‘int nodes’ as a parameter in the function. but i think it’s not necessary to write it like this:
*insert_nodes(start, int nodes)
instead of being like this:
*insert_nodes(start,nodes)
Another error that a get is in getch(). While compiling in Netbeans, it just shows an error on that place but it doesn’t mention what type of error.
struct tree_traversal
{
int data;
tree_traversal *left; //left subtree
tree_traversal *right; //right subtree
};
tree_traversal *insert_nodes(tree_traversal *start, int nodes);
void preOrderTraversal(tree_traversal *start);
void postOrderTraversal(tree_traversal *start);
void inOrderTraversal(tree_traversal *start);
int counter = 1;
int main(int argc, char **argv)
{
int choice, nodes;
do
{
switch(choice)
{
case 1:
cout<<"\n\t\a\a Enter the Values:\a\a";
cin>>nodes;
start=insert_nodes(start,nodes);
break;
case 2:
cout<<"\n\t\a\a The Values for In-Order Tree traversal is: \a\a"<<endl;
preOrderTraversal(start);
break;
case 3:
cout<<"\n\t\a\a The Values for In-Order Tree traversal is: a\a"<<endl;
postOrderTraversal(start);
break;
case 4:
cout<<"\n\t\a\a The Values for In-Order Tree traversal is: \a\a"<<endl;
inOrderTraversal(start);
break;
case 5:
exit(0);
}
} while(choice != 5);
return 0;
}
tree_traversal *insert_nodes(tree_traversal *start, int nodes)
{
if(start == NULL)
{
start = new tree_traversal;
start ->left = start ->right = NULL;
start ->data = nodes;
counter++;
}
else if(counter%2 == 0)
start ->left = insert_nodes(start ->left,nodes);
else
start ->right = insert_nodes(start ->right,nodes);
return(start);
}
void preOrderTraversal(tree_traversal *start)
{
if(start != NULL)
{
cout<<start ->data;
preOrderTraversal(start->left);
preOrderTraversal(start->right);
getch();
}
}
void postOrderTraversal(tree_traversal *start)
{
if(start != NULL)
{
postOrderTraversal(start->left);
postOrderTraversal(start->right);
cout<<start ->data;
getch();
}
}
void inOrderTraversal(tree_traversal *start)
{
if(start != NULL)
{
inOrderTraversal(start->left);
cout<<start ->data;
inOrderTraversal(start->right);
getch();
}
}
should be
You forgot to mention the data type for the return type & function argument in function definition.
Yes, You need to specify the data types of the function arguments in the function definition.