0% found this document useful (0 votes)
230 views

D1-Practice-Exercise-12 Binary Search Tree

The document contains code for implementing binary search trees. It includes functions for creating nodes, inserting elements, and traversing the tree using inorder, preorder and postorder traversal. The main() function takes input elements, inserts them into an empty tree and prints the output of different traversal techniques.

Uploaded by

Mihir Oberoi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
230 views

D1-Practice-Exercise-12 Binary Search Tree

The document contains code for implementing binary search trees. It includes functions for creating nodes, inserting elements, and traversing the tree using inorder, preorder and postorder traversal. The main() function takes input elements, inserts them into an empty tree and prints the output of different traversal techniques.

Uploaded by

Mihir Oberoi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

BCSE202P Data Structures and Algorithms Lab

D1-Slot-L37-L38 Lab
Exercise-12 Binary Search Tree -IPS
Name: Kathit Bhongale
Register no: 21BCE1786

Q1). Program For Binary Search Tree Creation and Insertion

#include <stdio.h>
#include <stdlib.h>

struct node
{
int data ;
struct node* left;
struct node* right;

};

struct node* createnode(value)


{
struct node*newnode = malloc(sizeof(struct node));
newnode->data=value;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}

struct node* insertdata(struct node*root,int data)


{
if (root == NULL)
return createnode(data);

if(data< root->data)
root->left=insertdata(root->left,data);

else if (data> root->data)


root->right=insertdata(root->right,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;

struct node *left, *right;

};

struct node *newNode(int item){

struct node *temp = (struct node *)malloc(sizeof(struct node));

temp->key = item;

temp->left = temp->right = NULL;

return temp;
}

void inordertraversal(struct node *root){

if (root != NULL){

inordertraversal(root->left);

printf("%d ", root->key);

inordertraversal(root->right);

struct node* insert(struct node* node, int key){

if (node == NULL) return newNode(key);

if (key < node->key)

node->left = insert(node->left, key);


else

node->right = insert(node->right, key);

return node;

struct node * minValueNode(struct node* node){

struct node* current = node;

while (current && current->left != NULL)

current = current->left;

return current;

struct node* deleteNode(struct node* root, int key){


if (root == NULL) return root;

if (key < root->key)

root->left = deleteNode(root->left, key);

else if (key > root->key)

root->right = deleteNode(root->right, key);

else{

if (root->left == NULL){

struct node *temp = root->right;

free(root);

return temp;

else if (root->right == NULL){


struct node *temp = root->left;

free(root);

return temp;

struct node* temp = minValueNode(root->right);

root->key = temp->key;

root->right = deleteNode(root->right, temp->key);

return root;

int main(){
struct node *root = NULL;

root = insert(root, 50);

root = insert(root, 30);

root = insert(root, 20);

root = insert(root, 40);

root = insert(root, 70);

root = insert(root, 60);

root = insert(root, 80);

printf("Inorder traversal of the given tree \n");

inordertraversal(root);

printf("\nDelete 20\n");

root = deleteNode(root, 20);


printf("Inorder traversal of the modified tree \n");

inordertraversal(root);

printf("\nDelete 30\n");

root = deleteNode(root, 30);

printf("Inorder traversal of the modified tree \n");

inordertraversal(root);

printf("\nDelete 50\n");

root = deleteNode(root, 50);

printf("Inorder traversal of the modified tree \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;

};

struct node* createnode(value)


{
struct node*newnode = malloc(sizeof(struct node));
newnode->data=value;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
struct node* insert(struct node*root,int data)
{
if (root == NULL)
return createnode(data);

if(data< root->data)
root->left=insert(root->left,data);

else if (data> root->data)


root->right=insert(root->right,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:

You might also like