D1-Practice-Exercise-12 Binary Search Tree
D1-Practice-Exercise-12 Binary Search Tree
D1-Slot-L37-L38 Lab
Exercise-12 Binary Search Tree -IPS
Name: Kathit Bhongale
Register no: 21BCE1786
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data ;
struct node* left;
struct node* right;
};
if(data< root->data)
root->left=insertdata(root->left,data);
return root;
}
struct node* inorder(struct node* root)
{
if(root==NULL)
return 0 ;
inorder(root->left);
printf("%d->",root->data);
inorder(root->right);
}
int main()
{
int n ;
int data ;
int forst;
scanf("%d",&n);
printf("enter the root element ");
scanf("%d",&forst);
struct node*root=NULL;
root=insertdata(root,forst);
for(int i=1;i<n;i++)
{
scanf("%d",&data);
insertdata(root,data);
}
inorder(root);
}
Output:
Q2). Program For Binary Search Tree Deletion
#include<stdio.h>
#include<stdlib.h>
struct node{
int key;
};
temp->key = item;
return temp;
}
if (root != NULL){
inordertraversal(root->left);
inordertraversal(root->right);
return node;
current = current->left;
return current;
else{
if (root->left == NULL){
free(root);
return temp;
free(root);
return temp;
root->key = temp->key;
return root;
int main(){
struct node *root = NULL;
inordertraversal(root);
printf("\nDelete 20\n");
inordertraversal(root);
printf("\nDelete 30\n");
inordertraversal(root);
printf("\nDelete 50\n");
inordertraversal(root);
return 0;
}
Output:
Q3) Write a C program to implement BST by inserting
50,30,75,20,45,90,100. Display the Inorder, Preorder traversal
and Postorder traversal
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data ;
struct node* left;
struct node* right;
};
if(data< root->data)
root->left=insert(root->left,data);
return root;
}
struct node* inorder(struct node* root)
{
if(root==NULL)
return 0 ;
inorder(root->left);
printf("%d->",root->data);
inorder(root->right);
}
struct node* preorder(struct node* root)
{
if(root==NULL)
return 0;
printf("%d->",root->data);
preorder(root->left);
preorder(root->right);
}
struct node* postorder(struct node* root)
{
if(root==NULL)
return 0;
postorder(root->left);
postorder(root->right);
printf("%d->",root->data);
}
int main()
{
int n ;
int data ;
int forst;
printf("enter the number of elements in the binary tree\n");
scanf("%d",&n);
printf("enter the root element ");
scanf("%d",&forst);
struct node*root=NULL;
root=insert(root,forst);
for(int i=1;i<n;i++)
{
scanf("%d",&data);
insert(root,data);
}
printf("Inorder ");
inorder(root);
printf("preorder ");
preorder(root);
printf("postorder ");
postorder(root);
}
Output: