📖 Scenario: You are working with a Binary Search Tree (BST) that stores numbers. You want to find the smallest number in this tree. This is useful when you want to know the minimum value quickly, like finding the earliest date or lowest price.
🎯 Goal: Build a simple BST with some numbers, then write code to find and print the minimum element in the BST.
📋 What You'll Learn
Create a BST node structure with integer data and left/right pointers
Manually create a BST with these exact values: 15, 10, 20, 8, 12, 17, 25
Write a function called findMin that returns the minimum value in the BST
Print the minimum value found by findMin
💡 Why This Matters
🌍 Real World
Finding minimum values quickly is useful in many applications like scheduling, price comparisons, and searching sorted data.
💼 Career
Understanding BSTs and how to find minimum or maximum elements is a common task in software engineering interviews and real-world coding.
Progress0 / 4 steps
1
Create BST Node Structure and Build Tree
Create a struct called Node with an int data, and two pointers Node* left and Node* right. Then create the BST root node with value 15. Manually add nodes with values 10, 20, 8, 12, 17, and 25 to form the BST.
DSA C++
Hint
Remember, in a BST, left child nodes have smaller values and right child nodes have larger values than the parent.
2
Declare the findMin Function
Declare a function called findMin that takes a Node* parameter named root and returns an int. This function will find the minimum value in the BST.
DSA C++
Hint
Just write the function header for now. The function will return an integer and take a pointer to Node.
3
Implement the findMin Function
Implement the findMin function to find the minimum value in the BST. Use a loop to go left until root->left is nullptr. Return the data of the leftmost node.
DSA C++
Hint
Keep moving to the left child until there is no left child. Then return that node's data.
4
Print the Minimum Element
Write a std::cout statement to print the minimum element found by calling findMin(root). Print exactly: Minimum element: X where X is the minimum value.
DSA C++
Hint
Call findMin(root) and print the result with std::cout. Use the exact format: Minimum element: X.
Practice
(1/5)
1. In a Binary Search Tree (BST), where is the minimum element always located?
easy
A. At any leaf node
B. At the leftmost node
C. At the root node
D. At the rightmost node
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values than their parents.
Step 2: Locate minimum element
The smallest value is found by going left until no more left child exists.
Final Answer:
At the leftmost node -> Option B
Quick Check:
Minimum = leftmost node [OK]
Hint: Minimum is always the leftmost node in BST [OK]
Common Mistakes:
Thinking minimum is at root
Confusing leftmost with rightmost
Assuming minimum can be any leaf
2. Which of the following C++ code snippets correctly finds the minimum element in a BST node pointer root?
easy
A. while(root->left != nullptr) root = root->left; return root->data;
B. while(root->right != nullptr) root = root->right; return root->data;
C. if(root == nullptr) return INT_MAX; return root->data;
D. return root->data;
Solution
Step 1: Check traversal direction
Minimum is found by moving left until no left child exists.
Step 2: Verify code correctness
while(root->left != nullptr) root = root->left; return root->data; loops left correctly and returns the leftmost node's data.