Binary Search Tree (BST) Operations in C
Introduction to Binary Search Tree (BST) Program
      A Binary Search Tree (BST) is a node-based binary tree data structure that maintains the following properties:
Left Subtree: All nodes in the left subtree of a node contain values less than the node’s value.
Right Subtree: All nodes in the right subtree of a node contain values greater than the node’s value.
No Duplicate Nodes:Â Typically, BSTs do not contain duplicate values.
BSTs are widely used for efficient searching, insertion, and deletion operations, with an average time complexity of O(log n) for these operations, where n is the number of nodes.
This program will demonstrate how to:
Create a BSTÂ by inserting nodes.
Search for a value in the BST.
Traverse the BST using in-order traversal (which prints nodes in ascending order).
Program Structure
Node Structure:Â Each node contains:
data
: The value stored in the node.left
: Pointer to the left child.right
: Pointer to the right child.
Functions:
createNode(value)
: Creates a new node.insert(root, value)
: Inserts a value into the BST.search(root, value)
: Searches for a value in the BST.inOrderTraversal(root)
: Traverses the BST in in-order.
Steps to Implement :
Â
#include <stdio.h> #include <stdlib.h> // Define the structure of the BST node 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 = newNode->right = NULL; return newNode; } // Function to insert a new node in 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 for inorder traversal (Left-Root-Right) void inorderTraversal(struct Node* root) { if (root != NULL) { inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); } } // Function to search for a value in the BST and print its location int search(struct Node* root, int value) { if (root == NULL) { return 0; // Value not found } if (root->data == value) { printf("%d is found at the root.\n", value); return 1; // Value found at root } if (value < root->data) { if (search(root->left, value)) { printf("%d is found in the left subtree of %d.\n", value, root->data); return 1; } } else { if (search(root->right, value)) { printf("%d is found in the right subtree of %d.\n", value, root->data); return 1; } } return 0; // Value not found } int main() { struct Node* root = NULL; int values[] = {50, 30, 70, 20, 40, 60, 80}; int n = sizeof(values) / sizeof(values[0]); // Inserting values into BST for (int i = 0; i < n; i++) { root = insert(root, values[i]); } // Display the inorder traversal of the BST printf("Inorder Traversal of BST: "); inorderTraversal(root); printf("\n"); // Search for a value in the BST int searchValue; printf("Enter a value to search in the BST: "); scanf("%d", &searchValue); if (!search(root, searchValue)) { printf("%d is not found in the BST.\n", searchValue); } return 0; }
Â
Understanding the code
int data; // Stores the value of the node struct Node* left; // Pointer to the left child node struct Node* right; // Pointer to the right child node };
In the above code :Â
int data;
This is the data field of the node.
It stores the integer value associated with the node.
For example, if the node represents the valueÂ
50
, thenÂdata = 50
.
struct Node* left;
This is a pointer to the left child of the current node.
In a BST, the left child node contains a value less than the current node’s value.
If there is no left child, this pointer will beÂ
NULL
.
struct Node* right;
This is a pointer to the right child of the current node.
In a BST, the right child node contains a value greater than the current node’s value.
If there is no right child, this pointer will beÂ
NULL
.
 Â
struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Allocate memory for the new node newNode->data = value; // Set the data field of the node newNode->left = newNode->right = NULL; // Initialize left and right pointers to NULL return newNode; // Return the newly created node }
Â
In the above code :Â
The createNode
 function is responsible for dynamically creating a new node for a Binary Search Tree (BST).Â
Function Signature:
 struct Node* createNode(int value)ÂThe function is namedÂ
createNode
.It takes an integerÂ
value
 as input, which will be stored in the new node.It returns a pointer to the newly created node (
struct Node*
).
Memory Allocation:
Âstruct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
malloc
 is used to dynamically allocate memory for the new node.sizeof(struct Node)
 calculates the size of theÂNode
 structure in bytes.(struct Node*)
 is a typecast to ensure the pointer returned byÂmalloc
 is of typeÂstruct Node*
.newNode
 is a pointer to the newly allocated memory block.
Set the Data Field:
ÂnewNode->data = value;
TheÂ
data
 field of the new node is assigned the inputÂvalue
.For example, ifÂ
value = 50
, thenÂnewNode->data = 50
.
Initialize Left and Right Pointers:
ÂnewNode->left = newNode->right = NULL;
TheÂ
left
 andÂright
 pointers of the new node are initialized toÂNULL
