C Program for Binary Search Tree
Last Updated :
18 Nov, 2023
A binary Search Tree is a binary tree where the value of any node is greater than the left subtree and less than the right subtree. In this article, we will discuss Binary Search Trees and various operations on Binary Search trees using C programming language.
Properties of Binary Search Tree
Following are some main properties of the binary search tree in C:
- All nodes of the left subtree are less than the root node and nodes of the right subtree are greater than the root node.
- The In-order traversal of binary search trees gives the values in ascending order.
- All the subtrees of BST hold the same properties.
Example:
Binary Search TreeBinary Tree Structure in C
struct BinaryTreeNode {
int key;
struct nodeBinaryTreeNode *left, *right;
};
here,
- key: It will be the data stored.
- left: Pointer to the left child.
- right: Pointer to the right child
Operations on BST C
- Search in BST
- Insertion in BST
- Deletion in BST
1. Search Operation on BST in C
The algorithm for the search operation is:
- If the root is NULL or the key of the root matches the target:
- Return the current root node.
- If the target is greater than the key of the current root:
- Recursively call searchNode with the right subtree of the current root and the target.
- Return the result of the recursive call.
- If the target is smaller than the key of the current root:
- Recursively call searchNode with the left subtree of the current root and the target.
- Return the result of the recursive call.
- If the target is not found in the current subtree, return NULL.
where the target is the key to search for in the tree.
Time Complexity: O(log n)
2. Insertion in BST
The insertNode function inserts a new node with a specific value into a Binary Search Tree while maintaining the binary search tree property.
Algorithm
- If the current node is NULL
- Return a new node created with the specified value.
- If the value is less than the key of the current node:
- Recursively call insertNode with the left subtree of the current node and the value.
- Update the left child of the current node with the result of the recursive call.
- If the value is greater than the key of the current node:
- Recursively call insertNode with the right subtree of the current node and the value.
- Update the right child of the current node with the result of the recursive call.
- Return the current node (the root of the modified tree).
Time Complexity: O(log n)
4. Deletion in BST
There are few cases at the time of deleting a node in BST
a. Deleted node is the leaf node
In this case, simply delete the node from the tree.

