Skip to content

Commit bf274c5

Browse files
Enhance Trie class documentation with detailed examples for methods
1 parent 4de3186 commit bf274c5

File tree

1 file changed

+114
-1
lines changed

1 file changed

+114
-1
lines changed

data_structures/trie/trie.py

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,30 @@ def __init__(self):
4343

4444

4545
class Trie:
46-
"""Trie (Prefix Tree) data structure for efficient string storage and retrieval."""
46+
"""Trie (Prefix Tree) data structure for efficient string storage and retrieval.
47+
48+
Examples:
49+
>>> trie = Trie()
50+
>>> trie.insert("hello")
51+
>>> trie.search("hello")
52+
True
53+
>>> trie.search("hell")
54+
False
55+
>>> trie.starts_with("hel")
56+
True
57+
"""
4758

4859
def __init__(self):
4960
"""Initialize Trie with an empty root node.
5061
5162
The root node doesn't represent any character and serves as the entry point.
63+
64+
Examples:
65+
>>> trie = Trie()
66+
>>> trie.root is not None
67+
True
68+
>>> isinstance(trie.root, Node)
69+
True
5270
"""
5371
self.root = Node()
5472

@@ -67,6 +85,20 @@ def insert(self, word: str) -> None:
6785
- If character doesn't exist as a child, create a new node
6886
- Move to that child node
6987
3. Mark the final node as end of word
88+
89+
Examples:
90+
>>> trie = Trie()
91+
>>> trie.insert("cat")
92+
>>> trie.search("cat")
93+
True
94+
>>> trie.insert("car")
95+
>>> trie.search("car")
96+
True
97+
>>> trie.search("ca")
98+
False
99+
>>> trie.insert("ca")
100+
>>> trie.search("ca")
101+
True
70102
"""
71103
current_node = self.root
72104
# Traverse through each character in the word
@@ -97,6 +129,18 @@ def search(self, word: str) -> bool:
97129
- If character doesn't exist as a child, word doesn't exist
98130
- Move to that child node
99131
3. Return whether the final node is marked as end of word
132+
133+
Examples:
134+
>>> trie = Trie()
135+
>>> trie.insert("apple")
136+
>>> trie.search("apple")
137+
True
138+
>>> trie.search("app")
139+
False
140+
>>> trie.search("apples")
141+
False
142+
>>> trie.search("orange")
143+
False
100144
"""
101145
current_node = self.root
102146
# Traverse through each character in the word
@@ -123,6 +167,25 @@ def delete(self, word: str) -> None:
123167
2. Unmark the end-of-word flag
124168
3. Remove nodes with no children (cleanup unused nodes)
125169
4. Only removes nodes that don't form other words
170+
171+
Examples:
172+
>>> trie = Trie()
173+
>>> trie.insert("cat")
174+
>>> trie.insert("car")
175+
>>> trie.search("cat")
176+
True
177+
>>> trie.delete("cat")
178+
>>> trie.search("cat")
179+
False
180+
>>> trie.search("car")
181+
True
182+
>>> trie.insert("apple")
183+
>>> trie.insert("app")
184+
>>> trie.delete("app")
185+
>>> trie.search("app")
186+
False
187+
>>> trie.search("apple")
188+
True
126189
"""
127190

128191
def _delete(node: Node, word: str, index: int) -> bool:
@@ -186,6 +249,19 @@ def starts_with(self, prefix: str) -> bool:
186249
- If character doesn't exist as a child, prefix doesn't exist
187250
- Move to that child node
188251
3. If we successfully traverse all characters, prefix exists
252+
253+
Examples:
254+
>>> trie = Trie()
255+
>>> trie.insert("hello")
256+
>>> trie.insert("help")
257+
>>> trie.starts_with("hel")
258+
True
259+
>>> trie.starts_with("hello")
260+
True
261+
>>> trie.starts_with("hey")
262+
False
263+
>>> trie.starts_with("h")
264+
True
189265
"""
190266
current_node = self.root
191267
# Traverse through each character in the prefix
@@ -214,6 +290,19 @@ def longest_common_prefix(self) -> str:
214290
- Node is not marked as end of word (prevents prefixes)
215291
3. Stop when multiple paths exist or word ends
216292
4. Return the prefix built from traversed characters
293+
294+
Examples:
295+
>>> trie = Trie()
296+
>>> trie.insert("flower")
297+
>>> trie.insert("flow")
298+
>>> trie.insert("flight")
299+
>>> trie.longest_common_prefix()
300+
'fl'
301+
>>> trie2 = Trie()
302+
>>> trie2.insert("dog")
303+
>>> trie2.insert("cat")
304+
>>> trie2.longest_common_prefix()
305+
''
217306
"""
218307
prefix = []
219308
current_node = self.root
@@ -244,6 +333,13 @@ def print_all_words(self) -> None:
244333
3. For each node:
245334
- If marked as end of word, print the current word
246335
- Recursively visit all children
336+
337+
Examples:
338+
>>> trie = Trie()
339+
>>> trie.insert("a")
340+
>>> trie.insert("b")
341+
>>> trie.print_all_words()
342+
a b
247343
"""
248344

249345
def _print_words(node: Node, current_word: str) -> None:
@@ -281,6 +377,23 @@ def autocomplete(self, prefix: str, limit: int) -> list:
281377
2. If prefix doesn't exist, return empty list
282378
3. Use DFS from that node to find all words starting with prefix
283379
4. Stop once limit is reached
380+
381+
Examples:
382+
>>> trie = Trie()
383+
>>> trie.insert("apple")
384+
>>> trie.insert("app")
385+
>>> trie.insert("apply")
386+
>>> trie.insert("apricot")
387+
>>> results = trie.autocomplete("app", 3)
388+
>>> len(results)
389+
3
390+
>>> "apple" in results
391+
True
392+
>>> results2 = trie.autocomplete("ap", 5)
393+
>>> len(results2) >= 3
394+
True
395+
>>> trie.autocomplete("xyz", 5)
396+
[]
284397
"""
285398
results = []
286399
current_node = self.root

0 commit comments

Comments
 (0)