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 <cclauss@me.com> * Update data_structures/kd_tree/example/hypercube_points.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/kd_tree/example/hypercube_points.py Co-authored-by: Christian Clauss <cclauss@me.com> * 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 <cclauss@me.com> Co-authored-by: Ramy-Badr-Ahmed <Ramy-Badr-Ahmed@users.noreply.github.com>
1 parent 9b5641d commit 976e385

File tree

14 files changed

+253
-0
lines changed

14 files changed

+253
-0
lines changed

DIRECTORY.md

Lines changed: 7 additions & 0 deletions
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)

data_structures/kd_tree/build_kdtree.py

Lines changed: 8 additions & 0 deletions
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

data_structures/kd_tree/example/example_usage.py

Lines changed: 8 additions & 0 deletions
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

data_structures/kd_tree/example/hypercube_points.py

Lines changed: 8 additions & 0 deletions
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

data_structures/kd_tree/kd_node.py

Lines changed: 8 additions & 0 deletions
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

data_structures/kd_tree/nearest_neighbour_search.py

Lines changed: 8 additions & 0 deletions
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

data_structures/kd_tree/tests/test_kdtree.py

Lines changed: 8 additions & 0 deletions
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

data_structures/suffix_tree/__init__.py

Whitespace-only changes.

data_structures/suffix_tree/example/__init__.py

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
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()

0 commit comments

Comments
 (0)