📖 Scenario: You are working with a Binary Search Tree (BST) that stores numbers. You want to find the inorder predecessor of a given node. The inorder predecessor is the node with the next smaller value in the BST.Think of the BST like a family tree sorted by age, and you want to find the person who is just older than a given person.
🎯 Goal: Build a Java program that creates a BST, sets a target node value, finds the inorder predecessor of that node, and prints the predecessor's value.
📋 What You'll Learn
Create a BST with exact nodes: 20, 10, 30, 5, 15, 25, 35
Set an integer variable target to 15
Implement a method inorderPredecessor to find the inorder predecessor of target
Print the value of the inorder predecessor or "No predecessor" if none exists
💡 Why This Matters
🌍 Real World
Finding the inorder predecessor is useful in database indexing and navigation systems where you need to find the previous item in sorted order.
💼 Career
Understanding BST operations like inorder predecessor is important for software engineers working with search algorithms, data retrieval, and optimization.
Progress0 / 4 steps
1
Create the BST nodes and insert them
Create a class Node with integer data, and Node references left and right. Then create a class BST with a Node root. Insert the nodes with values 20, 10, 30, 5, 15, 25, and 35 into the BST using the insert method.
DSA Java
Hint
Start by defining the Node class with data, left, and right. Then create the BST class with an insert method to add nodes in the correct position.
2
Set the target node value
Inside the main method, create an integer variable called target and set it to 15.
DSA Java
Hint
Inside the main method, write int target = 15; to set the target value.
3
Implement the inorder predecessor method
Add a method Node inorderPredecessor(Node root, int target) in the BST class. This method should find and return the inorder predecessor node of the node with value target. Use the BST properties to find the predecessor efficiently.
DSA Java
Hint
Use a loop starting from root. Move left if target is less or equal to current node's data. Otherwise, update predecessor to current and move right. Return predecessor at the end.
4
Print the inorder predecessor value
In the main method, call inorderPredecessor with tree.root and target. Store the result in a Node variable called pred. Then print pred.data if pred is not null, otherwise print "No predecessor".
DSA Java
Hint
Call inorderPredecessor with tree.root and target. Store the result in pred. If pred is not null, print pred.data. 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 largest value smaller than the given node
B. The node with the smallest value larger 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 order
Inorder traversal visits nodes in ascending order for BST, so predecessor is the previous node in this order.
Step 2: Define inorder predecessor
The inorder predecessor is the node with the largest value smaller than the current node's value.
Final Answer:
The node with the largest value smaller than the given node -> Option A
Quick Check:
Inorder predecessor = largest smaller value [OK]
Hint: Predecessor is just before current node in sorted order [OK]
Common Mistakes:
Confusing predecessor with successor
Thinking predecessor is always left child
Assuming predecessor is the parent node
2. Which of the following Java method signatures correctly represents a function to find the inorder predecessor in a BST node?
easy
A. public Node findInorderSuccessor(Node root, int key)
B. public int findInorderPredecessor(int[] arr, int key)
C. public Node findInorderPredecessor(Node root, int key)
D. public void findInorderPredecessor(Node root)
Solution
Step 1: Identify correct parameters for BST predecessor method
We need the BST root and the key node value to find predecessor, so method should accept Node root and int key.
Step 2: Check method return type and name
Return type should be Node (the predecessor node), and method name should indicate predecessor, so 'findInorderPredecessor(Node root, int key)' fits.
Final Answer:
public Node findInorderPredecessor(Node root, int key) -> Option C
Quick Check:
Correct signature includes root, key, returns Node [OK]
Hint: Method needs root and key, returns Node predecessor [OK]
Common Mistakes:
Using array instead of BST Node
Confusing predecessor with successor method name
Wrong return type like void or int
3. Given the BST below and the code snippet, what will be the output?
Assuming findInorderPredecessor is correctly implemented.
medium
A. null
B. 10
C. 30
D. 20
Solution
Step 1: Identify inorder predecessor of node 20 in BST
Inorder predecessor is the largest node smaller than 20. Left subtree of 20 has node 10, which is smaller and largest in left subtree.
Step 2: Confirm output of print statement
Predecessor node data is 10, so printed output is "10".
Final Answer:
10 -> Option B
Quick Check:
Largest smaller node than 20 is 10 [OK]
Hint: Predecessor is max in left subtree if left exists [OK]
Common Mistakes:
Printing null when left subtree exists
Confusing successor with predecessor
Printing the node itself instead of predecessor
4. Consider this buggy Java method to find the inorder predecessor in a BST:
public Node findInorderPredecessor(Node root, int key) {
Node predecessor = null;
while (root != null) {
if (key < root.data) {
root = root.left;
} else {
predecessor = root;
root = root.right;
}
}
return predecessor;
}
What is the main issue with this code?
medium
A. It does not handle the case when the node has a left subtree
B. It incorrectly updates predecessor when key equals root.data
C. It causes infinite loop for some inputs
D. It returns the successor instead of predecessor
Solution
Step 1: Analyze traversal logic for predecessor
The code moves left if key < root.data, else moves right and updates predecessor. But it never checks left subtree of the node itself.
Step 2: Identify missing left subtree handling
If the node has a left subtree, the predecessor is the maximum node in that left subtree, which this code does not check.
Final Answer:
It does not handle the case when the node has a left subtree -> Option A
Quick Check:
Missing left subtree max check causes wrong predecessor [OK]
Hint: Check left subtree max for predecessor if it exists [OK]
Common Mistakes:
Ignoring left subtree max node
Confusing predecessor with successor logic
Assuming predecessor is always ancestor
5. You have a BST and want to find the inorder predecessor of a node with value 15. The node has no left child. Which approach correctly finds the predecessor?
hard
A. Find the maximum node in the left subtree of 15
B. Return the right child of node 15
C. Return null because no left child exists
D. Traverse from root, keep track of last node with value less than 15
Solution
Step 1: Understand predecessor when no left child
If node has no left child, predecessor is one of its ancestors with value less than node's value.
Step 2: Use traversal from root to track predecessor
Traverse from root to node 15, keep updating predecessor to last node with value less than 15.
Final Answer:
Traverse from root, keep track of last node with value less than 15 -> Option D
Quick Check:
No left child means ancestor with smaller value is predecessor [OK]
Hint: Track last smaller ancestor when no left child [OK]
Common Mistakes:
Returning null immediately if no left child
Trying to find max in non-existent left subtree
Returning right child mistakenly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.