Discover how to find the 'just before' value in a tree without searching everywhere!
Why BST Inorder Predecessor in DSA C#?
Imagine you have a family tree drawn on paper, and you want to find the person who comes just before someone in the family order. Without a clear method, you have to scan the whole tree again and again, which is tiring and confusing.
Manually searching for the previous person means checking many family members one by one. This takes a lot of time and you can easily make mistakes by missing someone or going in the wrong direction.
The BST Inorder Predecessor method gives you a clear path to find the person just before another in the family order. It uses the tree's structure to jump directly to the right spot, saving time and avoiding errors.
int FindPredecessor(Node root, int key) {
List<int> inorder = new List<int>();
InorderTraversal(root, inorder);
for (int i = 1; i < inorder.Count; i++) {
if (inorder[i] == key) return inorder[i - 1];
}
return -1;
}Node FindPredecessor(Node root, int key) {
Node current = root;
Node predecessor = null;
while (current != null) {
if (key > current.Value) {
predecessor = current;
current = current.Right;
} else {
current = current.Left;
}
}
return predecessor;
}This method lets you quickly find the closest smaller value in a sorted tree, making searches and updates much faster and easier.
Think of a library catalog sorted by book IDs. If you want the book just before a certain ID, the BST Inorder Predecessor helps you find it instantly without flipping through every book.
Manual searching is slow and error-prone.
BST Inorder Predecessor uses tree structure for fast lookup.
It helps find the closest smaller value efficiently.
Practice
Solution
Step 1: Understand inorder traversal order
Inorder traversal visits nodes in ascending order for BST.Step 2: Define predecessor in inorder traversal
The predecessor is the node visited just before the current node, so it must have the largest value smaller than the current node.Final Answer:
The node with the largest value smaller than the given node -> Option AQuick Check:
Inorder predecessor = largest smaller value [OK]
- Confusing predecessor with successor
- Thinking predecessor is always left child
- Assuming predecessor is the parent node
Solution
Step 1: Check where predecessor lies if left subtree exists
Predecessor is the rightmost node in the left subtree.Step 2: Analyze code snippets
Node predecessor = node.Left; while (predecessor.Right != null) predecessor = predecessor.Right; starts at left child and moves right until no more right child, correctly finding predecessor.Final Answer:
Node predecessor = node.Left; while (predecessor.Right != null) predecessor = predecessor.Right; -> Option DQuick Check:
Left subtree's rightmost node = predecessor [OK]
- Going left repeatedly instead of right in left subtree
- Starting from right child instead of left
- Ignoring the while loop to find rightmost node
20
/ \
10 30
\
15
Solution
Step 1: Locate node 15 and check left subtree
Node 15 has no left child, so predecessor is ancestor smaller than 15.Step 2: Find nearest ancestor smaller than 15
Ancestor 10 is smaller than 15 and is the closest on the path.Final Answer:
10 -> Option BQuick Check:
Predecessor of 15 is 10 [OK]
- Choosing 20 which is larger than 15
- Choosing 15 itself
- Choosing 30 which is right subtree
Node FindPredecessor(Node root, Node node) {
if (node.Left != null) {
Node temp = node.Left;
while (temp.Left != null) {
temp = temp.Left;
}
return temp;
}
Node predecessor = null;
Node current = root;
while (current != null) {
if (node.Value > current.Value) {
predecessor = current;
current = current.Right;
} else {
current = current.Left;
}
}
return predecessor;
}Solution
Step 1: Analyze left subtree traversal
Predecessor in left subtree is rightmost node, so loop should move right, not left.Step 2: Identify incorrect loop condition
Current code loops while temp.Left != null, should be temp.Right != null to find rightmost node.Final Answer:
The while loop should check temp.Right, not temp.Left -> Option AQuick Check:
Rightmost node in left subtree = predecessor [OK]
- Checking left child repeatedly instead of right
- Ignoring null root case (not critical here)
- Confusing predecessor update logic
40
/ \
20 60
/ \ / \
10 30 50 70
/
25
Which node is the inorder predecessor of 25?Solution
Step 1: Check if node 25 has left subtree
Node 25 has no left child, so predecessor is ancestor smaller than 25.Step 2: Find nearest ancestor smaller than 25
Ancestor 20 is smaller than 25 and closest on path from root to 25.Final Answer:
20 -> Option CQuick Check:
Predecessor of 25 is 20 [OK]
- Choosing 30 which is larger than 25
- Choosing 10 which is smaller but not nearest ancestor
- Thinking 25 has no predecessor
Ready to prove it?
Pass the test and this pattern moves unknown
