data_structures.binary_tree.floor_and_ceiling ============================================= .. py:module:: data_structures.binary_tree.floor_and_ceiling .. autoapi-nested-parse:: In a binary search tree (BST): * The floor of key 'k' is the maximum value that is smaller than or equal to 'k'. * The ceiling of key 'k' is the minimum value that is greater than or equal to 'k'. Reference: https://bit.ly/46uB0a2 Author : Arunkumar Date : 14th October 2023 Classes ------- .. autoapisummary:: data_structures.binary_tree.floor_and_ceiling.Node Functions --------- .. autoapisummary:: data_structures.binary_tree.floor_and_ceiling.floor_ceiling Module Contents --------------- .. py:class:: Node .. py:method:: __iter__() -> collections.abc.Iterator[int] .. py:method:: __len__() -> int .. py:attribute:: key :type: int .. py:attribute:: left :type: Node | None :value: None .. py:attribute:: right :type: Node | None :value: None .. py:function:: floor_ceiling(root: Node | None, key: int) -> tuple[int | None, int | None] Find the floor and ceiling values for a given key in a Binary Search Tree (BST). Args: root: The root of the binary search tree. key: The key for which to find the floor and ceiling. Returns: A tuple containing the floor and ceiling values, respectively. Examples: >>> root = Node(10) >>> root.left = Node(5) >>> root.right = Node(20) >>> root.left.left = Node(3) >>> root.left.right = Node(7) >>> root.right.left = Node(15) >>> root.right.right = Node(25) >>> tuple(root) (3, 5, 7, 10, 15, 20, 25) >>> floor_ceiling(root, 8) (7, 10) >>> floor_ceiling(root, 14) (10, 15) >>> floor_ceiling(root, -1) (None, 3) >>> floor_ceiling(root, 30) (25, None)