0% found this document useful (0 votes)
3 views2 pages

prog8

This document contains a C program that implements a Binary Search Tree (BST) with functionalities for insertion, searching, and traversal (inorder, preorder, postorder). The program allows users to interactively insert data, search for elements, and display the tree in different traversal orders. It includes error handling for duplicate entries and prompts for user choices in a loop until exit is selected.

Uploaded by

manisha.rk04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

prog8

This document contains a C program that implements a Binary Search Tree (BST) with functionalities for insertion, searching, and traversal (inorder, preorder, postorder). The program allows users to interactively insert data, search for elements, and display the tree in different traversal orders. It includes error handling for duplicate entries and prompts for user choices in a loop until exit is selected.

Uploaded by

manisha.rk04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#include<stdlib.h>
struct BST{
int data;
struct BST* left, *right;
};
typedef struct BST NODE;
NODE *Createtree(NODE*node,int data)
{
if(node==NULL){
NODE*temp= (NODE*)calloc(1,sizeof(NODE));
temp->data=data;
return temp;
}
if(data<(node->data))
node->left= Createtree(node->left,data);
else if(data> node->data)
node->right= Createtree(node->right,data);
else
printf("Duplicate data found");
return node;
}
NODE* Search(NODE* node,int data){
if(node==NULL)
printf("Element not found");
else if(data<node->data)
node->left= Search(node->left,data);
else if (data>node->data)
node->right =Search(node-> right,data);
else
printf("\n Element found: %d",node->data);
return node;
}
void Inorder(NODE*node){
if(node!=NULL){
Inorder(node-> left);
printf("%d\t",node->data);
Inorder(node-> right);
}}
void Preorder(NODE*node)
{
if (node!=0)
{
printf("%d\t",node->data);
Preorder(node-> left);
Preorder(node-> right);
}}
void Postorder(NODE*node)
{
if (node!=0)
{

Postorder(node-> left);
Postorder(node-> right);
printf("%d\t",node->data);
}
}
int main()
{
int data,ch,i;
NODE*root=NULL;
printf("Program: Binary Search tree \n Name: Manisha.K\n USN: 4SN21IS014");
do
{
printf("\n1.Insertion BST\n2.Search element in BST\n3.Inorder\n4.Preorder\
n5.Postorder\nExit\n");
printf("Enter you choice");
scanf("%d",&ch);
switch(ch)
{
case 1:while(1)
{
printf("Enter the data to insert");
scanf("%d",&data);
root=Createtree(root,data);
printf("enter 0 to stop and 1 to continue");
scanf("%d",&ch);
if(ch==0)
break;
}break;
case 2: printf("Enter the element to search");
scanf("%d",&data);
root=Createtree(root,data);
break;
case 3:
printf("\n Inorder traversal:\n");
Inorder(root);
break;
case 4: printf("\n Inorder traversal:\n");
Preorder(root);
break;
case 5: printf("\n Inorder traversal:\n");
Postorder(root);
break;
case 6:exit(0);
default:printf("\n wrong option");
break;
}
}
while(1);
}

You might also like