📖 Scenario: You are building 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 if a contact with a specific phone number exists in your phone book.
🎯 Goal: Build a program that creates a BST with given phone numbers, sets a target phone number to search for, implements the search operation in the BST, and prints whether the phone number is found or not.
📋 What You'll Learn
Create a BST node structure with integer phone number values
Insert given phone numbers into the BST
Create a variable for the target phone number to search
Implement a function to search the BST for the target phone number
Print the search result as 'Found' or 'Not Found'
💡 Why This Matters
🌍 Real World
Phone books, contact lists, and databases often use BSTs to store and quickly search for unique keys like phone numbers.
💼 Career
Understanding BST search is fundamental for software engineers working with data structures, databases, and performance optimization.
Progress0 / 4 steps
1
Create BST Node Structure and Insert Phone Numbers
Create a struct called Node with an int field phone and two pointers left and right of type *Node. Then create a function insert that takes a *Node and an int phone number and returns the root *Node after inserting the phone number in the BST. Insert these phone numbers in order: 5551234, 5555678, 5550000, 5559999, 5554321. Store the root node in a variable called root.
DSA Go
Hint
Define a struct with fields for phone number and left/right child pointers. Use recursion to insert nodes in BST order.
2
Set Target Phone Number to Search
Create an int variable called target and set it to 5554321.
DSA Go
Hint
Use target := 5554321 to create and set the variable.
3
Implement BST Search Function
Write a function called search that takes a *Node called root and an int called target. It returns bool. The function should return true if target is found in the BST rooted at root, otherwise false. Use the BST property to decide whether to search left or right subtree or return true if found.
DSA Go
Hint
Use recursion and compare target with current node's phone number to decide the search path.
4
Print Search Result
Use fmt.Println to print "Found" if search(root, target) returns true, otherwise print "Not Found".
DSA Go
Hint
Use an if-else statement to print the correct message based on the search result.
Practice
(1/5)
1. What does the search operation in a Binary Search Tree (BST) do?
easy
A. Finds whether a given value exists in the BST
B. Deletes a node from the BST
C. Inserts a new node into the BST
D. Traverses all nodes in the BST
Solution
Step 1: Understand the purpose of BST search
The search operation is designed to check if a specific value is present in the BST.
Step 2: Differentiate from other operations
Insertion adds nodes, deletion removes nodes, and traversal visits nodes. Search only checks for existence.
Final Answer:
Finds whether a given value exists in the BST -> Option A
Quick Check:
Search = Find value [OK]
Hint: Search checks presence, not insertion or deletion [OK]
Common Mistakes:
Confusing search with insert or delete
Thinking search returns the node always
Assuming search traverses all nodes
2. Which of the following is the correct Go function signature for a BST search method that returns a boolean indicating if a value exists?
easy
A. func Search(val int) *Node
B. func (root *Node) Search(val int) bool
C. func (root Node) Search(val string) bool
D. func Search(root Node, val int) int
Solution
Step 1: Identify method receiver and return type
BST search is usually a method on a pointer to Node and returns bool for found or not.
Step 2: Check parameter types and return type
func (root *Node) Search(val int) bool uses pointer receiver, int parameter, and bool return which matches the expected signature.
Final Answer:
func (root *Node) Search(val int) bool -> Option B
Hint: Search returns bool, uses pointer receiver for tree nodes [OK]
Common Mistakes:
Using value receiver instead of pointer
Wrong parameter or return types
Confusing method vs function signatures
3. Given the BST below and the Go code snippet, what will be the output?
BST structure:
```
10
/ \
5 15
/
3
```
Go code:
found := root.Search(3)
fmt.Println(found)
medium
A. true
B. false
C. nil
D. runtime error
Solution
Step 1: Trace the search for value 3 in the BST
Starting at root 10, 3 < 10, go left to 5; 3 < 5, go left to 3; found node with value 3.
Step 2: Understand the returned value and print output
Search returns true because 3 exists; printing true outputs 'true'.
Final Answer:
true -> Option A
Quick Check:
3 found in BST = true [OK]
Hint: Follow BST left/right rules to find value [OK]
Common Mistakes:
Assuming search returns node instead of bool
Confusing true/false output
Ignoring BST structure when tracing
4. The following Go code for BST search has a bug. What is the bug?
func (root *Node) Search(val int) bool {
if root == nil {
return false
}
if root.Value == val {
return true
}
if val < root.Value {
return root.Search(val)
} else {
return root.Search(val)
}
}
medium
A. Base case for nil root is missing
B. Uses value receiver instead of pointer
C. Returns int instead of bool
D. Recursive calls do not move to left or right child nodes
Solution
Step 1: Analyze recursive calls in search method
Both recursive calls use root.Search(val) without moving to root.Left or root.Right, causing infinite recursion.
Step 2: Identify correct recursive calls
Should call root.Left.Search(val) if val < root.Value, else root.Right.Search(val) to move down the tree.
Final Answer:
Recursive calls do not move to left or right child nodes -> Option D
Quick Check:
Recursive calls must move down tree [OK]
Hint: Recursive calls must go to left or right child [OK]
Common Mistakes:
Calling search on same node causing infinite loop
Missing base case for nil
Wrong return types
5. You want to search for a value in a BST but also count how many nodes you visited during the search. Which modification to the search function is best?
hard
A. Use a global variable to count nodes visited during search
B. Return the node instead of bool and count nodes outside the function
C. Add a counter parameter passed by pointer and increment it on each node visited
D. Modify the BST to store counts in each node
Solution
Step 1: Understand the need to count nodes during search
Counting nodes visited requires tracking each node checked during recursion.
Step 2: Choose method to count nodes visited
Passing a pointer to a counter variable and incrementing it at each node visit allows accurate counting without side effects.
Final Answer:
Add a counter parameter passed by pointer and increment it on each node visited -> Option C
Quick Check:
Pass counter pointer to track visits [OK]
Hint: Pass counter pointer to track nodes visited [OK]
Common Mistakes:
Using global variables causing side effects
Counting outside search missing recursive visits
Changing BST structure unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.