Click here to Skip to main content
15,886,796 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<stdio.h>
struct bstnode{
	int data;
	struct bstnode* right;
	struct bstnode* left;
};
bstnode* getnewnode(int data){
	bstnode* newnode=(bstnode*)malloc(sizeof(struct bstnode));
	newnode->data=data;
	newnode->right=NULL;
	newnode->left=NULL;
	return newnode;
}
bstnode* 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,data);
		}
	return root;
}
int search(bstnode* root,int data){
	if(root==NULL) return(-1);
	else if(root->data ==data) return(1);
	else if(data <= root->data) return search(root->left,data);
	else return search(root->right,data);
}
void main(){
	struct bstnode* root=NULL;
	root=insert(root,15);root=insert(root,10);root=insert(root,20);
	root=insert(root,25);root=insert(root,8);root=insert(root,12);
	int number;
	printf("\n enter no. to be searched \n ");
	scanf("&d",&number);
	if(search(root,number)==1) printf("found \n ");
	else printf("not found \n");
	getch();
}


What I have tried:

I tried everything . help me out.
Posted
Updated 31-Mar-17 0:59am

Try the following code (and a more modern compiler)
C
#include<stdlib.h>
#include<stdio.h>
typedef struct tag_bstnode
{
  int data;
  struct tag_bstnode* right;
  struct tag_bstnode* left;
} bstnode;

bstnode* getnewnode(int data){
  bstnode* newnode=(bstnode*)malloc(sizeof(bstnode));
  newnode->data=data;
  newnode->right=NULL;
  newnode->left=NULL;
  return newnode;
}
bstnode* 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,data);
    }
  return root;
}
int search(bstnode* root,int data){
  if(root==NULL) return(-1);
  else if(root->data ==data) return(1);
  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,20);
  root=insert(root,25);root=insert(root,8);root=insert(root,12);
  int number;
  printf("\n enter no. to be searched \n ");
  scanf("%d",&number);
  if(search(root,number)==1) printf("found \n ");
  else printf("not found \n");
  getchar();
  return 0;
}
 
Share this answer
 
Quote:
I tried everything

I guess you didn't try the debugger. For further help, show some data and actual output, and explain how output is not correct (if necessary).

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
I think the problem lies in your insert routine, you overwrite the left or right nodes regardless of their existing content.

C#
else if(data<=root->data){
			root->left=insert(root->left,data);
		}else{
			root->right=insert(root->right,data);
		}


With the above if root->left already has a value in it you are overwriting it, whereas you need to traverse left until you either find an existing node equal or it is greater than data and the left node is null, then create a new node and store it in root->left.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900