Skip to content

Commit

Permalink
Merge pull request #12 from UBC-MDS/bst-search
Browse files Browse the repository at this point in the history
Bst search
  • Loading branch information
javiermtzo99 authored Jan 11, 2025
2 parents 40e34c5 + f2d4a61 commit 012a9cd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ datastructpy is a Python package designed to provide customizable and practical
- This method deletes a specified key from a Binary Search Tree (BST). If the key is found, it removes the node while maintaining BST properties. For nodes with two children, it replaces the node with its in-order successor (smallest value in the right subtree) and then deletes the successor node. If the node has one child or no children, it is removed, and its child (if any) takes its place.
- **`list_to_tree(elements)`**:
- This static method constructs a Binary Search Tree (BST) from a given list of elements. It sequentially inserts each element into the BST, ensuring the tree maintains its BST properties. The method is accessed directly via the BinarySearchTree class and returns a BinarySearchTree object with the provided elements organized in a valid BST structure.
- **`searchBST(root, value)`**:
- This function searches for a specified value within a Binary Search Tree (BST). It traverses the tree recursively, comparing the value with the root and navigating left or right based on the comparison.
- **Returns**: `True` if the value is found, `False` otherwise.

## datastructpy in Python Ecosystem
datastructpy complements Python’s standard library by providing customizable implementations of essential data structures for learning and interview preparation. While modules like collections (e.g., deque) and heapq focus on optimized, ready-to-use structures, datastructpy emphasizes clarity and adaptability, making it ideal for understanding core concepts. Unlike specialized libraries like [pyrsistent](https://pypi.org/project/pyrsistent/) or [sortedcontainers](https://pypi.org/project/sortedcontainers/), datastructpy bridges the gap between practical functionality and educational needs, carving out a unique space in the Python ecosystem.
Expand Down Expand Up @@ -63,6 +66,10 @@ else:
bst.delete(10) # Delete the root
print(bst.root.key) # Output: 15 (new root after deletion)

# Search for a value
result = searchBST(root, 15) # Returns True
print(result) # Output: True

# Final structure of the tree
print("Final Tree Structure:")
print(bst.root.key) # Output: 15
Expand Down
33 changes: 33 additions & 0 deletions src/datastructpy/datastructpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,36 @@ def list_to_tree(elements):
print(bst.root.left.key) # Output: 5 (left child of root)
print(bst.root.right.key) # Output: 15 (right child of root)
"""
@staticmethod
def searchBST(root, value):
"""
Search for a value in the Binary Search Tree (BST).
This function recursively searches the tree to find the node containing the specified value.
It compares the value to the root node's value and traverses left or right based on the comparison.
If the value is found, the function returns True; otherwise, it returns False.
Parameters
----------
root : BinarySearchTree
The root of the tree or subtree to search. Can be None if the tree is empty.
value : int
The value to search for in the tree.
Returns
-------
bool
- True if the value is found in the tree.
- False if the value is not found or the tree is empty.
Examples
--------
# Creating a Binary Search Tree
bst = BinarySearchTree(10)
bst.insert(5)
bst.insert(15)
# Searching for values in the tree
print(searchBST(bst, 5)) # Output: True
print(searchBST(bst, 20)) # Output: False
"""

0 comments on commit 012a9cd

Please sign in to comment.