b. Deleted node has a single child node
In this case, replace the target node with its child (single child) and then delete that child node.
-200.png)
c. Deleted node has two children
In this case, at first, we will find the inorder successor of that node. And then replace the node with the inorder successor. Then remove the inorder successor from its original position.
-200.png)
Algorithm
- If the root is NULL:
- Return NULL (base case for recursion).
- If the value to be deleted (x) is greater than the key of the current root:
- Recursively call delete with the right subtree of the current root and the value x.
- Update the right child of the current root with the result of the recursive call.
- If the value to be deleted (x) is smaller than the key of the current root:
- Recursively call delete with the left subtree of the current root and the value x.
- Update the left child of the current root with the result of the recursive call.
- If the value to be deleted (x) is equal to the key of the current root:
- If the current node has no child:
- Free the current node.
- Return NULL.
- If the current node has one child:
- Set temp to the non-null child of the current node.
- Free the current node.
- Return temp.
- If the current node has two children:
- Find the minimum node in the right subtree (temp).
- Replace the key of the current node with the key of temp.
- Recursively call delete with the right subtree of the current node and the key of temp.
- Return the current node (the root of the modified tree).
Time Complexity: O(log n)
C Program to Implement Binary Search Tree
C
// C program to implement binary search tree
#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 search for a node with a specific key in the
// tree
struct BinaryTreeNode*
searchNode(struct BinaryTreeNode* root, int target)
{
if (root == NULL || root->key == target) {
return root;
}
if (root->key < target) {
return searchNode(root->right, target);
}
return searchNode(root->left, target);
}
// 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 perform post-order traversal
void postOrder(struct BinaryTreeNode* root)
{
if (root != NULL) {
postOrder(root->left);
postOrder(root->right);
printf(" %d ", root->key);
}
}
// Function to perform in-order traversal
void inOrder(struct BinaryTreeNode* root)
{
if (root != NULL) {
inOrder(root->left);
printf(" %d ", root->key);
inOrder(root->right);
}
}
// Function to perform pre-order traversal
void preOrder(struct BinaryTreeNode* root)
{
if (root != NULL) {
printf(" %d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
// Function to find the minimum value
struct BinaryTreeNode* findMin(struct BinaryTreeNode* root)
{
if (root == NULL) {
return NULL;
}
else if (root->left != NULL) {
return findMin(root->left);
}
return root;
}
// Function to delete a node from the tree
struct BinaryTreeNode* delete (struct BinaryTreeNode* root,
int x)
{
if (root == NULL)
return NULL;
if (x > root->key) {
root->right = delete (root->right, x);
}
else if (x < root->key) {
root->left = delete (root->left, x);
}
else {
if (root->left == NULL && root->right == NULL) {
free(root);
return NULL;
}
else if (root->left == NULL
|| root->right == NULL) {
struct BinaryTreeNode* temp;
if (root->left == NULL) {
temp = root->right;
}
else {
temp = root->left;
}
free(root);
return temp;
}
else {
struct BinaryTreeNode* temp
= findMin(root->right);
root->key = temp->key;
root->right = delete (root->right, temp->key);
}
}
return root;
}
int main()
{
// Initialize the root node
struct BinaryTreeNode* root = NULL;
// Insert nodes into the binary search tree
root = insertNode(root, 50);
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
// Search for a node with key 60
if (searchNode(root, 60) != NULL) {
printf("60 found");
}
else {
printf("60 not found");
}
printf("\n");
// Perform post-order traversal
postOrder(root);
printf("\n");
// Perform pre-order traversal
preOrder(root);
printf("\n");
// Perform in-order traversal
inOrder(root);
printf("\n");
// Perform delete the node (70)
struct BinaryTreeNode* temp = delete (root, 70);
printf("After Delete: \n");
inOrder(root);
// Free allocated memory (not done in this code, but
// good practice in real applications)
return 0;
}
Output60 found
20 40 30 60 80 70 50
50 30 20 40 70 60 80
20 30 40 50 60 70 80
After Delete:
20 30 40 50 60 80
To know more about Binary Search Tree, refer to the article - Binary Search Tree
Similar Reads
Inorder predecessor in Binary Search Tree
Given a Binary Search Tree, the task is to find the In-Order predecessor of a given target key. In a Binary Search Tree (BST), the Inorder predecessor of a node is the previous node in the Inorder traversal of the BST. The Inorder predecessor is NULL for the first node in the Inorder traversal. Exam
10 min read
Binary Search Tree
A Binary Search Tree (or BST) is a data structure used in computer science for organizing and storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right chi
3 min read
Search a node in Binary Tree
Given a Binary tree and a key. The task is to search and check if the given key exists in the binary tree or not. Examples: Input: Output: TrueInput: Output: False Approach:The idea is to use any of the tree traversals to traverse the tree and while traversing check if the current node matches with
7 min read
Find Mode in Binary Search tree
Given a Binary Search Tree, find the mode of the tree. Note: Mode is the value of the node which has the highest frequency in the binary search tree. Examples: Input: 100 / \ 50 160 / \ / \ 50 60 140 170 Output: The mode of BST is 50Explanation: 50 is repeated 2 times, and all other nodes occur only
10 min read
Insertion in Binary Search Tree (BST)
Given a BST, the task is to insert a new node in this BST. Example: How to Insert a value in a Binary Search Tree:A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching for a key from the root until we hit a leaf node. Once a leaf node is f
15+ min read
Leaf nodes from Preorder of a Binary Search Tree
Given Preorder traversal of a Binary Search Tree. Then the task is to print leaf nodes of the Binary Search Tree from the given preorder. Examples: Input : preorder[] = {890, 325, 290, 530, 965};Output : 290 530 965Explanation: Below is the representation of BST using preorder array. Table of Conten
15+ min read
Minimum in a Binary Search Tree
Given the root of a Binary Search Tree. The task is to find the minimum valued element in this given BST. Example: Input: Output: 1Explanation: The minimum element in the given BST is 1. Input: Output: 2Explanation: The minimum element in the given BST is 2 Table of Content [Naive Approach] Using In
12 min read
Iterative Search for a key 'x' in Binary Tree
Given a Binary Tree and a key to be searched in it, write an iterative method that returns true if key is present in Binary Tree, else false. For example, in the following tree, if the searched key is 3, then function should return true and if the searched key is 12, then function should return fals
14 min read
Deletion in Binary Search Tree (BST)
Given a BST, the task is to delete a node in this BST, which can be broken down into 3 scenarios: Case 1. Delete a Leaf Node in BST Case 2. Delete a Node with Single Child in BST Deleting a single child node is also simple in BST. Copy the child to the node and delete the node. Case 3. Delete a Node
10 min read
Searching in Binary Search Tree (BST)
Given a BST, the task is to search a node in this BST. For searching a value in BST, consider it as a sorted array. Now we can easily perform search operation in BST using Binary Search Algorithm. Input: Root of the below BST Output: TrueExplanation: 8 is present in the BST as right child of rootInp
7 min read