Skip to content

Commit 976e385

Browse files
Ramy-Badr-Ahmedpre-commit-ci[bot]cclauss
authored
Implemented Suffix Tree Data Structure (TheAlgorithms#11554)
* Implemented KD-Tree Data Structure * Implemented KD-Tree Data Structure. updated DIRECTORY.md. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Create __init__.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Replaced legacy `np.random.rand` call with `np.random.Generator` in kd_tree/example_usage.py * Replaced legacy `np.random.rand` call with `np.random.Generator` in kd_tree/hypercube_points.py * added typehints and docstrings * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docstring for search() * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added tests. Updated docstrings/typehints * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated tests and used | for type annotations * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * E501 for build_kdtree.py, hypercube_points.py, nearest_neighbour_search.py * I001 for example_usage.py and test_kdtree.py * I001 for example_usage.py and test_kdtree.py * Update data_structures/kd_tree/build_kdtree.py Co-authored-by: Christian Clauss <[email protected]> * Update data_structures/kd_tree/example/hypercube_points.py Co-authored-by: Christian Clauss <[email protected]> * Update data_structures/kd_tree/example/hypercube_points.py Co-authored-by: Christian Clauss <[email protected]> * Added new test cases requested in Review. Refactored the test_build_kdtree() to include various checks. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Considered ruff errors * Considered ruff errors * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update kd_node.py * imported annotations from __future__ * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Implementation of the suffix tree data structure * Adding data to DIRECTORY.md * Minor file renaming * minor correction * renaming in DIRECTORY.md * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Considering ruff part-1 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Considering ruff part-2 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Considering ruff part-3 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Considering ruff part-4 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Considering ruff part-5 * Implemented Suffix Tree Data Structure. Added some comments to my files in TheAlgorithms#11532, TheAlgorithms#11554. * updating DIRECTORY.md * Implemented Suffix Tree Data Structure. Added some comments to my files in TheAlgorithms#11532, TheAlgorithms#11554. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: Ramy-Badr-Ahmed <[email protected]>
1 parent 9b5641d commit 976e385

14 files changed

+253
-0
lines changed

Diff for: DIRECTORY.md

+7
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@
291291
* [Stack With Doubly Linked List](data_structures/stacks/stack_with_doubly_linked_list.py)
292292
* [Stack With Singly Linked List](data_structures/stacks/stack_with_singly_linked_list.py)
293293
* [Stock Span Problem](data_structures/stacks/stock_span_problem.py)
294+
* Suffix Tree
295+
* Example
296+
* [Example Usage](data_structures/suffix_tree/example/example_usage.py)
297+
* [Suffix Tree](data_structures/suffix_tree/suffix_tree.py)
298+
* [Suffix Tree Node](data_structures/suffix_tree/suffix_tree_node.py)
299+
* Tests
300+
* [Test Suffix Tree](data_structures/suffix_tree/tests/test_suffix_tree.py)
294301
* Trie
295302
* [Radix Tree](data_structures/trie/radix_tree.py)
296303
* [Trie](data_structures/trie/trie.py)

Diff for: data_structures/kd_tree/build_kdtree.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
2+
# in Pull Request: #11532
3+
# https://github.com/TheAlgorithms/Python/pull/11532
4+
#
5+
# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
6+
# addressing bugs/corrections to this file.
7+
# Thank you!
8+
19
from data_structures.kd_tree.kd_node import KDNode
210

311

Diff for: data_structures/kd_tree/example/example_usage.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
2+
# in Pull Request: #11532
3+
# https://github.com/TheAlgorithms/Python/pull/11532
4+
#
5+
# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
6+
# addressing bugs/corrections to this file.
7+
# Thank you!
8+
19
import numpy as np
210

311
from data_structures.kd_tree.build_kdtree import build_kdtree

Diff for: data_structures/kd_tree/example/hypercube_points.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
2+
# in Pull Request: #11532
3+
# https://github.com/TheAlgorithms/Python/pull/11532
4+
#
5+
# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
6+
# addressing bugs/corrections to this file.
7+
# Thank you!
8+
19
import numpy as np
210

311

Diff for: data_structures/kd_tree/kd_node.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
2+
# in Pull Request: #11532
3+
# https://github.com/TheAlgorithms/Python/pull/11532
4+
#
5+
# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
6+
# addressing bugs/corrections to this file.
7+
# Thank you!
8+
19
from __future__ import annotations
210

311

Diff for: data_structures/kd_tree/nearest_neighbour_search.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
2+
# in Pull Request: #11532
3+
# https://github.com/TheAlgorithms/Python/pull/11532
4+
#
5+
# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
6+
# addressing bugs/corrections to this file.
7+
# Thank you!
8+
19
from data_structures.kd_tree.kd_node import KDNode
210

311

Diff for: data_structures/kd_tree/tests/test_kdtree.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
2+
# in Pull Request: #11532
3+
# https://github.com/TheAlgorithms/Python/pull/11532
4+
#
5+
# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
6+
# addressing bugs/corrections to this file.
7+
# Thank you!
8+
19
import numpy as np
210
import pytest
311

Diff for: data_structures/suffix_tree/__init__.py

Whitespace-only changes.

Diff for: data_structures/suffix_tree/example/__init__.py

Whitespace-only changes.

Diff for: data_structures/suffix_tree/example/example_usage.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
2+
# in Pull Request: #11554
3+
# https://github.com/TheAlgorithms/Python/pull/11554
4+
#
5+
# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
6+
# addressing bugs/corrections to this file.
7+
# Thank you!
8+
9+
from data_structures.suffix_tree.suffix_tree import SuffixTree
10+
11+
12+
def main() -> None:
13+
"""
14+
Demonstrate the usage of the SuffixTree class.
15+
16+
- Initializes a SuffixTree with a predefined text.
17+
- Defines a list of patterns to search for within the suffix tree.
18+
- Searches for each pattern in the suffix tree.
19+
20+
Patterns tested:
21+
- "ana" (found) --> True
22+
- "ban" (found) --> True
23+
- "na" (found) --> True
24+
- "xyz" (not found) --> False
25+
- "mon" (found) --> True
26+
"""
27+
text = "monkey banana"
28+
suffix_tree = SuffixTree(text)
29+
30+
patterns = ["ana", "ban", "na", "xyz", "mon"]
31+
for pattern in patterns:
32+
found = suffix_tree.search(pattern)
33+
print(f"Pattern '{pattern}' found: {found}")
34+
35+
36+
if __name__ == "__main__":
37+
main()

Diff for: data_structures/suffix_tree/suffix_tree.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
2+
# in Pull Request: #11554
3+
# https://github.com/TheAlgorithms/Python/pull/11554
4+
#
5+
# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
6+
# addressing bugs/corrections to this file.
7+
# Thank you!
8+
9+
from data_structures.suffix_tree.suffix_tree_node import SuffixTreeNode
10+
11+
12+
class SuffixTree:
13+
def __init__(self, text: str) -> None:
14+
"""
15+
Initializes the suffix tree with the given text.
16+
17+
Args:
18+
text (str): The text for which the suffix tree is to be built.
19+
"""
20+
self.text: str = text
21+
self.root: SuffixTreeNode = SuffixTreeNode()
22+
self.build_suffix_tree()
23+
24+
def build_suffix_tree(self) -> None:
25+
"""
26+
Builds the suffix tree for the given text by adding all suffixes.
27+
"""
28+
text = self.text
29+
n = len(text)
30+
for i in range(n):
31+
suffix = text[i:]
32+
self._add_suffix(suffix, i)
33+
34+
def _add_suffix(self, suffix: str, index: int) -> None:
35+
"""
36+
Adds a suffix to the suffix tree.
37+
38+
Args:
39+
suffix (str): The suffix to add.
40+
index (int): The starting index of the suffix in the original text.
41+
"""
42+
node = self.root
43+
for char in suffix:
44+
if char not in node.children:
45+
node.children[char] = SuffixTreeNode()
46+
node = node.children[char]
47+
node.is_end_of_string = True
48+
node.start = index
49+
node.end = index + len(suffix) - 1
50+
51+
def search(self, pattern: str) -> bool:
52+
"""
53+
Searches for a pattern in the suffix tree.
54+
55+
Args:
56+
pattern (str): The pattern to search for.
57+
58+
Returns:
59+
bool: True if the pattern is found, False otherwise.
60+
"""
61+
node = self.root
62+
for char in pattern:
63+
if char not in node.children:
64+
return False
65+
node = node.children[char]
66+
return True

Diff for: data_structures/suffix_tree/suffix_tree_node.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
2+
# in Pull Request: #11554
3+
# https://github.com/TheAlgorithms/Python/pull/11554
4+
#
5+
# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
6+
# addressing bugs/corrections to this file.
7+
# Thank you!
8+
9+
from __future__ import annotations
10+
11+
12+
class SuffixTreeNode:
13+
def __init__(
14+
self,
15+
children: dict[str, SuffixTreeNode] | None = None,
16+
is_end_of_string: bool = False,
17+
start: int | None = None,
18+
end: int | None = None,
19+
suffix_link: SuffixTreeNode | None = None,
20+
) -> None:
21+
"""
22+
Initializes a suffix tree node.
23+
24+
Parameters:
25+
children (dict[str, SuffixTreeNode] | None): The children of this node.
26+
is_end_of_string (bool): Indicates if this node represents
27+
the end of a string.
28+
start (int | None): The start index of the suffix in the text.
29+
end (int | None): The end index of the suffix in the text.
30+
suffix_link (SuffixTreeNode | None): Link to another suffix tree node.
31+
"""
32+
self.children = children or {}
33+
self.is_end_of_string = is_end_of_string
34+
self.start = start
35+
self.end = end
36+
self.suffix_link = suffix_link

Diff for: data_structures/suffix_tree/tests/__init__.py

Whitespace-only changes.
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
2+
# in Pull Request: #11554
3+
# https://github.com/TheAlgorithms/Python/pull/11554
4+
#
5+
# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
6+
# addressing bugs/corrections to this file.
7+
# Thank you!
8+
9+
import unittest
10+
11+
from data_structures.suffix_tree.suffix_tree import SuffixTree
12+
13+
14+
class TestSuffixTree(unittest.TestCase):
15+
def setUp(self) -> None:
16+
"""Set up the initial conditions for each test."""
17+
self.text = "banana"
18+
self.suffix_tree = SuffixTree(self.text)
19+
20+
def test_search_existing_patterns(self) -> None:
21+
"""Test searching for patterns that exist in the suffix tree."""
22+
patterns = ["ana", "ban", "na"]
23+
for pattern in patterns:
24+
with self.subTest(pattern=pattern):
25+
assert self.suffix_tree.search(
26+
pattern
27+
), f"Pattern '{pattern}' should be found."
28+
29+
def test_search_non_existing_patterns(self) -> None:
30+
"""Test searching for patterns that do not exist in the suffix tree."""
31+
patterns = ["xyz", "apple", "cat"]
32+
for pattern in patterns:
33+
with self.subTest(pattern=pattern):
34+
assert not self.suffix_tree.search(
35+
pattern
36+
), f"Pattern '{pattern}' should not be found."
37+
38+
def test_search_empty_pattern(self) -> None:
39+
"""Test searching for an empty pattern."""
40+
assert self.suffix_tree.search(""), "An empty pattern should be found."
41+
42+
def test_search_full_text(self) -> None:
43+
"""Test searching for the full text."""
44+
assert self.suffix_tree.search(
45+
self.text
46+
), "The full text should be found in the suffix tree."
47+
48+
def test_search_substrings(self) -> None:
49+
"""Test searching for substrings of the full text."""
50+
substrings = ["ban", "ana", "a", "na"]
51+
for substring in substrings:
52+
with self.subTest(substring=substring):
53+
assert self.suffix_tree.search(
54+
substring
55+
), f"Substring '{substring}' should be found."
56+
57+
58+
if __name__ == "__main__":
59+
unittest.main()

0 commit comments

Comments
 (0)