hi...i am really facing a problem in printing a "binary search tree which does an inorder traversal and searches for a node and insert a node"....so i have written a program for that but it doesn't print a binary search tree and is giving some strange grabage values i don't know why?so i would be really greateful if u guys can take a look at my program and can let me know what am i doing wrong?and can bring about necessary changes or modifications in my program.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *left,*right;
};
typedef struct node tree;
void create( tree **p,int d)
{
if(*p==NULL)
{
*p=(tree*)malloc(sizeof(tree));
(*p)->data=d;
(*p)->left=NULL;
(*p)->right=NULL;
}
else
{
if(d<(*p)->data)
{
create(&((*p)->left),d);
}
else
{
create(&((*p)->right),d);
}
}
}
void output(tree *p,int level)
{
int i;
if(p)
{
output(p->left,level+1);
printf("\n");
for(i=0;i<level;i++)
printf(" ");
printf("%d",p->data);
output(p->right,level+1);
}
}
int search(tree *p,int data)
{
int flag=0;
while(p!=NULL)
{
if(p->data==data)
{
flag=1;
return(flag);
}
else
if(data<p->data)
{
p=p->left;
}
else
{
p=p->right;
}
}
return (flag);
}
void main()
{
tree *first=NULL;
int flag;
int number=0;
char ch;int d;
int data;
clrscr();
printf("\ninput choice 'b' to break:");
ch=getchar();
while(ch!='b')
{
fflush(stdin);
printf("input information of the node:");
scanf("%d",&data);
fflush(stdin);
printf("\ninput choice 'b' to break:");
ch=getchar();
}
number--;
printf("number of elements in the list is %d",number+1);
create(&first,d);
printf("tree is:\n");
output(first,1);
fflush(stdin);
printf("input the information of the node which we want to search:");
scanf("%d",&data);
flag=search(first,data);
if(flag)
{
printf("search is successful");
}
else
{
printf("search is unsuccessful");
}
getch();
}
please help me out!!!!!!!!