-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functionality and tests for the search function
- Loading branch information
1 parent
69ee212
commit 9e53a6c
Showing
4 changed files
with
169 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from collections import deque | ||
from datastructpy.node import Node | ||
|
||
def breadth_first_search(root, key): | ||
""" | ||
Performs a breadth-first search (BFS) on a binary search tree. | ||
Parameters: | ||
---------- | ||
root (Node): The root of the BST. | ||
key (int): The value to search for. | ||
Returns: | ||
---------- | ||
Node or None: The node containing the key, or None if not found. | ||
""" | ||
if not root: | ||
return None | ||
|
||
queue = deque([root]) | ||
|
||
while queue: | ||
node = queue.popleft() | ||
if node.key == key: | ||
return node # Found the key | ||
|
||
if node.left: | ||
queue.append(node.left) | ||
if node.right: | ||
queue.append(node.right) | ||
|
||
return None # Key not found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from datastructpy.node import Node | ||
|
||
def depth_first_search(root, key): | ||
""" | ||
Performs a depth-first search (DFS) on a binary search tree. | ||
Parameters: | ||
---------- | ||
root (Node): The root of the BST. | ||
key (int): The value to search for. | ||
Returns: | ||
---------- | ||
Node or None: The node containing the key, or None if not found. | ||
""" | ||
if not root: | ||
return None | ||
|
||
if root.key == key: | ||
return root # Found the key | ||
|
||
# Search left subtree | ||
left_result = depth_first_search(root.left, key) | ||
if left_result: | ||
return left_result | ||
|
||
# Search right subtree | ||
return depth_first_search(root.right, key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,77 @@ | ||
import pytest | ||
from datastructpy.non_linear.trees.binary_search_tree import BinarySearchTree | ||
|
||
@pytest.fixture | ||
def bst(): | ||
r""" | ||
Fixture to create a Binary Search Tree (BST) with predefined values. | ||
The tree structure: | ||
10 | ||
/ \ | ||
5 15 | ||
/ \ / \ | ||
3 7 13 18 | ||
""" | ||
elements = [10, 5, 15, 3, 7, 13, 18] | ||
return BinarySearchTree.list_to_tree(elements) | ||
|
||
@pytest.mark.parametrize("key, expected", [ | ||
(10, True), # Root node | ||
(5, True), # Left child of root | ||
(15, True), # Right child of root | ||
(3, True), # Left child of 5 | ||
(7, True), # Right child of 5 | ||
(13, True), # Left child of 15 | ||
(18, True), # Right child of 15 | ||
(20, False), # Not in tree | ||
(-1, False), # Not in tree | ||
]) | ||
def test_search_dfs(bst, key, expected): | ||
""" | ||
Tests depth-first search (DFS) in BST. | ||
Ensures DFS correctly identifies if a key exists in the tree. | ||
""" | ||
result = bst.search(key, algorithm='dfs') | ||
assert (result is not None) == expected | ||
|
||
@pytest.mark.parametrize("key, expected", [ | ||
(10, True), # Root node | ||
(5, True), # Left child of root | ||
(15, True), # Right child of root | ||
(3, True), # Left child of 5 | ||
(7, True), # Right child of 5 | ||
(13, True), # Left child of 15 | ||
(18, True), # Right child of 15 | ||
(20, False), # Not in tree | ||
(-1, False), # Not in tree | ||
]) | ||
def test_search_bfs(bst, key, expected): | ||
""" | ||
Tests breadth-first search (BFS) in BST. | ||
Ensures BFS correctly identifies if a key exists in the tree. | ||
""" | ||
result = bst.search(key, algorithm='bfs') | ||
assert (result is not None) == expected | ||
|
||
def test_search_default_algorithm(bst): | ||
""" | ||
Ensures that search() defaults to DFS when no algorithm is specified. | ||
""" | ||
assert bst.search(7) is not None # Should be found using DFS | ||
assert bst.search(20) is None # Should not be found | ||
|
||
def test_search_empty_tree(): | ||
""" | ||
Tests searching in an empty BST. | ||
The search should return None for any key. | ||
""" | ||
empty_bst = BinarySearchTree() | ||
assert empty_bst.search(10) is None # Should return None | ||
|
||
def test_search_invalid_algorithm(bst): | ||
""" | ||
Tests if search() handles invalid algorithm names properly. | ||
""" | ||
with pytest.raises(ValueError): | ||
bst.search(10, algorithm='invalid_algorithm') |