.This indicates that the new node has no children (it is a leaf node initially).
Return the New Node:
Âreturn newNode;
The function returns the pointer to the newly created node.
This allows the calling function to use the new node (e.g., to insert it into the BST).
struct Node* insert(struct Node* root, int value) { if (root == NULL) { return createNode(value); // Create a new node if the tree is empty } if (value < root->data) { root->left = insert(root->left, value); // Insert in the left subtree } else if (value > root->data) { root->right = insert(root->right, value); // Insert in the right subtree } return root; // Return the (possibly updated) root of the tree }
 In above code :Â
     The insert
 function is used to insert a new node with a given value into a Binary Search Tree (BST). It ensures that the BST property is maintained:
All values in the left subtree of a node are less than the node’s value.
All values in the right subtree of a node are greater than the node’s value.
Function Signature:
Âstruct Node* insert(struct Node* root, int value)
The function is namedÂ
insert
.It takes two arguments:
root
: A pointer to the root of the BST (or subtree).value
: The integer value to be inserted into the BST.
It returns a pointer to the root of the BST (or subtree) after insertion.
Base Case:
Âif (root == NULL) { return createNode(value); }
If the currentÂ
root
 isÂNULL
, it means the tree is empty (or we’ve reached a leaf node’s child position).In this case, a new node is created using theÂ
createNode
 function, and the pointer to this new node is returned.
Recursive Case:
Âif (value < root->data) { root->left = insert(root->left, value); // Insert in the left subtree } else if (value > root->data) { root->right = insert(root->right, value); // Insert in the right subtree }
If theÂ
value
 is less than the current node’s data (root->data
), the function recursively inserts the value into the left subtree.If theÂ
value
 is greater than the current node’s data, the function recursively inserts the value into the right subtree.The recursive calls ensure that the value is inserted at the correct position to maintain the BST property.
Return the Root:
Âreturn root;
After inserting the value (if necessary), the function returns theÂ
root
 of the current subtree.This ensures that the tree structure is preserved and updated correctly.
void inorderTraversal(struct Node* root) { if (root != NULL) { inorderTraversal(root->left); // Traverse the left subtree printf("%d ", root->data); // Visit the root node inorderTraversal(root->right); // Traverse the right subtree } }
 In above code :Â
     The inorderTraversal
 function is used to traverse a Binary Search Tree (BST) in in-order fashion. In-order traversal visits the nodes in the following order:
Left Subtree:Â Traverse the left subtree.
Root Node:Â Visit the root node.
Right Subtree:Â Traverse the right subtree.
For a BST, in-order traversal prints the nodes in ascending order.
Function Signature:
Âvoid inorderTraversal(struct Node* root)
The function is namedÂ
inorderTraversal
.It takes a single argument:Â
root
, which is a pointer to the root of the BST (or subtree).It does not return any value (
void
).
Base Case:
Âif (root != NULL) {
If the current node (
root
) isÂNULL
, the function stops and returns. This is the base case for the recursion.If the node is notÂ
NULL
, the function proceeds to traverse the tree.
Traverse the Left Subtree:
ÂinorderTraversal(root->left);
The function recursively calls itself on the left child of the current node.
This ensures that all nodes in the left subtree are visited before the current node.
Visit the Root Node:
Âprintf("%d ", root->data);
The value of the current node (
root->data
) is printed.This is the “visit” step in the in-order traversal.
Traverse the Right Subtree:
ÂinorderTraversal(root->right);
The function recursively calls itself on the right child of the current node.
This ensures that all nodes in the right subtree are visited after the current node.
int search(struct Node* root, int value) { if (root == NULL) { return 0; // Value not found } if (root->data == value) { printf("%d is found at the root.\n", value); return 1; // Value found at root } if (value < root->data) { if (search(root->left, value)) { printf("%d is found in the left subtree of %d.\n", value, root->data); return 1; } } else { if (search(root->right, value)) { printf("%d is found in the right subtree of %d.\n", value, root->data); return 1; } } return 0; // Value not found }
 In above code :Â
The search
 function is used to search for a value in a Binary Search Tree (BST) and print its location if found. It leverages the BST property to efficiently locate the value and provides information about where the value is found (e.g., at the root, in the left subtree, or in the right subtree). Â
Function Signature:
Âint search(struct Node* root, int value)
The function is namedÂ
search
.It takes two arguments:
root
: A pointer to the root of the BST (or subtree).value
: The integer value to search for in the BST.
It returns an integer:
1
 if the value is found.0
 if the value is not found.
Base Case:
Âif (root == NULL) { return 0; // Value not found }
If the current node (
root
) isÂNULL
, it means the value is not found in the tree (or subtree), so the function returnsÂ0
.
Value Found at Root:
Âif (root->data == value) { printf("%d is found at the root.\n", value); return 1; // Value found at root }
If the current node’s data (
root->data
) matches the search value, the function prints that the value is found at the root and returnsÂ1
.
Search in the Left Subtree:
Âif (value < root->data) { if (search(root->left, value)) { printf("%d is found in the left subtree of %d.\n", value, root->data); return 1; } }
If the search value is less than the current node’s data, the function recursively searches the left subtree.
If the value is found in the left subtree, it prints the location (e.g., “found in the left subtree ofÂ
<parent>
“) and returnsÂ1
.
Search in the Right Subtree:
Âelse { if (search(root->right, value)) { printf("%d is found in the right subtree of %d.\n", value, root->data); return 1; } }
If the search value is greater than the current node’s data, the function recursively searches the right subtree.
If the value is found in the right subtree, it prints the location (e.g., “found in the right subtree ofÂ
<parent>
“) and returnsÂ1
.
Value Not Found:
Âreturn 0; // Value not found
If the value is not found in the tree, the function returnsÂ
0
.
Â
The main
 function is the entry point of the program. It demonstrates how to:
Create a BSTÂ by inserting values.
Traverse the BSTÂ using in-order traversal.
Search for a value in the BST.
Let’s break down the code step by step:
struct Node* root = NULL; // Initialize the root of the BST as NULL int values[] = {50, 30, 70, 20, 40, 60, 80}; // Array of values to insert into the BST int n = sizeof(values) / sizeof(values[0]); // Calculate the number of values in the array // Inserting values into BST for (int i = 0; i < n; i++) { root = insert(root, values[i]); // Insert each value into the BST } // Display the inorder traversal of the BST printf("Inorder Traversal of BST: "); inorderTraversal(root); // Perform in-order traversal printf("\n"); // Search for a value in the BST int searchValue; printf("Enter a value to search in the BST: "); scanf("%d", &searchValue); // Read the value to search if (!search(root, searchValue)) { printf("%d is not found in the BST.\n", searchValue); // Print if the value is not found } return 0; // End of program }
and finally in the main function in above code Â
Initialize the BST:
cCopystruct Node* root = NULL;
TheÂ
root
 pointer is initialized toÂNULL
, indicating that the BST is initially empty.
Array of Values:
cCopyint values[] = {50, 30, 70, 20, 40, 60, 80}; int n = sizeof(values) / sizeof(values[0]);
An arrayÂ
values
 is defined, containing the values to be inserted into the BST.The variableÂ
n
 is calculated as the number of elements in theÂvalues
 array.
Insert Values into the BST:
cCopyfor (int i = 0; i < n; i++) { root = insert(root, values[i]); }
AÂ
for
 loop is used to insert each value from theÂvalues
 array into the BST.TheÂ
insert
 function is called for each value, and theÂroot
 of the BST is updated.
In-Order Traversal:
cCopyprintf("Inorder Traversal of BST: "); inorderTraversal(root); printf("\n");
TheÂ
inorderTraversal
 function is called to traverse the BST in in-order (left-root-right) and print the nodes in ascending order.The result is displayed as:Â
Inorder Traversal of BST: 20 30 40 50 60 70 80
.
Search for a Value:
cCopyint searchValue; printf("Enter a value to search in the BST: "); scanf("%d", &searchValue);
The user is prompted to enter a value to search for in the BST.
The value is stored in the variableÂ
searchValue
.
Perform the Search:
cCopyif (!search(root, searchValue)) { printf("%d is not found in the BST.\n", searchValue); }
TheÂ
search
 function is called to search for the value in the BST.If the value is not found (
search
 returnsÂ0
), a message is printed:Â<value> is not found in the BST.
End of Program:
cCopyreturn 0;
The program ends by returningÂ
0
, indicating successful execution.