Floor and Ceil from a BST | A Binary tree node ; 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 subtre or root has the ceil value ; Driver program to test above function
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . key = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def ceil ( root , inp ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if root . key == inp : NEW_LINE INDENT return root . key NEW_LINE DEDENT if root . key < inp : NEW_LINE INDENT return ceil ( root . right , inp ) NEW_LINE DEDENT val = ceil ( root . left , inp ) NEW_LINE return val if val >= inp else root . key NEW_LINE DEDENT root = Node ( 8 ) NEW_LINE root . left = Node ( 4 ) NEW_LINE root . right = Node ( 12 ) NEW_LINE root . left . left = Node ( 2 ) NEW_LINE root . left . right = Node ( 6 ) NEW_LINE root . right . left = Node ( 10 ) NEW_LINE root . right . right = Node ( 14 ) NEW_LINE for i in range ( 16 ) : NEW_LINE INDENT print " % ▁ d ▁ % ▁ d " % ( i , ceil ( root , i ) ) NEW_LINE DEDENT