コード例 #1
0
ファイル: prefixtreenode_test.py プロジェクト: tempor1s/CS2.1
 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)
コード例 #2
0
class PrefixTree:
    """PrefixTree: A multi-way prefix tree that stores strings with efficient
    methods to insert a string into the tree, check if it contains a matching
    string, and retrieve all strings that start with a given prefix string.
    Time complexity of these methods depends only on the number of strings
    retrieved and their maximum length (size and height of subtree searched),
    but is independent of the number of strings stored in the prefix tree, as
    its height depends only on the length of the longest string stored in it.
    This makes a prefix tree effective for spell-checking and autocompletion.
    Each string is stored as a sequence of characters along a path from the
    tree's root node to a terminal node that marks the end of the string."""

    # Constant for the start character stored in the prefix tree's ROOT NODE

    START_CHARACTER = ''

    def __init__(self, strings=None):
        """Initialize this prefix tree and insert the given strings, if any."""
        # Create a new root node with the start character
        self.root = PrefixTreeNode(PrefixTree.START_CHARACTER)
        # Count the number of strings inserted into the tree
        self.size = 0
        # Insert each string, if any were given
        if strings is not None:
            for string in strings:
                self.insert(string)

    def __repr__(self):
        """Return a string representation of this prefix tree."""
        return f'PrefixTree({self.strings()!r})'

    def is_empty(self):
        """Return True if this prefix tree is empty (contains no strings)."""
        return self.root.num_children() == 0
        # return self.size == 0 #maybe?

    def contains(self, string):
        """Return True if this prefix tree contains the given string."""
        return self.root.has_child(string)

    def insert(self, string):
        """Insert the given string into this prefix tree."""
        self.root.add_child(string)

    def _find_node(self, string):
        """Return a tuple containing the node that terminates the given string
        in this prefix tree and the node's depth, or if the given string is not
        completely found, return None and the depth of the last matching node.
        Search is done iteratively with a loop starting from the root node."""
        # Match the empty string
        if len(string) == 0:
            return self.root, 0
        # Start with the root node
        node = self.root
        # TODO

    def complete(self, prefix):
        """Return a list of all strings stored in this prefix tree that start
        with the given prefix string."""
        # Create a list of completions in prefix tree
        completions = []
        # TODO

    def strings(self):
        """Return a list of all strings stored in this prefix tree."""
        # Create a list of all strings in prefix tree
        all_strings = []
        # TODO

    def _traverse(self, node, prefix, visit):
        """Traverse this prefix tree with recursive depth-first traversal.