Binary Tree Search

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#define MAX 100

struct node
{
int data;
struct node *left,*right;
}*b,*root=NULL,*x,*t;

void setright (struct node *,struct node *);
void setleft (struct node *,struct node *);

int count=0;
void main()
{
int i,n,fin,m;
char u;
clrscr();
printf ("\nEnter number of elements in list :");
scanf ("%d",&n);

	 for (;count<n;)
	{
	 count++;
	 if (count==1)
	 {
	  printf ("Enter element %d :",count);
	  scanf ("%d",&i);
	  root=(struct node *)malloc(sizeof (struct node));
	  root->left=NULL;
	  root->right=NULL;
	  root->data=i;
	  t=root;
	 }
	 else
	 {
	  printf ("Enter element %d :",count);
	  scanf ("%d",&i);
	  b=(struct node *)malloc(sizeof (struct node));
	  b->left=NULL;
	  b->right=NULL;
	  b->data=i;
	  if (b->data>=t->data)
	  setright(b,t);
	  if (b->data<t->data)
	  setleft(b,t);
	 }
	}
/*searching element*/
do
{
 printf ("\nEnter element to be searched :");
 scanf ("%d",&m);
 t=root;
 fin=0;
 while (t!=NULL)
 {
 if (m==t->data)
  {
  fin=1;
  break;
  }
 if (m<t->data)
 t=t->left;
 if (m>t->data)
 t=t->right;
 }
 if (fin==1)
 printf ("\nElement %d is exist in list.",m);
 else
 printf ("\nElement %d doesn't exist.",m);
 printf ("\nDo u want to continue (y/n) ? ");
 u=getche();
}while (u=='y' || u=='Y');
getch();
}

void setleft (struct node *b,struct node *t)
{
 while (b->data<t->data && t->left!=NULL)
 t=t->left;
 if (b->data<t->data && t->left==NULL)
 t->left=b;
}

void setright (struct node *b,struct node *t)
{
 while (b->data>=t->data && t->right!=NULL)
 t=t->right;
 if (b->data>=t->data && t->right==NULL)
 t->right=b;
}

Output

OUTPUT:

Enter number of elements in list :5
Enter element 1 :12
Enter element 2 :34
Enter element 3 :45
Enter element 4 :56
Enter element 5 :67

Enter element to be searched :89

Element 89 doesn't exist.
Do u want to continue (y/n) ? y
Enter element to be searched :45

Element 45 is exist in list.
Do u want to continue (y/n) 

Binary Seach Tree Algorithm
Title: Binary Seach Tree Algorithm (309 clicks)
Caption: Binary Seach Tree Algorithm
Filename: binarysearchtree.zip
Size: 2 KB