C Program to Implement a Binary search tree library with above six operations | DS - IProgramX

Q. Implement a Binary search tree library ( btree.h) with above six operations. Write a menu driven driver program to call the above functions


Program

#include<stdio.h>
#include<stdlib.h>
#include "btree.h"
int menu()
{
int ch;
system("clear");
printf("\n\t0.Exit\n\t1.Create BST\n\t2.Insert Node\n\t3.Search a node\n\t4.Display tree\n\tEnter you choice :");
scanf("%d",&ch);
return ch;
}
int main()
{
struct node *root= NULL;
int ch;
while((ch=menu())!=0)
{
if(ch==1)
{
createbst(&root);
}
else
if(ch==2)
{
int node;
printf("\n\tEnter data : ");
scanf("%d",&node);
insert(&root,node);
}
else
if(ch==3)
{
int node;
printf("\n\tEnter node to be searched : ");
scanf("%d",&node);
search(root,node);
getchar();getchar();
}

else
if(ch==4)
{
display(root);
getchar();getchar();
}
}
}

Library Function ( .h file )
NOTE: save file name as ' btree.h'

struct node
{
int data;
struct node *left,*right;
};
struct node * createnode(struct node *newnode,int data)
{
newnode=malloc(sizeof(struct node));
newnode->data=data;
newnode->left= newnode->right = NULL;
return newnode;
}
void insert(struct node **root,int data)
{
struct node *newnode;
newnode=createnode(newnode,data);
if(*root==NULL)
*root=newnode;
else
{
struct node *temp=*root;
while(1)
{
if(data <= temp->data)
{
if(temp->left==NULL)
{
temp->left=newnode;
break;
}
temp=temp->left;
}
else
{
if(temp->right==NULL)
{
temp->right=newnode;
break;
}
temp=temp->right;
}
}
}
}
void createbst(struct node **root)
{
int n,i;
int data;
printf("\n\tENter the how many nodes ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\tEnter data : ");
scanf("%d",&data);
insert(root,data);
}
}
void display(struct node *temp)
{
if(temp)
{
printf("%d\t",temp->data);
display(temp->left);
display(temp->right);
}
}
int search(struct node *temp,int data)
{
if(temp)
{
if(temp->data == data)

{
printf("data found");
return;
}
search(temp->left,data);
search(temp->right,data);
}
}

Output:

'clear' is not recognized as an internal or external command,
operable program or batch file.

        0.Exit
        1.Create BST
        2.Insert Node
        3.Search a node
        4.Display tree
        Enter you choice :1

        ENter the how many nodes 7

        Enter data : 3

        Enter data : 4

        Enter data : 5

        Enter data : 7

        Enter data : 8

        Enter data : 9

        Enter data : 1
'clear' is not recognized as an internal or external command,
operable program or batch file.

        0.Exit
        1.Create BST
        2.Insert Node
        3.Search a node
        4.Display tree
        Enter you choice :4
3       1       4       5       7       8       9       3
'clear' is not recognized as an internal or external command,
operable program or batch file.

        0.Exit
        1.Create BST
        2.Insert Node
        3.Search a node
        4.Display tree
        Enter you choice :3

        Enter node to be searched : 5
data found

Post a Comment

3 Comments