resq_dsa.trie
Trie data structure and string matching algorithms. This module provides a prefix tree (Trie) implementation for efficient string storage and retrieval, along with the Rabin-Karp string matching algorithm for pattern search. Classes: Trie: Prefix tree for word storage and prefix-based retrieval. Functions: rabin_karp: Find all occurrences of a pattern in text using rolling hash.Trie Objects
_root- The root node of the Trie.
trie = Trie() trie.insert(“hello”) trie.insert(“help”) trie.search(“hello”) True trie.starts_with(“hel”) [‘hello’, ‘help’]
Trie.__init__
Trie.insert
word- The word to insert.
trie = Trie() trie.insert(“python”)
Trie.search
word- The word to search for.
trie = Trie() trie.insert(“test”) trie.search(“test”) True trie.search(“tes”) False
Trie.starts_with
prefix- The prefix to search for.
trie = Trie() trie.insert(“hello”) trie.insert(“help”) trie.insert(“hero”) trie.starts_with(“he”) [‘hello’, ‘help’, ‘hero’]
rabin_karp
text- The text to search in.pattern- The pattern to search for.
rabin_karp(“ABABDABACDABABCABAB”, “ABABCABAB”) [10] rabin_karp(“hello world”, “wor”) [6] rabin_karp(“test”, “xyz”) []