donnemartin / interactive-coding-challenges

120+ interactive Python coding interview challenges (algorithms and data structures). Includes Anki flashcards.
Other
29.44k stars 4.45k forks source link

Bug in trie solution? #238

Closed codescv closed 6 years ago

codescv commented 6 years ago

in the delete method of graphs_trees/trie/trie_solution.ipynb:

while parent is not None:

As we are propagating the delete up the

        # parents, if this node has children, stop
        # here to prevent orphaning its children.
        # Or
        # if this node is a terminating node that is
        # not the terminating node of the input word, 
        # stop to prevent removing the associated word.
        if node.children or node.terminates:
            return
        del parent.children[node.key]
        node = parent
        parent = parent.parent

When you traverse from child to parent, the parent will always at least contain one child. Thus the parent.children[node.key] will never be deleted.