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

Write A Program To Implement I) Binary Search Tree

The document provides a C program that implements a Binary Search Tree (BST) with functionalities for inserting, searching, deleting nodes, and performing in-order traversal. It defines a structure for the nodes and includes functions to handle the various operations on the BST. The main function allows users to interactively choose operations until they decide to exit the program.

Uploaded by

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

Write A Program To Implement I) Binary Search Tree

The document provides a C program that implements a Binary Search Tree (BST) with functionalities for inserting, searching, deleting nodes, and performing in-order traversal. It defines a structure for the nodes and includes functions to handle the various operations on the BST. The main function allows users to interactively choose operations until they decide to exit the program.

Uploaded by

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

8.

Write a program to implement


i) Binary Search tree

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

// Define the structure for a node in the BST


struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

// Function to insert a node into the BST


struct Node* insert(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}

// Function to perform in-order traversal


void inOrder(struct Node* root) {
if (root != NULL) {
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
}

// Function to search for a value in the BST


struct Node* search(struct Node* root, int value) {
if (root == NULL || root->data == value) {
return root;
}
if (value < root->data) {
return search(root->left, value);
}
return search(root->right, value);
}

// Function to find the minimum value node in the BST


struct Node* findMin(struct Node* root) {
while (root->left != NULL) {
root = root->left;
}
return root;
}

// Function to delete a node from the BST


struct Node* deleteNode(struct Node* root, int value) {
if (root == NULL) {
return root;
}

if (value < root->data) {


root->left = deleteNode(root->left, value);
} else if (value > root->data) {
root->right = deleteNode(root->right, value);
} else {
// Node with only one child or no child
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;
}

// Node with two children: Get the inorder successor (smallest in the right
subtree)
struct Node* temp = findMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}

// Main function
int main() {
struct Node* root = NULL;
int choice, value;

do {
printf("\nBinary Search Tree Operations\n");
printf("1. Insert\n");
printf("2. In-order Traversal\n");
printf("3. Search\n");
printf("4. Delete\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;

case 2:
printf("In-order Traversal: ");
inOrder(root);
printf("\n");
break;

case 3:
printf("Enter value to search: ");
scanf("%d", &value);
if (search(root, value)) {
printf("Value found in the BST.\n");
} else {
printf("Value not found in the BST.\n");
}
break;

case 4:
printf("Enter value to delete: ");
scanf("%d", &value);
root = deleteNode(root, value);
break;

case 5:
printf("Exiting...\n");
break;

default:
printf("Invalid choice! Try again.\n");
}
} while (choice != 5);

return 0;
}

You might also like