def _traverse(self, node: PrefixTreeNode, prefix: str, visit: Callable) -> None: """Traverse this prefix tree with recursive depth-first traversal. Start at the given node with the given prefix representing its path in this prefix tree and visit each node with the given visit function.""" # Once the node is filled with characters and contains a terminal node, it'll append if node.is_terminal(): visit(prefix) for char in node.children.keys(): next_node = node.get_child(char) self._traverse(next_node, prefix + char, visit)
def test_init_and_properties(self): character = 'a' node = PrefixTreeNode(character) # Verify node character assert isinstance(node.character, str) print(node.character) assert node.character == 'a' # Verify children nodes structure assert isinstance(node.children, PrefixTreeNode.CHILDREN_TYPE) assert len(node.children) == 26 assert node.children == [None] * 26 assert node.num_children() == 0 # Verify terminal boolean assert node.terminal is False assert node.is_terminal() is False