/*PROGRAM TO SEARCHES AN ITEM FROM BINARY SEARCH TREE*/
#include<stdio.h>
#include<malloc.h>
struct tree
{
int Info;
struct tree *l_child;
struct tree *r_child;
};
int flag = 0;
int search_item(struct tree*, int);
struct tree *create_tree (int , struct tree *);
void display(struct tree *, int );
/* Function to create a tree */
struct tree * create_tree (int item, struct tree *node)
{
if (node == NULL)
{
node = (struct tree *) malloc( sizeof(struct tree ));
node->Info = item;
node->l_child = NULL;
node->r_child = NULL;
return (node);
}
if (item < node->Info)
/* Smaller item becomes left child */
node->l_child = create_tree (item, node->l_child);
else
/* Greater or equal item becomes right child */
node->r_child = create_tree (item, node->r_child);
return(node);
}
/* Function to display tree */
void display(struct tree *T, int level)
{
int i;
if (T)
{
display(T->r_child, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf("\t");
printf("|----%d", T->Info);
printf("\n");
display(T->l_child, level+1);
}
}
/* Function to search an item */
int search_item(struct tree *node, int item)
{
while (node != NULL)
{
if (node->Info == item)
{
flag = 1;
return(flag);
}
else
if(item < node->Info)
{
node = node->l_child;
}
else
{
node = node->r_child;
}
}
return(flag);
}/* Main Function */
void main()
{
int flag,n,i=0,item;
struct tree *T = (struct tree *) malloc(sizeof(struct tree));
clrscr();
T = NULL;
printf("\n\n\n -------------------------------------");
printf("\n How many nodes you want to create:");
scanf("%d",&n);
while(i != n)
{
printf("\n\n ----------------------------------");
printf("\n Enter item you want to insert: ");
scanf("%d", &item);
T = create_tree (item,T);
printf("\n\n\t----------");
printf("\n\t Tree is: ");
printf("\n\t----------\n\n");
display(T, 1);
i++;
}
printf("\n\n ----------------------------------");
printf("\n Enter the item you want to search: ");
scanf("%d", &item);
flag = search_item(T, item);
if (flag)
{
printf("\n\n\n\t\t --------------------");
printf("\n\t\t Search is successful");
printf("\n\t\t --------------------");
}
else
{ printf("\n\n\n\t\t --------------------");
printf("\n\t\t Search unsuccessful");
printf("\n\t\t --------------------");
}
getch();
}
#include<malloc.h>
struct tree
{
int Info;
struct tree *l_child;
struct tree *r_child;
};
int flag = 0;
int search_item(struct tree*, int);
struct tree *create_tree (int , struct tree *);
void display(struct tree *, int );
/* Function to create a tree */
struct tree * create_tree (int item, struct tree *node)
{
if (node == NULL)
{
node = (struct tree *) malloc( sizeof(struct tree ));
node->Info = item;
node->l_child = NULL;
node->r_child = NULL;
return (node);
}
if (item < node->Info)
/* Smaller item becomes left child */
node->l_child = create_tree (item, node->l_child);
else
/* Greater or equal item becomes right child */
node->r_child = create_tree (item, node->r_child);
return(node);
}
/* Function to display tree */
void display(struct tree *T, int level)
{
int i;
if (T)
{
display(T->r_child, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf("\t");
printf("|----%d", T->Info);
printf("\n");
display(T->l_child, level+1);
}
}
/* Function to search an item */
int search_item(struct tree *node, int item)
{
while (node != NULL)
{
if (node->Info == item)
{
flag = 1;
return(flag);
}
else
if(item < node->Info)
{
node = node->l_child;
}
else
{
node = node->r_child;
}
}
return(flag);
}/* Main Function */
void main()
{
int flag,n,i=0,item;
struct tree *T = (struct tree *) malloc(sizeof(struct tree));
clrscr();
T = NULL;
printf("\n\n\n -------------------------------------");
printf("\n How many nodes you want to create:");
scanf("%d",&n);
while(i != n)
{
printf("\n\n ----------------------------------");
printf("\n Enter item you want to insert: ");
scanf("%d", &item);
T = create_tree (item,T);
printf("\n\n\t----------");
printf("\n\t Tree is: ");
printf("\n\t----------\n\n");
display(T, 1);
i++;
}
printf("\n\n ----------------------------------");
printf("\n Enter the item you want to search: ");
scanf("%d", &item);
flag = search_item(T, item);
if (flag)
{
printf("\n\n\n\t\t --------------------");
printf("\n\t\t Search is successful");
printf("\n\t\t --------------------");
}
else
{ printf("\n\n\n\t\t --------------------");
printf("\n\t\t Search unsuccessful");
printf("\n\t\t --------------------");
}
getch();
}
No comments:
Post a Comment