Given a Binary Search Tree and a number x, find the floor of x in the given BST:
Examples:
Input: x = 14 and root of below tree
10
/ \
5 15
/ \
12 30
Output: 12
Input: x = 15 and root of below tree
10
/ \
5 15
/ \
12 30
Output: 15

Naive Approach: To solve the problem follow the below idea:
A simple solution is to traverse the tree using (Inorder or Preorder or Postorder) and keep track of the closest smaller or same element
Efficient Approach: To solve the problem follow the below idea:
We can efficiently find the closest smaller or same element in O(H) time where H is the height of BST.
Follow the given steps to solve the problem:
- Start at the root Node
- If root->data == key, the floor of the key is equal to the root.
- Else if root->data > key, then the floor of the key must lie in the left subtree.
- Else floor may lie in the right subtree but only if there is a value lesser than or equal to the key.
- If not, then the root is the key.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->left = temp->right = NULL;
temp->data = key;
return temp;
}
Node* insert(Node* root, int key)
{
if (!root)
return newNode(key);
if (key < root->data)
root->left = insert(root->left, key);
else
root->right = insert(root->right, key);
return root;
}
int floor (Node* root, int key)
{
if (!root)
return INT_MAX;
if (root->data == key)
return root->data;
if (root->data > key)
return floor (root->left, key);
int floorValue = floor (root->right, key);
return (floorValue <= key) ? floorValue : root->data;
}
int main()
{
Node* root = NULL;
root = insert(root, 7);
insert(root, 10);
insert(root, 5);
insert(root, 3);
insert(root, 6);
insert(root, 8);
insert(root, 12);
cout << floor (root, 9) << endl;
return 0;
}
|
Java
import java.io.*;
class GfG {
static class Node {
int data;
Node left, right;
}
static Node newNode( int key)
{
Node temp = new Node();
temp.left = null ;
temp.right = null ;
temp.data = key;
return temp;
}
static Node insert(Node root, int key)
{
if (root == null )
return newNode(key);
if (key < root.data)
root.left = insert(root.left, key);
else
root.right = insert(root.right, key);
return root;
}
static int floor(Node root, int key)
{
if (root == null )
return Integer.MAX_VALUE;
if (root.data == key)
return root.data;
if (root.data > key)
return floor(root.left, key);
int floorValue = floor(root.right, key);
return (floorValue <= key) ? floorValue : root.data;
}
public static void main(String[] args)
{
Node root = null ;
root = insert(root, 7 );
insert(root, 10 );
insert(root, 5 );
insert(root, 3 );
insert(root, 6 );
insert(root, 8 );
insert(root, 12 );
System.out.println(floor(root, 9 ));
}
}
|
Python3
INT_MAX = 2147483647
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def insert(root, key):
if ( not root):
return newNode(key)
if (key < root.data):
root.left = insert(root.left, key)
else :
root.right = insert(root.right, key)
return root
def floor(root, key):
if ( not root):
return INT_MAX
if (root.data = = key):
return root.data
if (root.data > key):
return floor(root.left, key)
floorValue = floor(root.right, key)
return floorValue if (floorValue < = key) else root.data
if __name__ = = '__main__' :
root = None
root = insert(root, 7 )
insert(root, 10 )
insert(root, 5 )
insert(root, 3 )
insert(root, 6 )
insert(root, 8 )
insert(root, 12 )
print (floor(root, 9 ))
|
C#
using System;
class GfG {
public class Node {
public int data;
public Node left, right;
}
static Node newNode( int key)
{
Node temp = new Node();
temp.left = null ;
temp.right = null ;
temp.data = key;
return temp;
}
static Node insert(Node root, int key)
{
if (root == null )
return newNode(key);
if (key < root.data)
root.left = insert(root.left, key);
else
root.right = insert(root.right, key);
return root;
}
static int floor(Node root, int key)
{
if (root == null )
return int .MaxValue;
if (root.data == key)
return root.data;
if (root.data > key)
return floor(root.left, key);
int floorValue = floor(root.right, key);
return (floorValue <= key) ? floorValue : root.data;
}
public static void Main(String[] args)
{
Node root = null ;
root = insert(root, 7);
insert(root, 10);
insert(root, 5);
insert(root, 3);
insert(root, 6);
insert(root, 8);
insert(root, 12);
Console.WriteLine(floor(root, 9));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data=0;
this .left= this .right= null ;
}
}
function newNode(key)
{
let temp = new Node();
temp.left = null ;
temp.right = null ;
temp.data = key;
return temp;
}
function insert(root,key)
{
if (root == null )
return newNode(key);
if (key < root.data)
root.left = insert(root.left, key);
else
root.right = insert(root.right, key);
return root;
}
function floor(root,key)
{
if (root == null )
return Number.MAX_VALUE;
if (root.data == key)
return root.data;
if (root.data > key)
return floor(root.left, key);
let floorValue = floor(root.right, key);
return (floorValue <= key) ? floorValue : root.data;
}
let root = null ;
root = insert(root, 7);
insert(root, 10);
insert(root, 5);
insert(root, 3);
insert(root, 6);
insert(root, 8);
insert(root, 12);
document.write(floor(root, 9));
</script>
|
Time Complexity: O(H), where H is the height of the tree
Auxiliary Space: O(1)