0% found this document useful (0 votes)
12 views6 pages

Program No 15 Aim: Program Code:: Write A Program To Implement Binary Search Tree With All Its Operations

The document outlines a program to implement a Binary Search Tree (BST) with operations such as insertion, searching, deletion, and in-order traversal. It includes the C code for the BST implementation and demonstrates user interaction through a menu-driven interface. The program allows users to insert nodes, search for nodes, delete nodes, and display the tree's contents in order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Program No 15 Aim: Program Code:: Write A Program To Implement Binary Search Tree With All Its Operations

The document outlines a program to implement a Binary Search Tree (BST) with operations such as insertion, searching, deletion, and in-order traversal. It includes the C code for the BST implementation and demonstrates user interaction through a menu-driven interface. The program allows users to insert nodes, search for nodes, delete nodes, and display the tree's contents in order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Dronacharya College of Engineering APS&H Department

Program No 15
Aim: Write a Program to implement Binary Search Tree
with all its operations
Program Code:
#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

// Node structure

struct Node {

int info;

struct Node *left;

struct Node *right;

};

// Global pointers

struct Node *root = NULL;

struct Node *data = NULL;

// Function prototypes

struct Node *insert(struct Node *, int);

struct Node *search(struct Node *, int);

struct Node *delet(struct Node *, int);

Name: - Kritika Kadian(27095) Lab Name: - DSUC Lab Page No.: -


Dronacharya College of Engineering APS&H Department

struct Node *getMinimumNode(struct case 2:


Node *);
printf("\nEnter node to be
struct Node *getNode(int); searched: ");

void inorder(struct Node *); scanf("%d", &value);

data = search(root, value);

void main() { if (data == NULL)

int choice, value; printf("\nNode is not


available");
char ch;
else

printf("\nNode is available");
do {
break;
printf("\n1. Insert Node in BST");

printf("\n2. Search a Node in BST");


case 3:
printf("\n3. Delete a Node in BST");
printf("\nEnter the node to be
printf("\n4. InOrder Traversing"); deleted: ");
printf("\nEnter your choice: "); scanf("%d", &value);
scanf("%d", &choice); root = delet(root, value);

inorder(root);
switch (choice) { break;
case 1:

printf("\nEnter the data in case 4:


Node: ");
inorder(root);
scanf("%d", &value);
break;
root = insert(root, value);

break;
default:

Name: - Kritika Kadian(27095) Lab Name: - DSUC Lab Page No.: -


Dronacharya College of Engineering APS&H Department

printf("\nWrong Choice"); struct Node *search(struct Node *ptr,


int value) {
}
if (ptr == NULL || ptr->info == value)

return ptr;
printf("\nDo you want to continue
(y/n): "); if (value < ptr->info)

ch = getche(); return search(ptr->left, value);

} while (ch == 'y' || ch == 'Y'); else

} return search(ptr->right, value);

// Function to insert a node in BST

struct Node *insert(struct Node *node, // Function to delete a node from BST
int value) {
struct Node *delet(struct Node *ptr, int
if (node == NULL) { value) {

return getNode(value); if (ptr == NULL)

} else if (value < node->info) { return NULL;

node->left = insert(node->left,
value);
if (value < ptr->info) {
} else {
ptr->left = delet(ptr->left, value);
node->right = insert(node->right,
value); return ptr;

} } else if (value > ptr->info) {

return node; ptr->right = delet(ptr->right, value);

} return ptr;

// Function to search a node in BST


// Node to be deleted found

Name: - Kritika Kadian(27095) Lab Name: - DSUC Lab Page No.: -


Dronacharya College of Engineering APS&H Department

if (ptr->left == NULL && ptr->right == struct Node *temp = ptr;


NULL) {
while (temp->left != NULL)
free(ptr);
temp = temp->left;
return NULL;
return temp;
} else if (ptr->left == NULL) {
}
struct Node *temp = ptr->right;

free(ptr);
// In-order traversal
return temp;
void inorder(struct Node *ptr) {
} else if (ptr->right == NULL) {
if (ptr != NULL) {
struct Node *temp = ptr->left;
inorder(ptr->left);
free(ptr);
printf("|%d|", ptr->info);
return temp;
inorder(ptr->right);
} else {
}
struct Node *min =
getMinimumNode(ptr->right); }

ptr->info = min->info;

ptr->right = delet(ptr->right, min- // Function to allocate memory and


>info); create a new node

return ptr; struct Node *getNode(int value) {

} struct Node *temp;

} temp = (struct Node


*)malloc(sizeof(struct Node));

temp->info = value;
// Function to get node with minimum
value (in-order successor) temp->left = NULL;

struct Node *getMinimumNode(struct temp->right = NULL;


Node *ptr) { return temp; }

Name: - Kritika Kadian(27095) Lab Name: - DSUC Lab Page No.: -


Dronacharya College of Engineering APS&H Department

OUTPUT:
1. Insert Node in BST
2. Search a Node in BST
3. Delete a Node in BST
4. InOrder Traversing
Enter your choice: 1

Enter the data in Node: 10

Do you want to continue: y

1. Insert Node in BST


2. Search a Node in BST
3. Delete a Node in BST
4. InOrder Traversing
Enter your choice: 1

Enter the data in Node: 25

Do you want to continue: y

1. Insert Node in BST


2. Search a Node in BST
3. Delete a Node in BST
4. InOrder Traversing
Enter your choice: 4

|10||25|

Do you want to continue: y

Name: - Kritika Kadian(27095) Lab Name: - DSUC Lab Page No.: -


Dronacharya College of Engineering APS&H Department

1. Insert Node in BST 4. InOrder Traversing


2. Search a Node in BST Enter your choice: 4
3. Delete a Node in BST
4. InOrder Traversing |10||25||45||98|
Enter your choice: 1
Do you want to continue: y
Enter the data in Node: 98
1. Insert Node in BST
Do you want to continue: y 2. Search a Node in BST
3. Delete a Node in BST
1. Insert Node in BST 4. InOrder Traversing
2. Search a Node in BST Enter your choice: 3
3. Delete a Node in BST
4. InOrder Traversing Enter the node to be deleted: 25
Enter your choice: 1
|10||45||98|
Enter the data in Node: 45
Do you want to continue: n
Do you want to continue: y

1. Insert Node in BST


2. Search a Node in BST
3. Delete a Node in BST

Name: - Kritika Kadian(27095) Lab Name: - DSUC Lab Page No.: -

You might also like