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

Binary Search Tree (BST)

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)
16 views

Binary Search Tree (BST)

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

Binary Search Tree

To implement a Binary Search Tree (BST) in C that supports the following operations:

 Insertion
 Deletion
 Finding a node
 Finding the minimum and maximum values
 Displaying the tree structure
 Exiting the program

ALGORITHM

1. Insertion:
o Create a new node if the current node is NULL.
o Compare the new value with the current node's key.
o Recursively insert the value in the left or right subtree based on the
comparison.
2. Deletion:
o Search for the node to be deleted.
o If the node has no children, remove it directly.
o If the node has one child, replace it with its child.
o If the node has two children, find the in-order successor, replace the
node's key with the successor's key, and recursively delete the successor.
3. Find:
o Recursively search the tree based on key comparison.
4. Find Min:
o Traverse to the leftmost node in the tree.
5. Find Max:
o Traverse to the rightmost node in the tree.
6. Display:
o Perform an in-order traversal to print the tree in a hierarchical format
with indentation.
7. Exit:
o Free all allocated memory and terminate the program.

Theory

A Binary Search Tree (BST) is a hierarchical data structure where each node has up to
two children. In a BST:

 The left child contains keys less than the node's key.
 The right child contains keys greater than the node's key.
In-Order Traversal:

 Visits nodes in ascending order by recursively visiting the left subtree,


processing the node, and then visiting the right subtree.

PROGRAM

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

// Define a structure for a binary tree node


struct BinaryTreeNode {
int key;
struct BinaryTreeNode *left, *right;
};

// Function to create a new node with a given value


struct BinaryTreeNode* newNodeCreate(int value) {
struct BinaryTreeNode* temp = (struct BinaryTreeNode*)malloc(sizeof(struct
BinaryTreeNode));
temp->key = value;
temp->left = temp->right = NULL;
return temp;
}

// Function to insert a node with a specific value in the tree


struct BinaryTreeNode* insertNode(struct BinaryTreeNode* node, int value) {
if (node == NULL) {
return newNodeCreate(value);
}
if (value < node->key) {
node->left = insertNode(node->left, value);
} else if (value > node->key) {
node->right = insertNode(node->right, value);
}
return node;
}

// Function to find a node with a specific key in the tree


struct BinaryTreeNode* findNode(struct BinaryTreeNode* root, int target) {
if (root == NULL || root->key == target) {
return root;
}
if (root->key < target) {
return findNode(root->right, target);
}
return findNode(root->left, target);
}

// Function to find the minimum value node


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

// Function to find the maximum value node


struct BinaryTreeNode* findMax(struct BinaryTreeNode* root) {
while (root && root->right != NULL) {
root = root->right;
}
return root;
}

// Function to delete a node from the tree


struct BinaryTreeNode* deleteNode(struct BinaryTreeNode* root, int x) {
if (root == NULL) {
return NULL;
}

if (x > root->key) {
root->right = deleteNode(root->right, x);
} else if (x < root->key) {
root->left = deleteNode(root->left, x);
} else {
// Node with only one child or no child
if (root->left == NULL) {
struct BinaryTreeNode* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct BinaryTreeNode* temp = root->left;
free(root);
return temp;
}

struct BinaryTreeNode* temp = findMin(root->right);


root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
return root;
}
void inOrder(struct BinaryTreeNode* root) {
if (root != NULL) {
inOrder(root->left);
printf(" %d ", root->key);
inOrder(root->right);
}
}

// Function to display the tree structure


void displayTree(struct BinaryTreeNode* root, int space) {
if (root == NULL) {
return;
}

// Increase distance between levels


space += 10;

displayTree(root->right, space);

// Print current node after space


printf("\n");
for (int i = 10; i < space; i++) {
printf(" ");
}
printf("%d\n", root->key);
displayTree(root->left, space);
}

// Function to free the memory allocated for the tree


void freeTree(struct BinaryTreeNode* root) {
if (root != NULL) {
freeTree(root->left);
freeTree(root->right);
free(root);
}
}

// Main function to interact with the user


int main() {
struct BinaryTreeNode* root = NULL;
int choice, value;
struct BinaryTreeNode* temp;
while (1) {
printf("\n1. Insert\n2. Delete\n3. Find\n4. Find Min\n5. Find Max\n6. Display\n7.
Exit\nEnter Your Choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insertNode(root, value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
root = deleteNode(root, value);
break;
case 3:
printf("Enter value to find: ");
scanf("%d", &value);
temp = findNode(root, value);
if (temp != NULL) {
printf("Node with value %d found.\n", value);
} else {
printf("Node with value %d not found.\n", value);
}
break;
case 4:
temp = findMin(root);
if (temp != NULL) {
printf("Minimum value: %d\n", temp->key);
} else {
printf("Tree is empty.\n");
}
break;
case 5:
temp = findMax(root);
if (temp != NULL) {
printf("Maximum value: %d\n", temp->key);
} else {
printf("Tree is empty.\n");
}
break;
case 6:
printf("Tree structure:\n");
displayTree(root, 0);
printf("\n");
break;
case 7:
freeTree(root);
exit(0);
default:
printf("Invalid choice.\n");
}
}

return 0;
}

You might also like