📖 Scenario: You are working with a Binary Search Tree (BST) that stores numbers. You want to find the next bigger number after a given number in the tree. This is called the inorder successor.Think of the BST as a sorted list of numbers arranged in a tree shape. Finding the inorder successor means finding the number that comes right after a given number when you look at the numbers in order.
🎯 Goal: Build a program that creates a BST, then finds the inorder successor of a given node value.This will help you understand how to navigate trees and find specific nodes based on their order.
📋 What You'll Learn
Create a BST with specific nodes
Add a helper variable to store the target node value
Write a function to find the inorder successor of the target node
Print the value of the inorder successor or None if it does not exist
💡 Why This Matters
🌍 Real World
Finding the next bigger item in sorted data stored as a tree is useful in databases, file systems, and search engines.
💼 Career
Understanding BST traversal and successor logic is important for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the BST nodes
Create a class called Node with an __init__ method that takes val, left, and right parameters. Then create the following BST structure by creating nodes and linking them: root = Node(20), root.left = Node(10), root.right = Node(30), root.left.left = Node(5), root.left.right = Node(15), root.right.right = Node(40)
DSA Python
Hint
Define the Node class first. Then create the root node with value 20. Add left and right children as described.
2
Set the target node value
Create a variable called target_val and set it to 15. This is the value of the node whose inorder successor you want to find.
DSA Python
Hint
Just create a variable named target_val and assign 15 to it.
3
Write the inorder successor function
Write a function called inorder_successor that takes root and target_val as parameters. It should find the node with value target_val in the BST and return the node that is its inorder successor. Use the BST property and the following logic: - If the node has a right child, the successor is the leftmost node in the right subtree. - Otherwise, the successor is the lowest ancestor for which the node is in the left subtree. Return None if no successor exists.
DSA Python
Hint
Use a loop to find the target node. Keep track of a possible successor when moving left. If the node has a right child, go down to its leftmost child. Otherwise, return the stored successor.
4
Print the inorder successor value
Call the inorder_successor function with root and target_val. Then print the value of the returned node if it exists, or print None if there is no successor.
DSA Python
Hint
Call the function and print the value of the returned node. If it is None, print None.
Practice
(1/5)
1. What is the inorder successor of a node in a Binary Search Tree (BST)?
easy
A. The left child of the given node
B. The node with the largest key smaller than the given node's key
C. The node with the smallest key greater than the given node's key
D. The root node of the BST
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 the given node.
Final Answer:
The node with the smallest key greater than the given node's key -> Option C
Quick Check:
Inorder successor = next larger node [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 Python function signatures correctly represents a function to find the inorder successor in a BST node class?
easy
A. def inorder_successor(self, p: 'Node') -> 'Node':
B. def inorder_successor(self, root: 'Node', p: 'Node') -> 'Node':
C. def inorder_successor(root, p):
D. def inorder_successor(p: 'Node') -> 'Node':
Solution
Step 1: Identify parameters needed
To find inorder successor, we need the BST root and the target node p.
Step 2: Check function signature correctness
def inorder_successor(self, root: 'Node', p: 'Node') -> 'Node': includes self (method), root, and p with type hints, matching common BST method style.