Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check whether one value is present in BST or not in Python
A Binary Search Tree (BST) is a tree data structure where each node has at most two children, and for each node, all values in the left subtree are smaller and all values in the right subtree are larger. We can efficiently search for a value by comparing it with the current node and moving left or right accordingly.
To check if a value exists in a BST, we use the BST property to navigate efficiently. If the target value is smaller than the current node, we search the left subtree; if larger, we search the right subtree.
Algorithm Steps
Start at the root node
If the current node is null, return False (value not found)
If the current node's value equals the target value, return True
If the current node's value is greater than the target, search the left subtree
Otherwise, search the right subtree
Implementation
class TreeNode:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
class Solution:
def solve(self, root, val):
if not root:
return False
if root.data == val:
return True
if root.data > val:
return self.solve(root.left, val)
return self.solve(root.right, val)
# Create the BST
root = TreeNode(5)
root.left = TreeNode(1)
root.right = TreeNode(9)
root.right.left = TreeNode(7)
root.right.right = TreeNode(10)
root.right.left.left = TreeNode(6)
root.right.left.right = TreeNode(8)
# Search for value 7
ob = Solution()
result = ob.solve(root, 7)
print(f"Is 7 present in BST? {result}")
# Test with a value not in the tree
result2 = ob.solve(root, 4)
print(f"Is 4 present in BST? {result2}")
Is 7 present in BST? True Is 4 present in BST? False
How It Works
The algorithm follows the BST property to efficiently locate the target value:
Start at root (5): 7 > 5, so move to right child
Visit node (9): 7 < 9, so move to left child
Visit node (7): 7 == 7, found the target value!
Time and Space Complexity
Time Complexity: O(h) where h is the height of the tree. In the worst case (skewed tree), this is O(n). For a balanced BST, it's O(log n).
Space Complexity: O(h) due to the recursive call stack.
Iterative Approach
We can also solve this problem iteratively to avoid recursion overhead ?
class Solution:
def solve_iterative(self, root, val):
current = root
while current:
if current.data == val:
return True
elif current.data > val:
current = current.left
else:
current = current.right
return False
# Test the iterative approach
ob = Solution()
result = ob.solve_iterative(root, 7)
print(f"Iterative search for 7: {result}")
Iterative search for 7: True
Conclusion
Searching in a BST leverages the tree's ordered structure to efficiently locate values in O(log n) time for balanced trees. Both recursive and iterative approaches work effectively, with the iterative version using constant space.
