Skip to content

Commit

Permalink
Functionality change for search method
Browse files Browse the repository at this point in the history
  • Loading branch information
javiermtzo99 committed Jan 11, 2025
1 parent 237d85e commit 9e8175d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ datastructpy is a Python package designed to provide customizable and practical
**Methods**
- **`insert(root, key)`**:
- This method inserts a specified key into a Binary Search Tree (BST) by recursively finding the correct position while maintaining BST properties, creating a new root node if the tree is empty, inserting into the left subtree if the key is smaller, or the right subtree if larger.
- **`search(key)`**:
- This method checks whether a specified key exists in the Binary Search Tree (BST). It starts from the root and traverses the tree based on the key’s value, returning True if the key is found and False otherwise. This method is called directly on the BinarySearchTree instance, providing an easy way to verify the presence of a value in the tree.
- **`delete(root, key)`**:
- 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 @@ -55,6 +54,11 @@ bst.insert(2) # Insert into left subtree of 5
print(bst.root.right.left.key) # Output: 12 (right subtree of 10, left child of 15)
print(bst.root.left.left.key) # Output: 2 (left child of 5)

# Search for values in the BST
print("Searching for Values:")
print(bst.search(8)) # Output: True (8 exists in the tree)
print(bst.search(20)) # Output: False (20 does not exist in the tree)

# Delete a node
print("Deleting Nodes:")
bst.delete(5) # Delete the left child of the root
Expand All @@ -66,10 +70,6 @@ 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
65 changes: 33 additions & 32 deletions src/datastructpy/datastructpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,39 @@ def insert(self, key):
print(bst.root.left.key) # Output: 5
print(bst.root.right.key) # Output: 15
"""

def search(self, key):
"""
Checks if a value exists in the Binary Search Tree (BST).
This method traverses the BST to determine if a node with the specified key exists.
It starts from the root and:
- Returns True if a node with the given key is found.
- Returns False if the key is not present in the tree.
Parameters
----------
key : int
The value to search for in the tree.
Returns
-------
bool
- True if a node with the specified key exists in the tree.
- False if the key does not exist or the tree is empty.
Examples
--------
# Creating a Binary Search Tree
bst = BinarySearchTree()
bst.insert(10)
bst.insert(5)
bst.insert(15)
# Searching for values in the tree
print(bst.search(5)) # Output: True (5 exists in the tree)
print(bst.search(20)) # Output: False (20 does not exist in the tree)
"""

def delete(self, key):
"""Delete a value from the BST.
Expand Down Expand Up @@ -128,36 +161,4 @@ 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 9e8175d

Please sign in to comment.