Skip to content

Commit

Permalink
Merge branch 'feature/suffix-tree-implementation'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramy-Badr-Ahmed committed Sep 7, 2024
2 parents 283a9b7 + 7479d2d commit 819f3d1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
26 changes: 14 additions & 12 deletions data_structures/suffix_tree/suffix_tree_node.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
from __future__ import annotations
from typing import Dict, Optional


class SuffixTreeNode:
def __init__(self,
children: Dict[str, 'SuffixTreeNode'] = None,
is_end_of_string: bool = False,
start: int | None = None,
end: int | None = None,
suffix_link: SuffixTreeNode | None = None) -> None:
def __init__(
self,
children: dict[str, SuffixTreeNode] | None = None,
is_end_of_string: bool = False,
start: int | None = None,
end: int | None = None,
suffix_link: SuffixTreeNode | None = None,
) -> None:
"""
Initializes a suffix tree node.
Parameters:
children (Dict[str, SuffixTreeNode], optional): The children of this node. Defaults to an empty dictionary.
is_end_of_string (bool, optional): Indicates if this node represents the end of a string. Defaults to False.
start (int | None, optional): The start index of the suffix in the text. Defaults to None.
end (int | None, optional): The end index of the suffix in the text. Defaults to None.
suffix_link (SuffixTreeNode | None, optional): Link to another suffix tree node. Defaults to None.
children (dict[str, SuffixTreeNode] | None): The children of this node.
is_end_of_string (bool): Indicates if this node represents
the end of a string.
start (int | None): The start index of the suffix in the text.
end (int | None): The end index of the suffix in the text.
suffix_link (SuffixTreeNode | None): Link to another suffix tree node.
"""
self.children = children or {}
self.is_end_of_string = is_end_of_string
Expand Down
35 changes: 22 additions & 13 deletions data_structures/suffix_tree/tests/test_suffix_tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest

from data_structures.suffix_tree.suffix_tree import SuffixTree


Expand All @@ -8,34 +9,42 @@ def setUp(self) -> None:
self.text = "banana"
self.suffix_tree = SuffixTree(self.text)

def test_search_existing_patterns(self):
def test_search_existing_patterns(self) -> None:
"""Test searching for patterns that exist in the suffix tree."""
patterns = ["ana", "ban", "na"]
for pattern in patterns:
with self.subTest(pattern = pattern):
self.assertTrue(self.suffix_tree.search(pattern), f"Pattern '{pattern}' should be found.")
with self.subTest(pattern=pattern):
assert self.suffix_tree.search(
pattern
), f"Pattern '{pattern}' should be found."

def test_search_non_existing_patterns(self):
def test_search_non_existing_patterns(self) -> None:
"""Test searching for patterns that do not exist in the suffix tree."""
patterns = ["xyz", "apple", "cat"]
for pattern in patterns:
with self.subTest(pattern = pattern):
self.assertFalse(self.suffix_tree.search(pattern), f"Pattern '{pattern}' should not be found.")
with self.subTest(pattern=pattern):
assert not self.suffix_tree.search(
pattern
), f"Pattern '{pattern}' should not be found."

def test_search_empty_pattern(self):
def test_search_empty_pattern(self) -> None:
"""Test searching for an empty pattern."""
self.assertTrue(self.suffix_tree.search(""), "An empty pattern should be found.")
assert self.suffix_tree.search(""), "An empty pattern should be found."

def test_search_full_text(self):
def test_search_full_text(self) -> None:
"""Test searching for the full text."""
self.assertTrue(self.suffix_tree.search(self.text), "The full text should be found in the suffix tree.")
assert self.suffix_tree.search(
self.text
), "The full text should be found in the suffix tree."

def test_search_substrings(self):
def test_search_substrings(self) -> None:
"""Test searching for substrings of the full text."""
substrings = ["ban", "ana", "a", "na"]
for substring in substrings:
with self.subTest(substring = substring):
self.assertTrue(self.suffix_tree.search(substring), f"Substring '{substring}' should be found.")
with self.subTest(substring=substring):
assert self.suffix_tree.search(
substring
), f"Substring '{substring}' should be found."


if __name__ == "__main__":
Expand Down

0 comments on commit 819f3d1

Please sign in to comment.