Discover how to jump to the next item in a sorted tree without searching everything!
Why BST Inorder Successor in DSA C?
Imagine you have a family tree drawn on paper, and you want to find the next person in order after a given family member. Doing this by hand means scanning the entire tree every time, which is slow and confusing.
Manually searching for the next person in order means checking many family members one by one. This is slow and easy to make mistakes, especially if the tree is big or unbalanced.
The BST Inorder Successor method helps you quickly find the next person in order by using the tree's structure. It avoids scanning the whole tree and finds the answer by following clear steps.
int findSuccessor(Node* root, int key) {
int successor = -1;
inorderTraversal(root, &successor, key);
return successor;
}
// This scans the whole tree every time.Node* findInorderSuccessor(Node* root, Node* target) {
if (target->right != NULL) {
return findMin(target->right);
}
Node* successor = NULL;
Node* current = root;
while (current != NULL) {
if (target->data < current->data) {
successor = current;
current = current->left;
} else {
current = current->right;
}
}
return successor;
}This concept enables fast and reliable finding of the next item in sorted order without checking every node.
In a contact list sorted by names, finding the next contact after a given name quickly helps you jump through the list without scrolling all contacts.
Manual search is slow and error-prone for finding next in order.
BST Inorder Successor uses tree structure for efficient searching.
It helps quickly find the next bigger value in a sorted tree.
Practice
Solution
Step 1: Understand inorder traversal in BST
Inorder traversal visits nodes in ascending order of their keys.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 BQuick Check:
Inorder successor = next bigger node [OK]
- Confusing successor with predecessor
- Thinking successor is always right child
- Assuming successor is left child
Solution
Step 1: Analyze function parameters
The function needs the BST root and the node to find successor for, both as pointers.Step 2: Analyze return type
It should return a pointer to the successor node, so return type is struct Node*.Final Answer:
struct Node* inorderSuccessor(struct Node* root, struct Node* n); -> Option AQuick Check:
Correct signature uses pointers for nodes [OK]
- Using int instead of struct Node*
- Missing node parameter
- Returning struct Node by value
20
/ \
10 30
\ \
15 40
Solution
Step 1: Locate node 15 and find its right subtree
Node 15 has no right child, so successor is an ancestor.Step 2: Move up to find the lowest ancestor greater than 15
Parent 10 is less than 15, move up to 20 which is greater than 15.Final Answer:
20 -> Option CQuick Check:
Next bigger after 15 is 20 [OK]
- Choosing right child 30 incorrectly
- Choosing smaller ancestor 10
- Confusing successor with predecessor
struct Node* inorderSuccessor(struct Node* root, struct Node* n) {
if (n->right != NULL) {
struct Node* temp = n->right;
while (temp->left != NULL) {
temp = temp->left;
}
return temp;
}
struct Node* succ = NULL;
while (root != NULL) {
if (n->data < root->data) {
succ = root;
root = root->left;
} else {
root = root->right;
}
}
return succ;
}
What is the main logical error in this code?Solution
Step 1: Check handling of right child
Code correctly finds leftmost node in right subtree if right child exists.Step 2: Check updating successor when moving right
In the else branch (n->data >= root->data), it incorrectly sets succ = root before moving right. Successor should NOT be updated when going right, as n is in (or at) the right subtree.Step 3: Identify error
The main logical error is incorrectly updating the successor when traversing right.Final Answer:
It incorrectly updates successor when moving right -> Option AQuick Check:
Successor updates only on left moves [OK]
- Thinking recursion is required
- Updating successor when moving right
- Ignoring right subtree case
20
/ \
10 30
/ \ / \
5 15 25 40
If the node with value 25 has no right child, what is the inorder successor and why?Solution
Step 1: Check if node 25 has right child
Node 25 has no right child, so successor is an ancestor.Step 2: Find lowest ancestor greater than 25
Ancestor 30 is greater than 25 and is the lowest such ancestor.Final Answer:
30, because it is the lowest ancestor greater than 25 -> Option DQuick Check:
Successor = nearest bigger ancestor [OK]
- Choosing right child 40 incorrectly
- Saying no successor exists
- Choosing root 20 incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
