Skip to content

Commit b691fa3

Browse files
Alex de la CruzAlex de la Cruz
Alex de la Cruz
authored and
Alex de la Cruz
committed
added doctests
1 parent c6aa63b commit b691fa3

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

data_structures/trie/radix_tree.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
A Radix Tree is a data structure that represents a space-optimized trie (prefix tree) in which
3-
each node that is the only child is merged with its parent
2+
A Radix Tree is a data structure that represents a space-optimized trie (prefix tree) in which
3+
each node that is the only child is merged with its parent [https://en.wikipedia.org/wiki/Radix_tree]
44
"""
55

66

@@ -22,6 +22,9 @@ def match(self, word):
2222
2323
Returns:
2424
(str, str, str): common substring, remaining prefix, remaining word
25+
26+
>>> RadixNode("myprefix").match("mystring")
27+
('my', 'prefix', 'string')
2528
"""
2629
x = 0
2730
for q, w in zip(self.prefix, word):
@@ -37,6 +40,8 @@ def insert_many(self, words: list[str]) -> None:
3740
3841
Args:
3942
words (list[str]): list of words
43+
44+
>>> RadixNode("myprefix").insert_many(["mystring", "hello"])
4045
"""
4146
for word in words:
4247
self.insert(word)
@@ -46,6 +51,8 @@ def insert(self, word: str) -> None:
4651
4752
Args:
4853
word (str): word to insert
54+
55+
>>> RadixNode("myprefix").insert("mystring")
4956
"""
5057
# Case 1: If the word is the prefix of the node
5158
# Solution: We set the current node as leaf
@@ -90,6 +97,9 @@ def find(self, word: str) -> bool:
9097
9198
Returns:
9299
bool: True if the word appears on the tree
100+
101+
>>> RadixNode("myprefix").find("mystring")
102+
False
93103
"""
94104
incoming_node = self.nodes.get(word[0], None)
95105
if not incoming_node:
@@ -116,6 +126,9 @@ def delete(self, word: str) -> bool:
116126
117127
Returns:
118128
bool: _description_
129+
130+
>>> RadixNode("myprefix").delete("mystring")
131+
False
119132
"""
120133
incoming_node = self.nodes.get(word[0], None)
121134
if not incoming_node:
@@ -203,6 +216,5 @@ def main() -> None:
203216
root.print_tree()
204217

205218

206-
207219
if __name__ == "__main__":
208220
main()

0 commit comments

Comments
 (0)