Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Searching for minimum and maximum values in an Javascript Binary Search Tree
In this article, we will explain how to find the minimum and maximum values in a binary search tree (BST), with implementation in JavaScript.
A binary search tree is a data structure that stores data in a sorted order such that for every node, the left subtree contains values less than the node's value, and the right subtree contains values greater than the node's value. So the leftmost node will have the minimum value, and the rightmost node will have the maximum value.
Find Minimum and Maximum in a BST
Given root node of a binary search tree, your task is to find the node with the minimum value (leftmost node) and the maximum value (rightmost node). Consider the following binary search tree:
Scenario 1
Input:
Root node: 10
10
/ \
5 15
/ \ \
3 7 20
Output:
Min: 3
Max: 20
Explanation: The value of leftmost node in the given BST is 3, and the value of rightmost node is 20.
Scenario 2
Input:
Root node: 8
8
\
12
/ \
10 14
Output:
Min: 8
Max: 14
Explanation: The root node itself is the leftmost node in this case, and it has value 8. The rightmost node is 14, which is the maximum value in the BST.
To solve this problem, we have two approaches:
- Brute Force Approach: Using Inorder Traversal
- Optimized Approach: Traversing Only Left/Right Subtree
Using Inorder Traversal (Brute Force)
The inorder traversal of a binary search tree visits nodes in ascending order. Therefore, the first node visited during an in-order traversal will be the node with the minimum value and the last node visited will be the node with the maximum value.
Algorithm Steps
- Perform inorder traversal to collect all nodes in ascending order
- The first element is the minimum value
- The last element is the maximum value
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function inorderTraversal(root, visitedNodes) {
if (root !== null) {
inorderTraversal(root.left, visitedNodes);
visitedNodes.push(root.value);
inorderTraversal(root.right, visitedNodes);
}
}
// Create a sample binary search tree
let root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.right = new Node(7);
root.right.right = new Node(20);
// Find min and max using inorder traversal
let visitedNodes = [];
inorderTraversal(root, visitedNodes);
console.log("Visited nodes:", visitedNodes);
console.log("Minimum value:", visitedNodes[0]);
console.log("Maximum value:", visitedNodes[visitedNodes.length - 1]);
Visited nodes: [3, 5, 7, 10, 15, 20] Minimum value: 3 Maximum value: 20
Using Left/Right Traversal (Optimized)
The optimal way to find the minimum value in a binary search tree is to traverse only the left subtree. Since the left child of a node always contains a value less than its parent, the leftmost node will have the minimum value. Similarly, we can find the maximum value by traversing the right subtree.
Algorithm Steps
- For minimum: Keep going left until you reach a node with no left child
- For maximum: Keep going right until you reach a node with no right child
- Return null if the tree is empty
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function findMinNode(root) {
if (root === null) {
return null;
}
let current = root;
while (current.left !== null) {
current = current.left;
}
return current;
}
function findMaxNode(root) {
if (root === null) {
return null;
}
let current = root;
while (current.right !== null) {
current = current.right;
}
return current;
}
// Create a sample binary search tree
let root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.right = new Node(7);
root.right.right = new Node(20);
// Find minimum and maximum nodes
let minNode = findMinNode(root);
let maxNode = findMaxNode(root);
if (minNode !== null) {
console.log("Minimum value in BST:", minNode.value);
} else {
console.log("Tree is empty");
}
if (maxNode !== null) {
console.log("Maximum value in BST:", maxNode.value);
} else {
console.log("Tree is empty");
}
Minimum value in BST: 3 Maximum value in BST: 20
Comparison
| Approach | Time Complexity | Space Complexity | Use Case |
|---|---|---|---|
| Inorder Traversal | O(N) | O(N) | When you need all sorted values |
| Direct Traversal | O(H) | O(1) | When you only need min/max |
Where N is the number of nodes in the BST and H is the height of the BST.
Conclusion
The optimized approach using direct left/right traversal is preferred for finding minimum and maximum values in a BST due to its O(H) time complexity and O(1) space complexity. Use inorder traversal only when you need the complete sorted sequence.
