Inorder Successor in Binary Search Tree
Last Updated :
28 Jan, 2025
In the Binary Tree, the Inorder successor of a node is the next node in the Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inorder traversal.
Example:
In the below diagram, inorder successor of 8 is 10, inorder successor of 10 is 12 and inorder successor of 14 is 20.
We have discussed different methods for Inorder successor in a Binary Tree. These methods either work in O(n) time or expect parent pointer/reference to be present in every node. We can use Binary Search Tree properties to efficiently find the successor in O(h) time.
Approach:
We follow the idea of normal BST Search. In BST search, we get closer to the key by comparing with the current node. So the last greater key visited during search is the successor. The following cases arise during the search.
- If we reach null, then the given target does not exist, we return null
- If current node matches the target and right child is not empty, then successor is leftmost node in right subtree.
- If current node is greater , then it is a potential successor, we mark it as successor and proceed to left
- If current node is smaller or equal to the target, we proceed to right.
Below is the implementation of the above approach:
C++
// C++ Program to find Inorder Successor in
// Binary Search Tree
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int x) {
data = x;
left = nullptr;
right = nullptr;
}
};
Node* leftMost(Node* node) {
Node* curr = node;
while (curr->left != nullptr)
curr = curr->left;
return curr;
}
Node* getSucc(Node* root, int target) {
// Base Case 1: No Inorder Successor
if (root == nullptr)
return nullptr;
// Base Case 2: root is same as target and
// right child is not empty
if (root->data == target && root->right != nullptr)
return leftMost(root->right);
// Use BST properties to search for
// target and successor
Node* succ = nullptr;
Node* curr = root;
while (curr != nullptr) {
// If curr node is greater, then it
// is a potential successor
if (target < curr->data) {
succ = curr;
curr = curr->left;
}
// If smaller, then successor must
// be in the right child
else if (target >= curr->data)
curr = curr->right;
}
return succ;
}
int main() {
// Construct a BST
// 20
// / \
// 8 22
// / \
// 4 12
// / \
// 10 14
Node *root = new Node(20);
root->left = new Node(8);
root->right = new Node(22);
root->left->left = new Node(4);
root->left->right = new Node(12);
root->left->right->left = new Node(10);
root->left->right->right = new Node(14);
int target = 14;
Node* succ = getSucc(root, target);
if (succ != nullptr)
cout << succ->data;
else
cout << "null";
return 0;
}
C
// C Program to find Inorder Successor
// in Binary Search Tree
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to find leftmost node in
// subtree with given root.
struct Node* leftMost(struct Node* node) {
struct Node* curr = node;
while (curr->left != NULL)
curr = curr->left;
return curr;
}
struct Node* getSucc(struct Node* root, int target) {
// Base Case 1: No Inorder Successor
if (root == NULL)
return NULL;
// Base Case 2: root is same as target and
// right child is not empty
if (root->data == target && root->right != NULL)
return leftMost(root->right);
// Use BST properties to search for
// target and successor
struct Node* succ = NULL;
struct Node* curr = root;
while (curr != NULL) {
// If curr node is greater, then it
// is a potential successor
if (target < curr->data) {
succ = curr;
curr = curr->left;
}
// If smaller, then successor must
// be in the right child
else if (target >= curr->data)
curr = curr->right;
}
return succ;
}
struct Node* createNode(int x) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
int main() {
// Construct a BST
// 20
// / \
// 8 22
// / \
// 4 12
// / \
// 10 14
struct Node *root = createNode(20);
root->left = createNode(8);
root->right = createNode(22);
root->left->left = createNode(4);
root->left->right = createNode(12);
root->left->right->left = createNode(10);
root->left->right->right = createNode(14);
int target = 14;
struct Node* succ = getSucc(root, target);
if (succ != NULL)
printf("%d", succ->data);
else
printf("null");
return 0;
}
Java
// Java Program to find Inorder Successor
// in Binary Search Tree
class Node {
int data;
Node left, right;
Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Function to find leftmost node in
// subtree with given root.
static Node leftMost(Node node) {
Node curr = node;
while (curr.left != null)
curr = curr.left;
return curr;
}
static Node getSucc(Node root, int target) {
// Base Case 1: No Inorder Successor
if (root == null)
return null;
// Base Case 2: root is same as target and
// right child is not empty
if (root.data == target && root.right != null)
return leftMost(root.right);
// Use BST properties to search for
// target and successor
Node succ = null;
Node curr = root;
while (curr != null) {
// If curr node is greater, then it
// is a potential successor
if (target < curr.data) {
succ = curr;
curr = curr.left;
}
// If smaller, then successor must
// be in the right child
else if (target >= curr.data)
curr = curr.right;
}
return succ;
}
public static void main(String[] args) {
// Construct a BST
// 20
// / \
// 8 22
// / \
// 4 12
// / \
// 10 14
Node root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(4);
root.left.right = new Node(12);
root.left.right.left = new Node(10);
root.left.right.right = new Node(14);
int target = 14;
Node succ = getSucc(root, target);
if (succ != null)
System.out.println(succ.data);
else
System.out.println("null");
}
}
Python
# Python Program to find Inorder
# successor in Binary Search Tree
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# Function to find leftmost node in
# subtree with given root.
def leftMost(node):
curr = node
while curr.left is not None:
curr = curr.left
return curr
def getSucc(root, target):
# Base Case 1: No Inorder Successor
if root is None:
return None
# Base Case 2: root is same as target and
# right child is not empty
if root.data == target and root.right is not None:
return leftMost(root.right)
# Use BST properties to search for
# target and successor
succ = None
curr = root
while curr is not None:
# If curr node is greater, then it
# is a potential successor
if target < curr.data:
succ = curr
curr = curr.left
# If smaller, then successor must
# be in the right child
elif target >= curr.data:
curr = curr.right
return succ
if __name__ == '__main__':
# Construct a BST
# 20
# / \
# 8 22
# / \
# 4 12
# / \
# 10 14
root = Node(20)
root.left = Node(8)
root.right = Node(22)
root.left.left = Node(4)
root.left.right = Node(12)
root.left.right.left = Node(10)
root.left.right.right = Node(14)
target = 14
succ = getSucc(root, target)
if succ is not None:
print(succ.data)
else:
print("null")
C#
// C# Program to find Inorder Successor
// in Binary Search Tree
using System;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Function to find leftmost node in
// subtree with given root.
static Node leftMost(Node node) {
Node curr = node;
while (curr.left != null)
curr = curr.left;
return curr;
}
static Node getSucc(Node root, int target) {
// Base Case 1: No Inorder Successor
if (root == null)
return null;
// Base Case 2: root is same as target and
// right child is not empty
if (root.data == target && root.right != null)
return leftMost(root.right);
// Use BST properties to search for
// target and successor
Node succ = null;
Node curr = root;
while (curr != null) {
// If curr node is greater, then it
// is a potential successor
if (target < curr.data) {
succ = curr;
curr = curr.left;
}
// If smaller, then successor must
// be in the right child
else if (target >= curr.data)
curr = curr.right;
}
return succ;
}
static void Main(string[] args) {
// Construct a BST
// 20
// / \
// 8 22
// / \
// 4 12
// / \
// 10 14
Node root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(4);
root.left.right = new Node(12);
root.left.right.left = new Node(10);
root.left.right.right = new Node(14);
int target = 14;
Node succ = getSucc(root, target);
if (succ != null)
Console.WriteLine(succ.data);
else
Console.WriteLine("null");
}
}
JavaScript
// JavaScript Program to find Inorder
// Successor in Binary Search Tree
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// Function to find leftmost node in
// subtree with given root.
function leftMost(node) {
let curr = node;
while (curr.left !== null) {
curr = curr.left;
}
return curr;
}
function getSucc(root, target) {
// Base Case 1: No Inorder Successor
if (root === null)
return null;
// Base Case 2: root is same as target and
// right child is not empty
if (root.data === target && root.right !== null)
return leftMost(root.right);
// Use BST properties to search for
// target and successor
let succ = null;
let curr = root;
while (curr !== null) {
// If curr node is greater, then it
// is a potential successor
if (target < curr.data) {
succ = curr;
curr = curr.left;
}
// If smaller, then successor must
// be in the right child
else if (target >= curr.data) {
curr = curr.right;
}
}
return succ;
}
// Construct a BST
// 20
// / \
// 8 22
// / \
// 4 12
// / \
// 10 14
const root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(4);
root.left.right = new Node(12);
root.left.right.left = new Node(10);
root.left.right.right = new Node(14);
const target = 14;
const succ = getSucc(root, target);
if (succ !== null)
console.log(succ.data);
else
console.log("null");
Time Complexity: O(h), where h is the height of the tree.
Auxiliary Space: O(1)
Similar Reads
Binary Search Tree A Binary Search Tree (BST) is a type of binary tree data structure in which each node contains a unique key and satisfies a specific ordering property:All nodes in the left subtree of a node contain values strictly less than the nodeâs value. All nodes in the right subtree of a node contain values s
4 min read
Introduction to Binary Search Tree Binary Search Tree is a data structure used in computer science for organizing and storing data in a sorted manner. Binary search tree follows all properties of binary tree and for every nodes, its left subtree contains values less than the node and the right subtree contains values greater than the
3 min read
Applications of BST Binary Search Tree (BST) is a data structure that is commonly used to implement efficient searching, insertion, and deletion operations along with maintaining sorted sequence of data. Please remember the following properties of BSTs before moving forward.The left subtree of a node contains only node
3 min read
Applications, Advantages and Disadvantages of Binary Search Tree A Binary Search Tree (BST) is a data structure used to storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right child containing values greater than the p
2 min read
Insertion in Binary Search Tree (BST) Given a BST, the task is to insert a new node in this BST.Example: How to Insert a value in a Binary Search Tree:A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching for a key from the root until we hit a leaf node. Once a leaf node is fo
15 min read
Searching in Binary Search Tree (BST) Given a BST, the task is to search a node in this BST. For searching a value in BST, consider it as a sorted array. Now we can easily perform search operation in BST using Binary Search Algorithm. Input: Root of the below BST Output: TrueExplanation: 8 is present in the BST as right child of rootInp
7 min read
Deletion in Binary Search Tree (BST) Given a BST, the task is to delete a node in this BST, which can be broken down into 3 scenarios:Case 1. Delete a Leaf Node in BST Case 2. Delete a Node with Single Child in BSTDeleting a single child node is also simple in BST. Copy the child to the node and delete the node. Case 3. Delete a Node w
10 min read
Binary Search Tree (BST) Traversals â Inorder, Preorder, Post Order Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree. Input: A Binary Search TreeOutput: Inorder Traversal: 10 20 30 100 150 200 300Preorder Traversal: 100 20 10 30 200 150 300Postorder Traversal: 10 30 20 150 300 200 1
10 min read
Balance a Binary Search Tree Given a BST (Binary Search Tree) that may be unbalanced, the task is to convert it into a balanced BST that has the minimum possible height.Examples: Input: Output: Explanation: The above unbalanced BST is converted to balanced with the minimum possible height.Input: Output: Explanation: The above u
10 min read
Self-Balancing Binary Search Trees Self-Balancing Binary Search Trees are height-balanced binary search trees that automatically keep the height as small as possible when insertion and deletion operations are performed on the tree. The height is typically maintained in order of logN so that all operations take O(logN) time on average
4 min read