📖 Scenario: You are working with a phone contact list stored as a Binary Search Tree (BST). Each contact has a unique phone number as the key. You want to find the next contact in the sorted order after a given contact.
🎯 Goal: Build a program to find the inorder successor of a given node in a BST. The inorder successor is the node with the smallest key greater than the given node's key.
📋 What You'll Learn
Create a BST with exact nodes: 20, 9, 25, 5, 12, 11, 14
Create a variable called target pointing to the node with key 9
Implement a method inorderSuccessor that finds the inorder successor of target
Print the key of the inorder successor node
💡 Why This Matters
🌍 Real World
Finding the next contact in a sorted phonebook or database stored as a BST helps in quick navigation and search.
💼 Career
Understanding BST traversal and successor/predecessor logic is important for software roles involving data structures, databases, and system design.
Progress0 / 4 steps
1
Create the BST nodes
Create a class Node with integer key, and left and right child nodes. Then create the BST with nodes having keys 20, 9, 25, 5, 12, 11, and 14 arranged as a BST.
DSA Java
Hint
Start by defining the Node class with key, left, and right. Then create the root node with key 20 and add children to build the BST.
2
Set the target node
Create a variable called target that points to the node with key 9 in the BST.
DSA Java
Hint
Assign target to root.left because that node has key 9.
3
Implement inorderSuccessor method
Implement a static method inorderSuccessor that takes Node root and Node target and returns the inorder successor node. Use the BST property and handle the case when target has a right subtree or not.
DSA Java
Hint
Check if target has a right child. If yes, the successor is the leftmost node in the right subtree. Otherwise, traverse from root to find the lowest ancestor whose left child is also an ancestor of target.
4
Print the inorder successor key
Call inorderSuccessor with root and target and print the key of the returned node.
DSA Java
Hint
Call inorderSuccessor(root, target) and print the key of the returned node. If no successor exists, print a message.
Practice
(1/5)
1. What is the inorder successor of a node in a Binary Search Tree (BST)?
easy
A. The node with the smallest key greater than the given node's key
B. The node with the largest key smaller than the given node's key
C. The root node of the BST
D. The left child of the given node
Solution
Step 1: Understand inorder traversal order
Inorder traversal visits nodes in ascending order of their keys in a BST.
Step 2: Define inorder successor
The inorder successor of a node is the next node visited after it in inorder traversal, which is the smallest node greater than it.
Final Answer:
The node with the smallest key greater than the given node's key -> Option A
Quick Check:
Inorder successor = smallest greater key [OK]
Hint: Inorder successor is next bigger node in sorted order [OK]
Common Mistakes:
Confusing successor with predecessor
Thinking successor is always right child
Assuming successor is parent node
2. Which of the following Java method signatures correctly represents a function to find the inorder successor in a BST node class?
easy
A. public void inorderSuccessor(Node p)
B. public Node inorderSuccessor(Node root, Node p)
C. public int inorderSuccessor(Node root)
D. public Node inorderSuccessor(int key)
Solution
Step 1: Identify required inputs and outputs
To find inorder successor, we need the BST root and the target node; output is the successor node.
Step 2: Match method signature
public Node inorderSuccessor(Node root, Node p) takes root and node, returns Node, matching requirements.
Final Answer:
public Node inorderSuccessor(Node root, Node p) -> Option B
Quick Check:
Input root and node, output node [OK]
Hint: Method needs root and target node, returns successor node [OK]
Common Mistakes:
Missing root parameter
Returning int instead of Node
Using void return type
3. Given the BST below and the node with value 15, what is the inorder successor?
20
/ \
10 30
\
15
medium
A. 20
B. 15
C. 30
D. 10
Solution
Step 1: Locate node 15 and check right subtree
Node 15 has no right child, so successor is ancestor with greater value.
Step 2: Find lowest ancestor greater than 15
Ancestor 20 is the next bigger node after 15.
Final Answer:
20 -> Option A
Quick Check:
Next bigger after 15 is 20 [OK]
Hint: If no right child, successor is nearest bigger ancestor [OK]
Common Mistakes:
Choosing 30 which is too big
Choosing 10 which is smaller
Choosing the node itself
4. Consider this buggy Java code snippet to find the inorder successor of a node in BST: