Floor and Ceil from a BST | A binary tree node has key , left child and right child ; Function to find ceil of a given input in BST . If input is more than the max key in BST , return - 1 ; Base case ; We found equal key ; If root 's key is smaller,  ceil must be in right subtree ; Else , either left subtree or root has the ceil value ; Driver Code
class Node { constructor ( x ) { this . data = x ; this . left = null ; this . right = null ; } } let root ; function Ceil ( node , input ) { if ( node == null ) { return - 1 ; } if ( node . data == input ) { return node . data ; } if ( node . data < input ) { return Ceil ( node . right , input ) ; } let ceil = Ceil ( node . left , input ) ; return ( ceil >= input ) ? ceil : node . data ; } root = new Node ( 8 ) root . left = new Node ( 4 ) root . right = new Node ( 12 ) root . left . left = new Node ( 2 ) root . left . right = new Node ( 6 ) root . right . left = new Node ( 10 ) root . right . right = new Node ( 14 ) for ( let i = 0 ; i < 16 ; i ++ ) { document . write ( i + " " + Ceil ( root , i ) + " " ) ; }