
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the node with minimum value in a Binary Search Tree in C++
In this article, we will explain how to find the node with the minimum value in a binary search tree (BST) and provide a C++ implementation. 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.
Find Minimum Value in a Binary Search Tree
Given the root node of a binary search tree, the task is to find the node with the minimum value in the tree. The minimum value node is the leftmost node in the BST. For example, consider the following binary search tree:
Scenario 1
Input: Root node: 10 10 / \ 5 15 / \ \ 3 7 20 Output: 3 Explanation: The value of leftmost node in the given BST is 3, which is the node with the minimum value.
Scenario 2
Input: Root node: 8 8 \ 12 / \ 10 14 Output: 8 Explanation: The root node itself is the leftmost node in this case, and it has the minimum value of 8.
To solve this problem, we have two approaches:
In-order Traversal to Find Minimum Value in BST
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. The steps to implement this approach are as follows:
- Define a function inorderTraversal that takes the root node as input.
- Inside the function, use recursion to perform an inorder traversal, and then return all visited nodes in a vector.
- Call the inorderTraversal function and store the result in a vector visitedNodes.
- Print the first element of the visitedNodes vector. This will be the value of the minimum node in the BST.
Example
The code below implements the above algorithm to find the node with the minimum value in C++.
#include <iostream> #include <vector> using namespace std; // Definition of the Node structure for the binary search tree struct Node { int data; Node* left; Node* right; Node(int val) : data(val), left(nullptr), right(nullptr) {} }; // Helper function to perform in-order traversal void inorderHelper(Node* root, vector<int>& visitedNodes) { if (root == nullptr) return; inorderHelper(root->left, visitedNodes); visitedNodes.push_back(root->data); inorderHelper(root->right, visitedNodes); } // Function to perform in-order traversal and return visited nodes vector<int> inorderTraversal(Node* root) { vector<int> visitedNodes; inorderHelper(root, visitedNodes); return visitedNodes; } int main() { // Create a sample binary search tree Node* 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); // Call the inorderTraversal function to get visited nodes vector<int> visitedNodes = inorderTraversal(root); cout << "Minimum value in the BST: " << visitedNodes[0] << endl; return 0; }
The output of the above code will be:
Minimum value in the BST: 3
Optimized Approach to Find Minimum Value in BST
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. Here is how you can implement this approach:
- Define a function findMinValueNode that takes the root node as input.
- Create a current pointer of type node and initialize it to the root node.
- Check if the root is null. If it is, return null (indicating the tree is empty).
- Use a while loop that runs until the current--->left is not null. Inside the loop, update current to point to its left child.
- Once the loop ends, return the current node, which will be the leftmost node and thus the node with the minimum value.
- Print the value of the node, if the tree is not empty.
Example: C++ Code
Here is the C++ code that implements the optimized approach to find the node with the minimum value in a binary search tree:
#include <iostream> using namespace std; // Definition of the Node structure for the binary search tree struct Node { int data; Node* left; Node* right; Node(int val) : data(val), left(nullptr), right(nullptr) {} }; // Function to find the node with the minimum value in the BST Node* findMinValueNode(Node* root) { if (root == nullptr) return nullptr; // If the tree is empty Node* current = root; // Traverse to the leftmost node while (current->left != nullptr) { current = current->left; } return current; } int main() { // Create a sample binary search tree Node* 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); // Call the findMinValueNode function Node* minNode = findMinValueNode(root); // Print the result if the tree is not empty if (minNode != nullptr) { cout << "Minimum value in the BST: " << minNode->data << endl; } else { cout << "The tree is empty." << endl; } return 0; }
The output of the above code will be:
Minimum value in the BST: 3
Time Complexity and Space Complexity
Here is a comparison of the time complexity and space complexity of the above-mentioned approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Brute Force | O(N) | O(N) |
Optimized Traversal | O(H) | O(1) |
Where N is the number of nodes in the BST and H is the height of the BST.