# 5 Best Ways to Check for a Value in a BST in Python

Rate this post

π‘ Problem Formulation: We often encounter situations where we need to verify the existence of a specific value within a binary search tree (BST). In Python, this check can be approached in several ways, emphasizing efficiency, readability, or brevity. Consider a BST where integers are stored, and we wish to check if a number, say 12, is present. Desired output is a boolean indicating presence (True) or absence (False).

## Method 1: Recursive Search

This method utilizes the properties of BSTs to recursively traverse down the tree, reducing the search space by half with each step. The method is defined as `search_recursive(node, target)` where ‘node’ is the current tree node and ‘target’ is the value to be searched. If ‘target’ is found within the tree, the method returns `True`, otherwise it returns `False`.

Here’s an example:

```class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

def search_recursive(node, target):
if node is None:
return False
elif target == node.val:
return True
elif target < node.val:
return search_recursive(node.left, target)
else:
return search_recursive(node.right, target)

# Example BST
root = TreeNode(8)
root.left = TreeNode(3)
root.right = TreeNode(10)
root.left.left = TreeNode(1)
root.left.right = TreeNode(6)
root.right.right = TreeNode(14)

# Search for value
print(search_recursive(root, 12))
```

Output:

`False`

This snippet defines a simple BST and uses a recursive function `search_recursive()` to search for the value 12. The function takes advantage of BST property where left child is smaller and right child is greater than the node value, significantly reducing the number of comparisons needed.

## Method 2: Iterative Search

The iterative search method also exploits the BST’s ordered nature, but unlike recursion, it uses a while-loop to iterate through nodes. Function signature is `search_iterative(node, target)`. This method avoids the overhead of recursive calls, which can make it more efficient for large trees.

Here’s an example:

```def search_iterative(node, target):
current = node
while current:
if target == current.val:
return True
elif target < current.val:
current = current.left
else:
current = current.right
return False

# Use the same BST as before

# Search for value
print(search_iterative(root, 12))
```

Output:

`False`

The example code defines `search_iterative()`, which iterates as long as there are nodes available. The BST maintained from the previous example is used, and we attempt to find the value 12, which returns `False` using an iterative procedure.

## Method 3: In-Order Traversal and Search

This method involves performing an in-order traversal over the BST to create a sorted list. Then, a binary search algorithm can be applied to check if the target value is present. This is more inefficient than the previous methods due to additional space used and the cost of traversal, but it is useful when multiple queries will be made against a static BST.

Here’s an example:

```def in_order_traversal(node, lst=[]):
if node:
in_order_traversal(node.left, lst)
lst.append(node.val)
in_order_traversal(node.right, lst)
return lst

def binary_search(lst, target):
left, right = 0, len(lst) - 1
while left <= right:
mid = (left + right) // 2
if lst[mid] == target:
return True
elif lst[mid] < target:
left = mid + 1
else:
right = mid - 1
return False

# Perform in-order traversal to get sorted elements
sorted_elements = in_order_traversal(root)

# Search for value using binary search
print(binary_search(sorted_elements, 12))
```

Output:

`False`

The code snippet defines two functions: `in_order_traversal()` for BST traversal and `binary_search()` for checking the presence of the value. Although the value 12 is not present, this method would excel in case of repeated searches on an unchanging BST.

## Method 4: Breadth-First Search (BFS)

Applying a BFS algorithm to search for a value in a BST doesn’t fully utilize the properties of a BST, but it works well for balanced trees. The function `search_bfs(node, target)` employs a queue to explore the nodes level by level. While more memory-intensive, BFS can be faster in shallow trees.

Here’s an example:

```from collections import deque

def search_bfs(node, target):
if not node:
return False
queue = deque([node])
while queue:
current = queue.popleft()
if current.val == target:
return True
if current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)
return False

# Use the same BST as before

# Search for value
print(search_bfs(root, 12))
```

Output:

`False`

The `search_bfs()` example demonstrates a breadth-first search on a BST. Although 12 is not found in the tree, the algorithm efficiently explores each tree level before moving onto the next.

## Bonus One-Liner Method 5: Pythonic Recursive Search

For fans of Python’s concise syntax, a one-liner recursive search can be implemented for elegance. Using the same function call `search_one_liner(node, target)`, the method performs a boolean operation within a return statement, short-circuiting as soon as the value is found or confirmed absent.

Here’s an example:

```search_one_liner = lambda node, target: node and (node.val == target or search_one_liner(node.left, target) if target < node.val else search_one_liner(node.right, target))

# Use the same BST as before

# Search for value
print(search_one_liner(root, 12))
```

Output:

`False`

The single line of code succinctly captures the essence of a recursive search with a ternary conditional. Although perhaps less readable, it is a testament to Python’s capabilities for writing compact code.

## Summary/Discussion

• Method 1: Recursive Search. Efficient for binary trees. Risks stack overflow with deep trees or large data sets.
• Method 2: Iterative Search. Memory-efficient for large datasets. Faster for large BSTs due to elimination of recursion overhead.
• Method 3: In-Order Traversal and Search. Offers sorted data which may be useful for subsequent queries. Generally less efficient due to traversal and memory costs.
• Method 4: Breadth-First Search. Effective for balanced trees. Typically more memory-intensive and doesn’t fully utilize BST properties.
• Bonus One-Liner Method 5: Pythonic Recursive Search. Concise and elegant. May compromise readability for those unfamiliar with Python’s shorthand syntax.