Binary Search Tree
June 2025
1 Binary Search Tree (BST) - Detailed Notes
A Binary Search Tree (BST) is a special type of binary tree where each node
follows a particular property: for each node, all the elements in the left subtree
are less than the node’s value, and all the elements in the right subtree are
greater than the node’s value. This property ensures that the tree is sorted,
and it provides efficient operations such as search, insertion, and deletion.
1.1 Properties of Binary Search Tree
• Left Subtree: The value of every node in the left subtree is less than
the value of the parent node.
• Right Subtree: The value of every node in the right subtree is greater
than the value of the parent node.
• No duplicates: A valid BST does not contain duplicate values.
• Recursion: A BST can be represented recursively, where each subtree is
itself a BST.
1.2 Applications
• Searching for a value in logarithmic time.
• In-order traversal results in sorted order.
• Can be used to implement dynamic sets and lookup tables.
2 1. Binary Search Tree Structure (using struct)
In C++, a struct can be used to represent a node of a binary tree. Each node
has three parts: the data (value), a pointer to the left child, and a pointer to
the right child.
1
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
// Constructor to initialize a new node
Node(int val) {
data = val;
left = nullptr;
right = nullptr;
}
};
3 2. Insertion in Binary Search Tree
Insertion in a Binary Search Tree is done by finding the appropriate position
for the new value. We start at the root and recursively move left or right based
on whether the new value is smaller or greater than the current node’s value.
This ensures that the tree remains sorted.
3.1 Insertion Algorithm
1. Start at the root node.
2. If the new value is smaller than the current node’s data, move to the left
child.
3. If the new value is larger than the current node’s data, move to the right
child.
4. If you reach a NULL pointer, place the new node there.
3.2 Insertion Code
// Function to insert a new node in the BST
Node* insert(Node* root, int data) {
// If the tree is empty, create a new node and return it
if (root == nullptr) {
return new Node(data);
}
// Otherwise, recur down the tree
if (data < root->data) {
2
root->left = insert(root->left, data); // Insert into the left subtree
} else {
root->right = insert(root->right, data); // Insert into the right subtree
}
return root; // Return the unchanged root
}
4 3. Traversal of Binary Search Tree
Traversal refers to visiting all the nodes in a specific order. There are several
common ways to traverse a BST:
• In-order Traversal (LNR): Visit the left subtree, the root node, and
then the right subtree. This traversal gives the nodes in sorted order.
• Pre-order Traversal (NLR): Visit the root node, then the left subtree,
then the right subtree.
• Post-order Traversal (LRN): Visit the left subtree, the right subtree,
and then the root node.
4.1 In-order Traversal Code
// In-order Traversal (left, root, right)
void inorder(Node* root) {
if (root != nullptr) {
inorder(root->left); // Visit left subtree
cout << root->data << " "; // Visit root
inorder(root->right); // Visit right subtree
}
4.2 Pre-order Traversal Code
// Pre-order Traversal (root, left, right)
void preorder(Node* root) {
if (root != nullptr) {
cout << root->data << " "; // Visit root
preorder(root->left); // Visit left subtree
preorder(root->right); // Visit right subtree
}
4.3 Post-order Traversal Code
// Post-order Traversal (left, right, root)
void postorder(Node* root) {
if (root != nullptr) {
3
postorder(root->left); // Visit left subtree
postorder(root->right); // Visit right subtree
cout << root->data << " "; // Visit root
}
5 4. Deletion in Binary Search Tree
Deletion in a BST is more complicated than insertion because there are three
possible cases:
• Leaf node: Simply delete the node.
• Node with one child: Replace the node with its single child.
• Node with two children: Find the in-order successor (the smallest
node in the right subtree) or in-order predecessor (the largest node in
the left subtree), copy its value to the node to be deleted, and recursively
delete the in-order successor/predecessor.
5.1 Deletion Algorithm
1. Find the node to be deleted.
2. If it has no children, just remove it.
3. If it has one child, replace the node with its child.
4. If it has two children, replace the node’s value with its in-order succes-
sor/predecessor and recursively delete the successor/predecessor.
5.2 Deletion Code
// Function to find the minimum value node in a given tree
Node* minValueNode(Node* node) {
Node* current = node;
while (current && current->left != nullptr) {
current = current->left; // Go to the leftmost node
}
return current;
}
// Function to delete a node from the BST
Node* deleteNode(Node* root, int key) {
// If the tree is empty
if (root == nullptr) {
return root;
}
4
// Recur down the tree to find the node to be deleted
if (key < root->data) {
root->left = deleteNode(root->left, key); // Go left if key is smaller
} else if (key > root->data) {
root->right = deleteNode(root->right, key); // Go right if key is greater
} else {
// Node with only one child or no child
if (root->left == nullptr) {
Node* temp = root->right;
delete root; // Free the node
return temp;
} else if (root->right == nullptr) {
Node* temp = root->left;
delete root;
return temp;
}
// Node with two children: Get the inorder successor
Node* temp = minValueNode(root->right);
root->data = temp->data; // Copy the inorder successor’s content to this node
root->right = deleteNode(root->right, temp->data); // Delete the inorder successor
}
return root;
}
6 5. Convert BST to Sorted Array
To convert a Binary Search Tree (BST) into a sorted array, we can simply
perform an in-order traversal. The in-order traversal of a BST visits the
nodes in ascending order, and we can store the visited nodes in an array.
6.1 Conversion Algorithm
1. Perform an in-order traversal on the BST.
2. During the traversal, store each node’s value in an array.
3. The array will be sorted in ascending order.
6.2 Conversion Code
// In-order traversal to fill the array
void inOrderTraversal(Node* root, int* arr, int* index) {
if (root != nullptr) {
5
inOrderTraversal(root->left, arr, index); // Traverse left subtree
arr[*index] = root->data; // Store node value
(*index)++;
inOrderTraversal(root->right, arr, index); // Traverse right subtree
}
}
// Function to convert BST to sorted array
int* bstToSortedArray(Node* root, int* size) {
*size = 0;
// Calculate the number of nodes in the tree
Node* temp = root;
while (temp != nullptr) {
*size = *size + 1;
if (temp->left != nullptr) {
temp = temp->left;
} else {
temp = temp->right;
}
}
// Allocate memory for the array
int* arr = new int[*size];
int index = 0;
inOrderTraversal(root, arr, &index);
return arr;
}
7 6. Example Main Function
int main() {
Node* root = nullptr;
// Insert some nodes into the BST
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
// In-order traversal
cout << "In-order traversal: ";
inorder(root); // Should print 20 30 40 50 60 70 80
6
cout << endl;
// Delete a node
root = deleteNode(root, 20); // Delete node with no children
root = deleteNode(root, 30); // Delete node with one child
root = deleteNode(root, 50); // Delete node with two children
// In-order traversal after deletion
cout << "In-order traversal after deletion: ";
inorder(root); // Should print 40 60 70 80
cout << endl;
// Convert BST to sorted array
int size = 0;
int* sortedArray = bstToSortedArray(root, &size);
cout << "Sorted Array: ";
for (int i = 0; i < size; i++) {
cout << sortedArray[i] << " ";
}
cout << endl;
delete[] sortedArray; // Free the array memory
return 0;
}