📖 Scenario: You are working with a simple phone book application that stores contacts in a Binary Search Tree (BST). Each contact has a unique phone number. You want to find the contact with the smallest phone number to quickly access the earliest saved contact.
🎯 Goal: Build a BST with given phone numbers and write code to find the minimum phone number stored in the BST.
📋 What You'll Learn
Create a BST node class with value, left, and right attributes
Insert given phone numbers into the BST in the given order
Write a function to find the minimum value in the BST
Print the minimum phone number found
💡 Why This Matters
🌍 Real World
BSTs are used in databases and file systems to quickly find minimum or maximum values, like earliest or latest records.
💼 Career
Understanding BST operations is important for software engineers working on efficient data storage, search algorithms, and system design.
Progress0 / 4 steps
1
Create BST Node Class and Insert Nodes
Create a class called Node with an __init__ method that takes value and sets left and right to None. Then create a function called insert that takes root and value and inserts the value into the BST correctly. Finally, create a BST by inserting these phone numbers in order: 50, 30, 70, 20, 40, 60, 80. Store the root node in a variable called root.
DSA Python
Hint
Remember, in a BST, smaller values go to the left child and larger or equal values go to the right child.
2
Create Function to Find Minimum Value
Create a function called find_minimum that takes root as input and returns the minimum value in the BST by moving to the left child until it is None.
DSA Python
Hint
Keep moving left from the root until you find a node with no left child. That node has the smallest value.
3
Call Function to Find Minimum Value
Call the function find_minimum with the variable root and store the result in a variable called min_value.
DSA Python
Hint
Just call the function with root and save the result in min_value.
4
Print the Minimum Value
Print the variable min_value to display the smallest phone number in the BST.
DSA Python
Hint
Use print(min_value) to show the smallest phone number.
Practice
(1/5)
1. In a Binary Search Tree (BST), where is the minimum element always located?
easy
A. At the leftmost node
B. At the rightmost node
C. At the root node
D. At any leaf node
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values than their parents, so the smallest value is always towards the left.
Step 2: Locate minimum element
By moving left from the root repeatedly until no more left child exists, we reach the minimum element.
Final Answer:
At the leftmost node -> Option A
Quick Check:
Minimum element = leftmost node [OK]
Hint: Minimum is always the leftmost node in BST [OK]
Common Mistakes:
Confusing minimum with root node
Thinking minimum is at rightmost node
Assuming minimum can be any leaf
2. Which of the following Python code snippets correctly finds the minimum element in a BST node named root?
easy
A. while root.right is not None:
root = root.right
print(root.val)
B. while root.left is not None:
root = root.left
print(root.val)
C. if root.left is None:
print(root.val)
D. print(min(root.val, root.left.val))
Solution
Step 1: Check loop for minimum search
while root.left is not None:
root = root.left
print(root.val) correctly moves left while left child exists, which is how to find minimum in BST.
Step 2: Verify other options
while root.right is not None:
root = root.right
print(root.val) moves right (wrong for minimum), C only checks once, D uses min on values without traversal.
Final Answer:
while root.left is not None:
root = root.left
print(root.val) -> Option B
Quick Check:
Traverse left until None = while root.left is not None:
root = root.left
print(root.val) [OK]
Hint: Traverse left child until None to find minimum [OK]
Common Mistakes:
Traversing right child instead of left
Not using a loop to reach leftmost node
Using min() function incorrectly on nodes
3. What will be the output of the following code snippet?