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 [https://en.wikipedia.org/wiki/Radix_tree]
2
+ A Radix Tree is a data structure that represents a space-optimized
3
+ trie (prefix tree) in whicheach node that is the only child is merged
4
+ with its parent [https://en.wikipedia.org/wiki/Radix_tree]
4
5
"""
5
6
6
7
@@ -60,7 +61,8 @@ def insert(self, word: str) -> None:
60
61
self .is_leaf = True
61
62
62
63
# Case 2: The node has no edges that have a prefix to the word
63
- # Solution: We create an edge from the current node to a new one containing the word
64
+ # Solution: We create an edge from the current node to a new one
65
+ # containing the word
64
66
elif word [0 ] not in self .nodes :
65
67
self .nodes [word [0 ]] = RadixNode (prefix = word , is_leaf = True )
66
68
@@ -76,7 +78,8 @@ def insert(self, word: str) -> None:
76
78
self .nodes [matching_string [0 ]].insert (remaining_word )
77
79
78
80
# Case 4: The word is greater equal to the matching
79
- # Solution: Create a node in between both nodes, change prefixes and add the new node for the remaining word
81
+ # Solution: Create a node in between both nodes, change
82
+ # prefixes and add the new node for the remaining word
80
83
else :
81
84
incoming_node .prefix = remaining_prefix
82
85
@@ -173,7 +176,7 @@ def print_tree(self, height=0) -> None:
173
176
"""Print the tree
174
177
175
178
Args:
176
- height (int, optional): Height of the node that is being printed. Defaults to 0.
179
+ height (int, optional): Height of the printed node
177
180
"""
178
181
if self .prefix != "" :
179
182
print ("-" * height , self .prefix , " (leaf)" if self .is_leaf else "" )
0 commit comments