Largest number less than or equal to N in BST (Iterative Approach)
Last Updated :
22 Jul, 2022
We have a binary search tree and a number N. Our task is to find the greatest number in the binary search tree that is less than or equal to N. Print the value of the element if it exists otherwise print -1.
Examples: For the above given binary search tree-
Input : N = 24
Output :result = 21
(searching for 24 will be like-5->12->21)
Input : N = 4
Output : result = 3
(searching for 4 will be like-5->2->3)
We have discussed recursive approach in below post.
Largest number in BST which is less than or equal to N
Here an iterative approach is discussed. We try to find the predecessor of the target. Keep two pointers, one pointing to the current node and one for storing the answer. If the current node’s data > N, we move towards left. In other case, when current node’s data is less than N, the current node can be our answer (so far), and we move towards right.
Implementation:
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->right;
}
else
curr = curr->left;
}
if (ans)
return ans->key;
return -1;
}
int main()
{
int N = 25;
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, 19);
insert(root, 25);
printf ( "%d" , findFloor(root, N));
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.right;
}
else
curr = curr.left;
}
if (ans != null )
return ans.key;
return - 1 ;
}
public static void main(String[] args)
{
int N = 25 ;
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, 19 );
insert(root, 25 );
System.out.printf( "%d" , findFloor(root, N));
}
}
|
Python3
class newNode:
def __init__( self , item):
self .key = item
self .left = None
self .right = None
def insert(node, key):
if (node = = None ):
return newNode(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, key):
curr = root
ans = None
while (curr):
if (curr.key < = key):
ans = curr
curr = curr.right
else :
curr = curr.left
if (ans):
return ans.key
return - 1
if __name__ = = '__main__' :
N = 25
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, 19 )
insert(root, 25 )
print (findFloor(root, N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public 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.right;
}
else
curr = curr.left;
}
if (ans != null )
return ans.key;
return -1;
}
public static void Main(String[] args)
{
int N = 25;
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, 19);
insert(root, 25);
Console.Write( "{0}" , findFloor(root, N));
}
}
|
Javascript
<script>
class Node
{
constructor(item)
{
this .key = item;
this .left = null ;
this .right = null ;
}
}
function newNode(item)
{
let temp = new Node(item);
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)
{
let curr = root, ans = null ;
while (curr != null )
{
if (curr.key <= key)
{
ans = curr;
curr = curr.right;
}
else
curr = curr.left;
}
if (ans != null )
return ans.key;
return -1;
}
let N = 25;
let root = new Node(N);
insert(root, 19);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 12);
insert(root, 9);
insert(root, 21);
insert(root, 19);
insert(root, 25);
document.write(findFloor(root, N));
</script>
|
Share your thoughts in the comments
Please Login to comment...