0% found this document useful (0 votes)
14 views5 pages

Experiment No 7 Write A C Program To Implement Binary Search Tree ADT Using Singly Linked List. Code

Uploaded by

bobby.valsaraj24
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)
14 views5 pages

Experiment No 7 Write A C Program To Implement Binary Search Tree ADT Using Singly Linked List. Code

Uploaded by

bobby.valsaraj24
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/ 5

EXPERIMENT NO 7

Write a C Program to Implement Binary Search Tree ADT using


Singly Linked list.

CODE

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

// Define the structure of a node


struct Node {
int value;
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));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->value = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

// Function to insert a value into the BST


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

if (value < root->value) {


root->left = insert(root->left, value);
} else if (value > root->value) {
root->right = insert(root->right, value);
}
return root;
}

// Function to search for a value in the BST


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

if (value < root->value) {


return search(root->left, value);
} else {
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 value from the BST


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

if (value < root->value) {


root->left = delete(root->left, value);
} else if (value > root->value) {
root->right = delete(root->right, value);
} else {
// Node to be deleted is found
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 has two children


struct Node* temp = findMin(root->right);
root->value = temp->value;
root->right = delete(root->right, temp->value);
}
return root;
}

// Function to perform in-order traversal of the BST


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

// Main function to test the BST


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

// Insert values into the BST


root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 70);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 60);
root = insert(root, 80);

// In-order traversal
printf("In-order traversal: ");
inorderTraversal(root);
printf("\n");

// Search for a value


int searchValue = 40;
struct Node* searchResult = search(root, searchValue);
if (searchResult != NULL) {
printf("Node with value %d found in the BST.\n", searchValue);
} else {
printf("Node with value %d not found in the BST.\n", searchValue);
}

// Delete a node
int deleteValue = 20;
root = delete(root, deleteValue);
printf("In-order traversal after deleting %d: ", deleteValue);
inorderTraversal(root);
printf("\n");

deleteValue = 30;
root = delete(root, deleteValue);
printf("In-order traversal after deleting %d: ", deleteValue);
inorderTraversal(root);
printf("\n");

return 0;
}

OUTPUT

You might also like