📖 Scenario: You are working with a Binary Search Tree (BST) that stores unique integer values. You want to find the inorder predecessor of a given node value. The inorder predecessor of a node is the node with the largest value smaller than the given node's value.This is useful in scenarios like navigating a sorted list of items, or undoing steps in an ordered sequence.
🎯 Goal: Build a Go program that creates a BST, sets a target node value, finds the inorder predecessor of that node, and prints the predecessor's value or a message if none exists.
📋 What You'll Learn
Create a BST with the exact nodes: 20, 10, 30, 5, 15, 25, 35
Set a target node value to 20
Implement a function to find the inorder predecessor of the target node in the BST
Print the predecessor's value or 'No predecessor' if it does not exist
💡 Why This Matters
🌍 Real World
Finding inorder predecessors is useful in databases and file systems where ordered data navigation is needed.
💼 Career
Understanding BST operations like inorder predecessor is important for software engineering roles involving data structures and algorithms.
Progress0 / 4 steps
1
Create the BST structure and insert nodes
Define a struct called Node with value int, left *Node, and right *Node. Then create a function insert(root *Node, val int) *Node that inserts a value into the BST. Finally, create a BST by inserting these values in order: 20, 10, 30, 5, 15, 25, 35. Store the root in a variable called root.
DSA Go
Hint
Use recursion in insert to place smaller values to the left and larger to the right.
2
Set the target node value
Create an integer variable called target and set it to 20.
DSA Go
Hint
Just create a variable target and assign 20.
3
Implement function to find inorder predecessor
Write a function inorderPredecessor(root *Node, target int) *Node that finds the inorder predecessor of the node with value target in the BST rooted at root. Use the BST property and traversal logic to find the predecessor. The function should return a pointer to the predecessor node or nil if none exists. Call this function in main and store the result in a variable called pred.
DSA Go
Hint
Use a loop to traverse the tree. Move left if target is less or equal to current node's value, else update predecessor and move right.
4
Print the inorder predecessor result
In main, print the value of pred if it is not nil. Otherwise, print No predecessor. Use fmt.Println.
DSA Go
Hint
Check if pred is nil. If not, print pred.value. Otherwise, print No predecessor.
Practice
(1/5)
1. What is the inorder predecessor of a node in a Binary Search Tree (BST)?
easy
A. The node with the smallest value larger than the given node
B. The node with the largest value smaller than the given node
C. The left child of the given node
D. The right child of the given node
Solution
Step 1: Understand inorder traversal in BST
Inorder traversal visits nodes in ascending order of their values.
Step 2: Define inorder predecessor
The inorder predecessor of a node is the node that comes just before it in this ascending order, meaning it has the largest value smaller than the node.
Final Answer:
The node with the largest value smaller than the given node -> Option B
Quick Check:
Inorder predecessor = largest smaller value [OK]
Hint: Predecessor is largest smaller value in BST [OK]
Common Mistakes:
Confusing predecessor with successor
Thinking predecessor is always left child
Assuming predecessor is the parent node
2. Which of the following Go function signatures correctly represents a function to find the inorder predecessor in a BST node struct named Node?
easy
A. func inorderPredecessor(root *Node, key int) *Node
B. func inorderPredecessor(key int) Node
C. func inorderPredecessor(root Node, key int) Node
D. func inorderPredecessor(root *Node) int
Solution
Step 1: Analyze function parameters and return type
The function needs the BST root pointer and the key to find predecessor for, and returns a pointer to the predecessor node.
Step 2: Match with options
func inorderPredecessor(root *Node, key int) *Node matches this: it takes *Node root, int key, and returns *Node.
Final Answer:
func inorderPredecessor(root *Node, key int) *Node -> Option A
Quick Check:
Pointer root and key input, pointer node output [OK]
Hint: Use pointer receiver and return pointer for BST nodes [OK]
Common Mistakes:
Passing root by value instead of pointer
Returning node value instead of node pointer
Missing key parameter to identify target node
3. Given the following BST and code snippet, what will be printed?
type Node struct {
Val int
Left, Right *Node
}
func inorderPredecessor(root *Node, key int) *Node {
var predecessor *Node
curr := root
for curr != nil {
if key <= curr.Val {
curr = curr.Left
} else {
predecessor = curr
curr = curr.Right
}
}
return predecessor
}
func main() {
root := &Node{Val: 20}
root.Left = &Node{Val: 10}
root.Right = &Node{Val: 30}
root.Left.Left = &Node{Val: 5}
root.Left.Right = &Node{Val: 15}
pred := inorderPredecessor(root, 15)
if pred != nil {
fmt.Println(pred.Val)
} else {
fmt.Println("No predecessor")
}
}
medium
A. No predecessor
B. 5
C. 15
D. 10
Solution
Step 1: Trace the inorderPredecessor function for key=15
Start at root (20). Since 15 <= 20, move left to 10. Since 15 > 10, set predecessor=10, move right to 15. Since 15 <= 15, move left (nil). Loop ends.
Step 2: Return predecessor node value
Predecessor is node with value 10, so print 10.
Final Answer:
10 -> Option D
Quick Check:
Largest smaller than 15 is 10 [OK]
Hint: Move left if key ≤ node, else update predecessor and move right [OK]
Common Mistakes:
Returning the key itself as predecessor
Confusing predecessor with successor
Not updating predecessor correctly when moving right
4. The following Go function is intended to find the inorder predecessor of a node with value key in a BST. What is the bug?
A. The function should return curr instead of predecessor
B. The predecessor variable should be reset inside the loop
C. The condition should be key <= curr.Val to handle equal keys correctly
D. The loop should use recursion instead of iteration
Solution
Step 1: Analyze the condition in the loop
The current condition is key < curr.Val, which skips updating predecessor when key equals curr.Val.
Step 2: Correct the condition to include equality
Changing to key <= curr.Val ensures that when key equals curr.Val, we move left without updating predecessor, which is correct for inorder predecessor logic.
Final Answer:
The condition should be key <= curr.Val to handle equal keys correctly -> Option C
Quick Check:
Use <= to handle equal keys properly [OK]
Hint: Use <= in condition to handle equal keys correctly [OK]
Common Mistakes:
Using < instead of <= causes wrong predecessor for equal keys
Resetting predecessor inside loop loses previous valid value
Returning curr instead of predecessor returns wrong node
5. You have a BST where each node has a pointer to its parent. How would you find the inorder predecessor of a given node n efficiently without starting from the root? Choose the best approach.
hard
A. If n has a left child, find the rightmost node in its left subtree; else, move up using parent pointers until you find a node which is the right child of its parent
B. Always start from the root and do a full inorder traversal to find predecessor
C. If n has a right child, find the leftmost node in its right subtree; else, move up using parent pointers until you find a node which is the left child of its parent
D. Return the parent node directly as the predecessor
Solution
Step 1: Check if node has left subtree
If n has a left child, the predecessor is the rightmost node in that left subtree.
Step 2: If no left child, move up using parent pointers
Move up until you find a node that is the right child of its parent; that parent is the predecessor.
Final Answer:
If n has left child, find rightmost in left subtree; else move up until node is right child -> Option A
Quick Check:
Use left subtree or climb parents to find predecessor [OK]
Hint: Use left subtree or climb parents via right child link [OK]
Common Mistakes:
Starting from root wastes time
Confusing predecessor with successor logic
Returning parent directly without checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.