Ceil in a Binary Search Tree Python
Last Updated :
31 May, 2024
In a Binary Search Tree (BST), the ceiling of a given value is the smallest element in the tree that is greater than or equal to the given value. Finding the ceiling in a BST can be a useful operation when working with ordered data. In this article, we will explore how to find the ceiling in a Binary Search Tree using Python.
Binary Search Tree Overview
A Binary Search Tree is a binary tree data structure where each node has a key/value and follows a specific ordering property. The left child of a node contains a value smaller than the node's value, while the right child contains a value greater than the node's value. This ordering property allows for efficient searching, insertion, and deletion operations.
Finding the Ceiling in a Binary Search Tree
To find the ceiling in a Binary Search Tree, we can follow these steps:
- Start at the root node of the BST.
- If the root node's value is equal to the given value, then the root node itself is the ceiling.
- If the root node's value is less than the given value, move to the right subtree recursively.
- If the root node's value is greater than the given value, move to the left subtree recursively.
- Repeat steps 2-4 until we find the smallest value greater than or equal to the given value.
Example: Let's consider the following Binary Search Tree:
8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
Suppose we want to find the ceiling of the value 5 in this BST. We can follow the steps mentioned earlier:
- Start at the root node with a value of 8.
- Since 8 is greater than 5, move to the left subtree.
- Move to the left child with a value of 3.
- Since 3 is less than 5, move to the right subtree.
- Move to the right child with a value of 6.
- Since 6 is greater than 5, move to the left subtree.
- Move to the left child with a value of 4.
- Since 4 is less than 5, move to the right subtree.
- Move to the right child with a value of 7.
- Since 7 is greater than 5, move to the left subtree.
- There are no more left children, so the current node with a value of 7 is the ceiling of 5.
Therefore, the ceiling of 5 in this BST is 7.
Implementation of Ceil in a Binary Search Tree using Python
Here's an example implementation of finding the ceiling in a Binary Search Tree using Python:
Python
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def findCeiling(root, value):
if root is None:
return None
if root.value == value:
return root.value
if root.value < value:
return findCeiling(root.right, value)
ceil = findCeiling(root.left, value)
return ceil if ceil is not None and ceil >= value else root.value
# Create the Binary Search Tree
root = Node(8)
root.left = Node(3)
root.right = Node(10)
root.left.left = Node(1)
root.left.right = Node(6)
root.left.right.left = Node(4)
root.left.right.right = Node(7)
root.right.right = Node(14)
root.right.right.left = Node(13)
# Find the ceiling of 5
ceiling = findCeiling(root, 5)
print("Ceiling of 5:", ceiling)
Conclusion
Finding the ceiling in a Binary Search Tree is a useful operation when working with ordered data. By following the steps outlined in this article and implementing the logic in Python, you can efficiently find the smallest element in a BST that is greater than or equal to a given value. Understanding this concept can be valuable in various scenarios, such as searching for the next highest value in a sorted collection of data.
Similar Reads
Binary Search Tree In Python A Binary search tree is a binary tree where the values of the left sub-tree are less than the root node and the values of the right sub-tree are greater than the value of the root node. In this article, we will discuss the binary search tree in Python.What is a Binary Search Tree(BST)?A Binary Searc
11 min read
Search a node in Binary Tree Given a Binary tree and a key. The task is to search and check if the given key exists in the binary tree or not.Examples:Input: Output: TrueInput: Output: FalseApproach:The idea is to use any of the tree traversals to traverse the tree and while traversing check if the current node matches with the
7 min read
Binary Tree in Python Binary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves.Introduction to Binary TreeRepresentation of Binar
9 min read
Red Black Tree in Python Red Black Tree is a self-balancing binary search tree where each node has an extra bit representing its color, either red or black. By constraining how nodes are colored during insertions and deletions, red-black trees ensure the tree remains approximately balanced during all operations, allowing fo
11 min read
Inorder Traversal of Binary Tree in Python Inorder traversal is defined as a type of tree traversal technique which follows the Left-Root-Right pattern, such that:The left subtree is traversed firstThen the root node for that subtree is traversedFinally, the right subtree is traversedConsider the following tree: If we perform an inorder trav
3 min read
What is Binary Search Tree A binary search tree (BST) is a binary tree in which the left subtree of a node contains only nodes with less value and the right subtree of a node contains only nodes with values greater than it. Binary Search TreeCharacteristics of Binary Search Tree: The properties of a binary search tree are as
3 min read
Implement Binary Search Tree(BST) Iterator Binary Search Trees (BSTs) are data structures, in computer science. They are commonly used for searching, insertion and deletion operations. In situations it becomes necessary to traverse a BST in an order. For example an in order traversal visits nodes in ascending order based on their values. Thi
6 min read
Java Program to Construct a Binary Search Tree Binary Search Tree (BST) is the widely used data structure in computer science, primarily known for the efficient search, insertion, and deletion operations. It is the type of binary tree where each node has at most two children, referred to as the left child and the right child. Binary Search Tree
6 min read
AVL Tree in Python The AVL tree in Python is a selfâbalancing binary search tree that guarantees the difference of the heights of the left and right subtrees of a node is at most 1. The algorithm is named after its inventors, Georgy Adelson-Velsky, and Evgenii Landis who published their paper in 1962. The AVL tree kee
6 min read
Implementing a Binary Tree in Java A binary tree is a hierarchical data structure composed of the nodes. Each node contains the value and references to its left child node and right child node, which are also binary trees that are possibly null. The structure resembles the tree with the nodes branching out from a central root, where
5 min read