Given a Binary Search Tree and a number x, we have to find the floor of x in the given BST, where floor means the greatest value node of the BST which is smaller than or equal to x. if x is smaller than the smallest node of BST then return -1.
[Expected Approach - 1] Using Recursion - O(h) Time and O(h) Space
The idea is to find the floor in a BST by traversing recursively: if the node’s value is greater than the key, move left; if it is less than or equal to the key, record it and move right.
Follow the given steps to solve the problem:
Start at the root node.
If root->data == x, the floor of x is equal to the root.
Else if root->data > x, the floor must lie in the left subtree.
Otherwise, search in the right subtree for a value less than or equal to x, but if not found, the root itself is the floor.
C++
// C++ implementation to find floor of given // number x in BST#include<iostream>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// This function is used to find floor // of given number xintfloor(Node*root,intx){// Base case: return -1 if no floor foundif(root==nullptr){return-1;}// If the root's data is equal to x, // we've found the floorif(root->data==x){returnroot->data;}// If root's data is greater than x, // search in the left subtreeif(root->data>x){returnfloor(root->left,x);}// Else, search in the right subtree // and compare with current rootintfloorValue=floor(root->right,x);// If the right subtree returns //a valid floor, return that// Otherwise, return the current root's datareturn(floorValue<=x&&floorValue!=-1)?floorValue:root->data;}intmain(){// Representation of the given tree// 10// / \ // 5 15// / \ // 12 30 Node*root=newNode(10);root->left=newNode(5);root->right=newNode(15);root->right->left=newNode(12);root->right->right=newNode(30);intx=14;cout<<floor(root,x)<<endl;return0;}
C
// C implementation to find floor of given // number x in BST#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left;structNode*right;};// This function is used to find floor // of given number xintfloor(structNode*root,intx){// Base case: return -1 if no floor foundif(root==NULL){return-1;}// If the root's data is equal to x, // we've found the floorif(root->data==x){returnroot->data;}// If root's data is greater than x, // search in the left subtreeif(root->data>x){returnfloor(root->left,x);}// Else, search in the right subtree // and compare with current rootintfloorValue=floor(root->right,x);// If the right subtree returns // a valid floor, return that// Otherwise, return the current root's datareturn(floorValue<=x&&floorValue!=-1)?floorValue:root->data;}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->left=NULL;newNode->right=NULL;returnnewNode;}intmain(){// Representation of the given tree// 10// / \ // 5 15// / \ // 12 30 structNode*root=createNode(10);root->left=createNode(5);root->right=createNode(15);root->right->left=createNode(12);root->right->right=createNode(30);intx=14;printf("%d\n",floor(root,x));return0;}
Java
// Java implementation to find floor of given // number x in BSTclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}// This function is used to find floor // of given number xclassGfG{staticintfloor(Noderoot,intx){// Base case: return -1 if no floor foundif(root==null){return-1;}// If the root's data is equal to x, // we've found the floorif(root.data==x){returnroot.data;}// If root's data is greater than x, // search in the left subtreeif(root.data>x){returnfloor(root.left,x);}// Else, search in the right subtree // and compare with current rootintfloorValue=floor(root.right,x);// If the right subtree returns a valid floor,// return that, otherwise return the current root's datareturn(floorValue<=x&&floorValue!=-1)?floorValue:root.data;}publicstaticvoidmain(String[]args){// Representation of the given tree// 10// / \// 5 15// / \// 12 30 Noderoot=newNode(10);root.left=newNode(5);root.right=newNode(15);root.right.left=newNode(12);root.right.right=newNode(30);intx=14;System.out.println(floor(root,x));}}
Python
# Python implementation to find floor of given # number x in BSTclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# This function is used to find floor # of given number xdeffind_floor(root,x):# Base case: return -1 if no floor foundifrootisNone:return-1# If the root's data is equal to x, # we've found the floorifroot.data==x:returnroot.data# If root's data is greater than x, # search in the left subtreeifroot.data>x:returnfind_floor(root.left,x)# Else, search in the right subtree # and compare with current rootfloor_value=find_floor(root.right,x)# If the right subtree returns a valid floor,# return that, otherwise return the current root's datareturnfloor_valueiffloor_value<=x \
andfloor_value!=-1elseroot.dataif__name__=="__main__":# Representation of the given tree# 10# / \# 5 15# / \# 12 30 root=Node(10)root.left=Node(5)root.right=Node(15)root.right.left=Node(12)root.right.right=Node(30)x=14print(find_floor(root,x))
C#
// C# implementation to find floor of given // number x in BSTusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}// This function is used to find floor // of given number xclassGfG{staticintFindFloor(Noderoot,intx){// Base case: return -1 if no floor foundif(root==null){return-1;}// If the root's data is equal to x, // we've found the floorif(root.data==x){returnroot.data;}// If root's data is greater than x, // search in the left subtreeif(root.data>x){returnFindFloor(root.left,x);}// Else, search in the right subtree // and compare with current rootintfloorValue=FindFloor(root.right,x);// If the right subtree returns a valid floor,// return that, otherwise return the current root's datareturn(floorValue<=x&&floorValue!=-1)?floorValue:root.data;}staticvoidMain(string[]args){// Representation of the given tree// 10// / \// 5 15// / \// 12 30 Noderoot=newNode(10);root.left=newNode(5);root.right=newNode(15);root.right.left=newNode(12);root.right.right=newNode(30);intx=14;Console.WriteLine(FindFloor(root,x));}}
JavaScript
// JavaScript implementation to find floor of given // number x in BSTclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// This function is used to find floor // of given number xfunctionfindFloor(root,x){// Base case: return -1 if no floor foundif(root===null){return-1;}// If the root's data is equal to x, // we've found the floorif(root.data===x){returnroot.data;}// If root's data is greater than x, // search in the left subtreeif(root.data>x){returnfindFloor(root.left,x);}// Else, search in the right subtree // and compare with current rootletfloorValue=findFloor(root.right,x);// If the right subtree returns a valid floor,// return that, otherwise return the current root's datareturn(floorValue<=x&&floorValue!==-1)?floorValue:root.data;}// Driver Code// Representation of the given tree// 10// / \// 5 15// / \// 12 30 letroot=newNode(10);root.left=newNode(5);root.right=newNode(15);root.right.left=newNode(12);root.right.right=newNode(30);letx=14;console.log(findFloor(root,x));
Output
12
[Expected Approach - 2] Iterative Solution- O(h) Time and O(1) Space
The idea is to iteratively traverse the BST to find the greatest value smaller than or equal to x. If the current node's data is greater than x, move left. If it's smaller, update the floor value and move right. This process continues until the closest floor value is found.
Below is the implementation of the above approach:
C++
// C++ implementation to find floor of given // number x in BST#include<iostream>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// This function is used to find floor // of given number xintfloor(Node*root,intx){// Initialize variable to store the floor valueintfloorValue=-1;while(root!=nullptr){// If the root's data is equal to x, // we've found the floorif(root->data==x){returnroot->data;}// If root's data is greater than x, // search in the left subtreeif(root->data>x){root=root->left;}else{// Update floorValue and move to the // right subtreefloorValue=root->data;root=root->right;}}returnfloorValue;}intmain(){// Representation of the given tree// 10// / \ // 5 15// / \ // 12 30 Node*root=newNode(10);root->left=newNode(5);root->right=newNode(15);root->right->left=newNode(12);root->right->right=newNode(30);intx=14;cout<<floor(root,x)<<endl;return0;}
C
// C implementation to find floor of given // number x in BST#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left;structNode*right;};// This function is used to find floor // of given number xintfloor(structNode*root,intx){// Initialize variable to store the floor valueintfloorValue=-1;while(root!=NULL){// If the root's data is equal to x, // we've found the floorif(root->data==x){returnroot->data;}// If root's data is greater than x, // search in the left subtreeif(root->data>x){root=root->left;}else{// Update floorValue and move to the // right subtreefloorValue=root->data;root=root->right;}}// Return the floor value foundreturnfloorValue;}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->left=NULL;newNode->right=NULL;returnnewNode;}intmain(){// Representation of the given tree// 10// / \ // 5 15// / \ // 12 30 structNode*root=createNode(10);root->left=createNode(5);root->right=createNode(15);root->right->left=createNode(12);root->right->right=createNode(30);intx=14;printf("%d\n",floor(root,x));return0;}
Java
// Java implementation to find floor of given // number x in BSTclassNode{intdata;Nodeleft;Noderight;Node(intx){data=x;left=null;right=null;}}classGfG{// This function is used to find floor // of given number xstaticintfloor(Noderoot,intx){// Initialize variable to store the floor valueintfloorValue=-1;while(root!=null){// If the root's data is equal to x, // we've found the floorif(root.data==x){returnroot.data;}// If root's data is greater than x, // search in the left subtreeif(root.data>x){root=root.left;}else{// Update floorValue and move to the // right subtreefloorValue=root.data;root=root.right;}}// Return the floor value foundreturnfloorValue;}publicstaticvoidmain(String[]args){// Representation of the given tree// 10// / \// 5 15// / \// 12 30 Noderoot=newNode(10);root.left=newNode(5);root.right=newNode(15);root.right.left=newNode(12);root.right.right=newNode(30);intx=14;System.out.println(floor(root,x));}}
Python
# Python implementation to find floor of given # number x in BSTclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# This function is used to find floor # of given number xdeffloor(root,x):# Initialize variable to store the floor valuefloor_value=-1whilerootisnotNone:# If the root's data is equal to x, # we've found the floorifroot.data==x:returnroot.data# If root's data is greater than x, # search in the left subtreeifroot.data>x:root=root.leftelse:# Update floor_value and move to the # right subtreefloor_value=root.dataroot=root.rightreturnfloor_valueif__name__=="__main__":# Representation of the given tree# 10# / \# 5 15# / \# 12 30 root=Node(10)root.left=Node(5)root.right=Node(15)root.right.left=Node(12)root.right.right=Node(30)x=14print(floor(root,x))
C#
// C# implementation to find floor of given // number x in BSTusingSystem;classNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intx){data=x;left=null;right=null;}}// This function is used to find floor // of given number xclassGfG{staticintFloor(Noderoot,intx){// Initialize variable to store the floor valueintfloorValue=-1;while(root!=null){// If the root's data is equal to x, // we've found the floorif(root.data==x){returnroot.data;}// If root's data is greater than x, // search in the left subtreeif(root.data>x){root=root.left;}else{// Update floorValue and move to the // right subtreefloorValue=root.data;root=root.right;}}// Return the floor value foundreturnfloorValue;}staticvoidMain(){// Representation of the given tree// 10// / \// 5 15// / \// 12 30 Noderoot=newNode(10);root.left=newNode(5);root.right=newNode(15);root.right.left=newNode(12);root.right.right=newNode(30);intx=14;Console.WriteLine(Floor(root,x));}}
JavaScript
// JavaScript implementation to find floor of given // number x in BSTclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// This function is used to find floor // of given number xfunctionfloor(root,x){// Initialize variable to store the floor valueletfloorValue=-1;while(root!==null){// If the root's data is equal to x, // we've found the floorif(root.data===x){returnroot.data;}// If root's data is greater than x, // search in the left subtreeif(root.data>x){root=root.left;}else{// Update floorValue and move to the // right subtreefloorValue=root.data;root=root.right;}}returnfloorValue;}// Driver Code// Representation of the given tree// 10// / \// 5 15// / \// 12 30 constroot=newNode(10);root.left=newNode(5);root.right=newNode(15);root.right.left=newNode(12);root.right.right=newNode(30);constx=14;console.log(floor(root,x));