1
1
"""
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]
4
4
"""
5
5
6
6
@@ -22,6 +22,9 @@ def match(self, word):
22
22
23
23
Returns:
24
24
(str, str, str): common substring, remaining prefix, remaining word
25
+
26
+ >>> RadixNode("myprefix").match("mystring")
27
+ ('my', 'prefix', 'string')
25
28
"""
26
29
x = 0
27
30
for q , w in zip (self .prefix , word ):
@@ -37,6 +40,8 @@ def insert_many(self, words: list[str]) -> None:
37
40
38
41
Args:
39
42
words (list[str]): list of words
43
+
44
+ >>> RadixNode("myprefix").insert_many(["mystring", "hello"])
40
45
"""
41
46
for word in words :
42
47
self .insert (word )
@@ -46,6 +51,8 @@ def insert(self, word: str) -> None:
46
51
47
52
Args:
48
53
word (str): word to insert
54
+
55
+ >>> RadixNode("myprefix").insert("mystring")
49
56
"""
50
57
# Case 1: If the word is the prefix of the node
51
58
# Solution: We set the current node as leaf
@@ -90,6 +97,9 @@ def find(self, word: str) -> bool:
90
97
91
98
Returns:
92
99
bool: True if the word appears on the tree
100
+
101
+ >>> RadixNode("myprefix").find("mystring")
102
+ False
93
103
"""
94
104
incoming_node = self .nodes .get (word [0 ], None )
95
105
if not incoming_node :
@@ -116,6 +126,9 @@ def delete(self, word: str) -> bool:
116
126
117
127
Returns:
118
128
bool: _description_
129
+
130
+ >>> RadixNode("myprefix").delete("mystring")
131
+ False
119
132
"""
120
133
incoming_node = self .nodes .get (word [0 ], None )
121
134
if not incoming_node :
@@ -203,6 +216,5 @@ def main() -> None:
203
216
root .print_tree ()
204
217
205
218
206
-
207
219
if __name__ == "__main__" :
208
220
main ()
0 commit comments