You are on page 1of 3

#include <stdio.

h>
#include <stdlib.h>
struct node
{
int data;
int left;
int right;
};
void cal_binarytree(struct node **start, int data)
{
struct node *temp=NULL;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->left=NULL;
temp->right=NULL;

struct node *curr;


curr=*start;
if(*start==NULL)
{
*start=temp;

}
else
{
while(15)
{
if(data > curr->data)
{
if(curr->right==NULL)
{curr->right=temp;
break;}
else
{
curr=curr->right;

}
else
{ if(curr->left==NULL)
{
curr->left=temp;
break;}
else
{
curr=curr->left;
}
}

}
curr=temp;
}

}
void search(int vts, struct node **start)
{
struct node *temp=NULL;
temp=*start;
if(*start==NULL)
{printf("The tree is empty");
return;
}
else if(vts==(*start)->data)
{
printf("Value found at start");
return;
}
else
{
while(temp!=NULL)
{
if(vts > temp->data)
{if(temp->right==NULL)
break;
else
{
temp=temp->right;
}

}
else
{if(temp->left!=NULL)
temp=temp->left;
else
break;
}
if(vts==temp->data)
{
if(temp->right==NULL && temp->left ==NULL)
printf("value found at leaf");
else
printf("Value found at internal");
return;
}
}
}

printf("Value not found");


}

/* run this program using the console pauser or add your own getch, system("pause")
or input loop */

int main(int argc, char *argv[]) {

struct node *start=NULL;


printf("Enter the no of elements you want to enter\n");
int size;
scanf("%d",&size);

int i;
for(i=0; i<size; i++)
{
int input;
scanf("%d",&input);
cal_binarytree(&start, input);

}
printf("Enter the element to search in tree\n");
int vts=0;
scanf("\n%d",&vts);
search(vts,&start);
return 0;
}

You might also like