Ejemplo n.º 1
0
 def test_child_methods(self):
     # Create node 'A' and verify it does not have any children
     node_A = PrefixTreeNode('A')
     assert node_A.num_children() == 0
     assert node_A.has_child('B') is False
     # Verify getting child from node 'A' raises error
     with self.assertRaises(ValueError):
         node_A.get_child('B')
     # Create node 'B' and add it as child to node 'A'
     node_B = PrefixTreeNode('B')
     node_A.add_child('B', node_B)
     # Verify node 'A' has node 'B' as child
     assert node_A.num_children() == 1
     assert node_A.has_child('B') is True
     assert node_A.get_child('B') is node_B
     # Verify adding node 'B' as child to node 'A' again raises error
     with self.assertRaises(ValueError):
         node_A.add_child('B', node_B)
     # Create node 'C' and add it as another child to node 'A'
     node_C = PrefixTreeNode('C')
     node_A.add_child('C', node_C)
     # Verify node 'A' has both nodes 'B' and 'C' as children
     assert node_A.num_children() == 2
     assert node_A.has_child('B') is True
     assert node_A.has_child('C') is True
     assert node_A.get_child('C') is node_C
     # Verify adding node 'C' as child to node 'A' again raises error
     with self.assertRaises(ValueError):
         node_A.add_child('C', node_C)
 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)