Introductions
A Binary Search Tree (BST) is a binary tree where values smaller than a node are generally placed in its left subtree, while larger values are placed in its right subtree. This ordering makes searching, insertion, and deletion more efficient than in an ordinary binary tree when the tree is reasonably balanced. These practice questions focus on practical BST operations such as insertion, searching, finding minimum and maximum values, traversal, counting nodes, and deleting nodes. Data Structure Binary Search Trees practice questions with solutions help to understand the concepts.
Question 1: Insert Values into a Binary Search Tree
Question
Create a Binary Search Tree by inserting these values in order:
50, 30, 70, 20, 40, 60, 80
Display the resulting tree structure.
Solution
The BST rule is:
Smaller value → Left
Larger value → Right
Start with 50:
50
Insert 30:
50
/
30
Insert 70:
50
/ \
30 70
Insert 20:
50
/ \
30 70
/
20
Insert 40:
50
/ \
30 70
/ \
20 40
Insert 60 and 80:
50
/ \
30 70
/ \ / \
20 40 60 80
JavaScript implementation:
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function insert(root, value) {
if (root === null) {
return new Node(value);
}
if (value < root.value) {
root.left = insert(root.left, value);
} else if (value > root.value) {
root.right = insert(root.right, value);
}
return root;
}
let root = null;
let values = [50, 30, 70, 20, 40, 60, 80];
for (let value of values) {
root = insert(root, value);
}
console.log(root);
Output
Node {
value: 50,
left: Node {
value: 30,
left: Node { value: 20, left: null, right: null },
right: Node { value: 40, left: null, right: null }
},
right: Node {
value: 70,
left: Node { value: 60, left: null, right: null },
right: Node { value: 80, left: null, right: null }
}
}
Answer
The resulting BST is:
50
/ \
30 70
/ \ / \
20 40 60 80
Question 2: Search for a Value in a Binary Search Tree
Question
Search for the value 60 in this BST:
50
/ \
30 70
/ \ / \
20 40 60 80
Return true if the value exists.
Solution
Because this is a BST, we do not need to search every node.
Start at 50.
Target:
60
Compare:
60 > 50
So move right.
At 70:
60 < 70
So move left.
We reach 60.
Therefore, the value is found.
function search(root, target) {
if (root === null) {
return false;
}
if (root.value === target) {
return true;
}
if (target < root.value) {
return search(root.left, target);
}
return search(root.right, target);
}
Complete example:
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function insert(root, value) {
if (root === null) {
return new Node(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 search(root, target) {
if (root === null) {
return false;
}
if (root.value === target) {
return true;
}
if (target < root.value) {
return search(root.left, target);
}
return search(root.right, target);
}
let root = null;
[50, 30, 70, 20, 40, 60, 80].forEach(value => {
root = insert(root, value);
});
console.log(search(root, 60));
Output
true
Answer
The value 60 exists in the BST.
The search path is:
50 → 70 → 60
Question 3: Search for a Value That Does Not Exist
Question
Search for 45 in this BST:
50
/ \
30 70
/ \ / \
20 40 60 80
Return false if the value is not found.
Solution
Start at 50.
45 < 50
Move left to 30.
45 > 30
Move right to 40.
45 > 40
Move right again.
There is no node there.
Therefore, 45 does not exist.
function search(root, target) {
if (root === null) {
return false;
}
if (root.value === target) {
return true;
}
if (target < root.value) {
return search(root.left, target);
}
return search(root.right, target);
}
Output
false
Answer
45 is not present in the BST.
The search path is:
50 → 30 → 40 → null
Question 4: Find the Minimum Value in a BST
Question
Find the smallest value in this BST:
50
/ \
30 70
/ \ / \
20 40 60 80
Solution
In a BST, smaller values are stored on the left.
Therefore, keep moving left until there is no more left child.
50 → 30 → 20
Node 20 has no left child.
So 20 is the minimum value.
function findMin(root) {
if (root === null) {
return null;
}
while (root.left !== null) {
root = root.left;
}
return root.value;
}
Output
20
Answer
The minimum value in the BST is 20.
Question 5: Find the Maximum Value in a BST
Question
Find the largest value in this BST:
50
/ \
30 70
/ \ / \
20 40 60 80
Solution
In a BST, larger values are stored on the right.
Keep moving right:
50 → 70 → 80
Node 80 has no right child.
Therefore, 80 is the maximum value.
function findMax(root) {
if (root === null) {
return null;
}
while (root.right !== null) {
root = root.right;
}
return root.value;
}
Output
80
Answer
The maximum value in the BST is 80.
Question 6: Perform Inorder Traversal of a BST
Question
Perform inorder traversal of this BST:
50
/ \
30 70
/ \ / \
20 40 60 80
Solution
Inorder traversal follows:
Left → Root → Right
For a Binary Search Tree, inorder traversal produces values in sorted order.
Visit the left subtree:
20 → 30 → 40
Visit the root:
50
Visit the right subtree:
60 → 70 → 80
Therefore:
20 → 30 → 40 → 50 → 60 → 70 → 80
JavaScript:
function inorder(root) {
if (root === null) {
return;
}
inorder(root.left);
console.log(root.value);
inorder(root.right);
}
Output
20
30
40
50
60
70
80
Answer
The inorder traversal is:
20 → 30 → 40 → 50 → 60 → 70 → 80
This is an important property of a BST.
Question 7: Count the Number of Nodes in a BST
Question
Count the total number of nodes in this BST:
50
/ \
30 70
/ \ / \
20 40 60 80
Solution
The nodes are:
20, 30, 40, 50, 60, 70, 80
There are 7 nodes.
Use recursion:
function countNodes(root) {
if (root === null) {
return 0;
}
return 1 +
countNodes(root.left) +
countNodes(root.right);
}
The function counts:
Current node
+ Left subtree
+ Right subtree
Output
7
Answer
The BST contains 7 nodes.
Question 8: Find the Height of a BST
Question
Find the height of this BST:
50
/ \
30 70
/ \ / \
20 40 60 80
Consider height as the number of edges in the longest path from the root to a leaf.
Solution
The longest paths include:
50 → 30 → 20
and:
50 → 70 → 80
Each path contains 2 edges.
Therefore:
Height = 2
Recursive solution:
function height(root) {
if (root === null) {
return -1;
}
return 1 + Math.max(
height(root.left),
height(root.right)
);
}
Output
2
Answer
The height of the BST is 2 edges.
Question 9: Delete a Leaf Node from a BST
Question
Delete node 20 from this BST:
50
/ \
30 70
/ \ / \
20 40 60 80
Display the BST after deletion.
Solution
Node 20 is a leaf node because it has no children.
To delete a leaf node, simply remove its connection from its parent.
Before deletion:
50
/ \
30 70
/ \ / \
20 40 60 80
After deletion:
50
/ \
30 70
\ / \
40 60 80
Recursive deletion code:
function deleteNode(root, value) {
if (root === null) {
return null;
}
if (value < root.value) {
root.left = deleteNode(root.left, value);
} else if (value > root.value) {
root.right = deleteNode(root.right, value);
} else {
if (root.left === null && root.right === null) {
return null;
}
}
return root;
}
Output
50
/ \
30 70
\ / \
40 60 80
Answer
Node 20 is deleted successfully.
Because it was a leaf, no replacement node was required.
Question 10: Delete a Node with Two Children
Question
Delete node 30 from this BST:
50
/ \
30 70
/ \ / \
20 40 60 80
Node 30 has two children. Rebuild the BST correctly after deletion.
Solution
Node 30 has:
Left child = 20
Right child = 40
When deleting a node with two children, we can replace it with its inorder successor.
The inorder successor is the smallest value in the node’s right subtree.
For node 30, the right subtree is:
40
Therefore:
Inorder successor = 40
Replace 30 with 40.
Before deletion:
50
/ \
30 70
/ \ / \
20 40 60 80
After replacing 30 with 40:
50
/ \
40 70
/ / \
20 60 80
JavaScript implementation:
function findMinNode(root) {
while (root.left !== null) {
root = root.left;
}
return root;
}
function deleteNode(root, value) {
if (root === null) {
return null;
}
if (value < root.value) {
root.left = deleteNode(root.left, value);
} else if (value > root.value) {
root.right = deleteNode(root.right, value);
} else {
// No child
if (root.left === null && root.right === null) {
return null;
}
// Only right child
if (root.left === null) {
return root.right;
}
// Only left child
if (root.right === null) {
return root.left;
}
// Two children
let successor = findMinNode(root.right);
root.value = successor.value;
root.right = deleteNode(
root.right,
successor.value
);
}
return root;
}
For this example, deleting 30 uses 40 as the inorder successor.
Output
50
/ \
40 70
/ / \
20 60 80
Answer
Node 30 is deleted by replacing it with its inorder successor, 40.
Key Takeaways
- A Binary Search Tree (BST) is a binary tree organized according to a value-ordering rule.
- Values smaller than a node are generally placed in its left subtree.
- Values larger than a node are generally placed in its right subtree.
- BST insertion follows the same left-smaller and right-larger rule.
- BST searching can avoid unnecessary parts of the tree.
- The minimum value is found by continuously moving to the left.
- The maximum value is found by continuously moving to the right.
- Inorder traversal of a valid BST produces values in sorted order.
- A leaf node can be deleted directly.
- Deleting a node with one child requires connecting its parent to that child.
- Deleting a node with two children commonly uses the inorder successor or inorder predecessor.
- A balanced BST can provide efficient searching, insertion, and deletion.
- In a balanced BST, these operations can have approximately O(log n) time complexity.
- In a highly unbalanced BST, these operations can become O(n).
- BSTs are an important foundation for understanding balanced trees such as AVL Trees and Red-Black Trees.
FAQs
1. What is a Binary Search Tree?
A Binary Search Tree is a binary tree organized so that smaller values are placed on the left and larger values are placed on the right according to the BST ordering rule.
2. Why is searching faster in a BST?
A BST can eliminate one subtree at every comparison when the tree is well balanced. This reduces the search space and can provide approximately O(log n) search time.
3. How do you find the minimum value in a BST?
Start at the root and keep moving to the left child until there is no left child. That node contains the minimum value.
4. How do you find the maximum value in a BST?
Start at the root and keep moving to the right child until there is no right child. That node contains the maximum value.
5. Why does inorder traversal of a BST produce sorted values?
Inorder traversal visits the left subtree, then the current node, and then the right subtree. Because of the BST ordering rule, this produces values from smallest to largest.
6. How is a leaf node deleted from a BST?
A leaf has no children, so it can simply be removed by setting the appropriate child reference of its parent to null.
7. How do you delete a BST node with two children?
A common approach is to replace the node with its inorder successor, which is the smallest value in its right subtree, and then delete that successor from its original position.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
