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. E
(root: Node | None, key: int)
| 34 | |
| 35 | |
| 36 | def floor_ceiling(root: Node | None, key: int) -> tuple[int | None, int | None]: |
| 37 | """ |
| 38 | Find the floor and ceiling values for a given key in a Binary Search Tree (BST). |
| 39 | |
| 40 | Args: |
| 41 | root: The root of the binary search tree. |
| 42 | key: The key for which to find the floor and ceiling. |
| 43 | |
| 44 | Returns: |
| 45 | A tuple containing the floor and ceiling values, respectively. |
| 46 | |
| 47 | Examples: |
| 48 | >>> root = Node(10) |
| 49 | >>> root.left = Node(5) |
| 50 | >>> root.right = Node(20) |
| 51 | >>> root.left.left = Node(3) |
| 52 | >>> root.left.right = Node(7) |
| 53 | >>> root.right.left = Node(15) |
| 54 | >>> root.right.right = Node(25) |
| 55 | >>> tuple(root) |
| 56 | (3, 5, 7, 10, 15, 20, 25) |
| 57 | >>> floor_ceiling(root, 8) |
| 58 | (7, 10) |
| 59 | >>> floor_ceiling(root, 14) |
| 60 | (10, 15) |
| 61 | >>> floor_ceiling(root, -1) |
| 62 | (None, 3) |
| 63 | >>> floor_ceiling(root, 30) |
| 64 | (25, None) |
| 65 | """ |
| 66 | floor_val = None |
| 67 | ceiling_val = None |
| 68 | |
| 69 | while root: |
| 70 | if root.key == key: |
| 71 | floor_val = root.key |
| 72 | ceiling_val = root.key |
| 73 | break |
| 74 | |
| 75 | if key < root.key: |
| 76 | ceiling_val = root.key |
| 77 | root = root.left |
| 78 | else: |
| 79 | floor_val = root.key |
| 80 | root = root.right |
| 81 | |
| 82 | return floor_val, ceiling_val |
| 83 | |
| 84 | |
| 85 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected