Smallest number in BST which is greater than or equal to N ( Iterative Approach)
Last Updated :
26 Aug, 2022
Given a Binary Search Tree and a number N, the task is to find the smallest number in the binary search tree that is greater than or equal to N.
Examples:
Input: N = 5
8
/ \
7 10
/ / \
2 9 13
Output: 7
As 7 is the smallest number in BST which is greater than N = 5.
Input: N = 10
8
/ \
5 11
/ \
2 7
\
3
Output: 11
As 11 is the smallest number in BST which is greater than N = 10.
A recursive solution for this problem has been already been discussed in this post. Below is an iterative approach for the problem:
Using Morris Traversal the above problem can be solved in constant space. Find the inorder successor of the target. Keep two pointers, one pointing to the current node and one for storing the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
Node *left, *right;
};
Node* newNode( int item)
{
Node* temp = new Node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
Node* insert(Node* node, int key)
{
if (node == NULL)
return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}
int findFloor(Node* root, int key)
{
Node *curr = root, *ans = NULL;
while (curr) {
if (curr->key > key) {
ans = curr;
curr = curr->left;
}
else if (curr->key == key) {
ans = curr;
break ;
}
else
curr = curr->right;
}
if (ans != NULL)
return ans->key;
return -1;
}
int main()
{
int N = 13;
Node* root = insert(root, 19);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 12);
insert(root, 9);
insert(root, 21);
insert(root, 25);
printf ( "%d" , findFloor(root, 15));
return 0;
}
|
Java
class GFG
{
static class Node
{
int key;
Node left, right;
};
static Node newNode( int item)
{
Node temp = new Node();
temp.key = item;
temp.left = temp.right = null ;
return temp;
}
static Node insert(Node node, int key)
{
if (node == null )
{
return newNode(key);
}
if (key < node.key)
{
node.left = insert(node.left, key);
}
else if (key > node.key)
{
node.right = insert(node.right, key);
}
return node;
}
static int findFloor(Node root, int key)
{
Node curr = root, ans = null ;
while (curr != null )
{
if (curr.key > key)
{
ans = curr;
curr = curr.left;
}
else if (curr.key == key)
{
ans = curr;
break ;
}
else
{
curr = curr.right;
}
}
if (ans != null )
{
return ans.key;
}
return - 1 ;
}
public static void main(String[] args)
{
int N = 13 ;
Node root = new Node();
insert(root, 19 );
insert(root, 2 );
insert(root, 1 );
insert(root, 3 );
insert(root, 12 );
insert(root, 9 );
insert(root, 21 );
insert(root, 25 );
System.out.printf( "%d" , findFloor(root, 15 ));
}
}
|
Python3
class Node:
def __init__( self , key):
self .key = key
self .left = None
self .right = None
def insert(node: Node, key: int ) - > Node:
if (node is None ):
return Node(key)
if (key < node.key):
node.left = insert(node.left, key)
elif (key > node.key):
node.right = insert(node.right, key)
return node
def findFloor(root: Node, key: int ) - > int :
curr = root
ans = None
while (curr):
if (curr.key > key):
ans = curr
curr = curr.left
elif (curr.key = = key):
ans = curr
break
else :
curr = curr.right
if (ans ! = None ):
return ans.key
return - 1
if __name__ = = "__main__" :
N = 13
root = None
root = insert(root, 19 )
insert(root, 2 )
insert(root, 1 )
insert(root, 3 )
insert(root, 12 )
insert(root, 9 )
insert(root, 21 )
insert(root, 25 )
print (findFloor(root, 15 ))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
class Node
{
public int key;
public Node left, right;
};
static Node newNode( int item)
{
Node temp = new Node();
temp.key = item;
temp.left = temp.right = null ;
return temp;
}
static Node insert(Node node, int key)
{
if (node == null )
{
return newNode(key);
}
if (key < node.key)
{
node.left = insert(node.left, key);
}
else if (key > node.key)
{
node.right = insert(node.right, key);
}
return node;
}
static int findFloor(Node root, int key)
{
Node curr = root, ans = null ;
while (curr != null )
{
if (curr.key > key)
{
ans = curr;
curr = curr.left;
}
else if (curr.key == key)
{
ans = curr;
break ;
}
else
{
curr = curr.right;
}
}
if (ans != null )
{
return ans.key;
}
return -1;
}
public static void Main(String[] args)
{
Node root = new Node();
insert(root, 19);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 12);
insert(root, 9);
insert(root, 21);
insert(root, 25);
Console.Write( "{0}" , findFloor(root, 15));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .key = 0;
this .left = null ;
this .right = null ;
}
};
function newNode(item)
{
var temp = new Node();
temp.key = item;
temp.left = temp.right = null ;
return temp;
}
function insert(node, key)
{
if (node == null )
{
return newNode(key);
}
if (key < node.key)
{
node.left = insert(node.left, key);
}
else if (key > node.key)
{
node.right = insert(node.right, key);
}
return node;
}
function findFloor(root, key)
{
var curr = root, ans = null ;
while (curr != null )
{
if (curr.key > key)
{
ans = curr;
curr = curr.left;
}
else if (curr.key == key)
{
ans = curr;
break ;
}
else
{
curr = curr.right;
}
}
if (ans != null )
{
return ans.key;
}
return -1;
}
var root = new Node();
insert(root, 19);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 12);
insert(root, 9);
insert(root, 21);
insert(root, 25);
document.write(findFloor(root, 15));
</script>
|
Complexity Analysis:
- Time complexity: O(N)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...