Change
int bst::search(tree_node* root,int data) {
int r;
if(root == NULL) {
return 0;
}
else if (root != NULL){
if(root->data == data) {
return 1;
}
}
else if(data <= root->data) {
return search(root->left,data);
}
else {
return search(root->right,data);
}
}
to
int bst::search(tree_node* root,int data) {
int r;
if(root == NULL) {
return 0;
}
else if (root != NULL){
if(root->data == data) {
return 1;
}
else if(data <= root->data) {
return search(root->left,data);
}
else {
return search(root->right,data);
}
}
}
Of course, the issue would have been easier to spot if you had indented the code correctly:
int bst::search(tree_node* root,int data) {
int r;
if(root == NULL) {
return 0;
}
else if (root != NULL){
if(root->data == data) {
return 1;
}
}
else if(data <= root->data) {
return search(root->left,data);
}
else {
return search(root->right,data);
}
}
It would have been even more obvious if you had removed the redundand second test:
int bst::search(tree_node* root,int data) {
int r;
if(root == NULL) { return 0;
}
else { if(root->data == data) {
return 1;
}
}
else if(data <= root->data) { return search(root->left,data);
}
else {
return search(root->right,data);
}
}
Lessons learned:
1. format the code correctly to improve readability and the ability to spot errors before even compiling the code
2. check your conditional statements for redundant checks and have the compiler help you find logical errors
3. Once you fix the syntax error in the last version, the compiler will likely notice that there is a return missing (see solutuion 1); probably it didn't bother with an error message before because it realized it would never get to the end of the function anyway.