Containment Tests

Suppose I’m interested in finding all sentences with a definite determiner in a subject. This means checking whether a particular subtree (corresponding to the subject) contains a particular element.

Let’s figure out how to compute whether a tree contains a particular piece of data, and then we’ll get back to figuring out how to grab the relevant subtree.

Containment tests for a particular class (viewed as a sort of container) are often implemented in the __contains__ magic method, which defines the behavior of in. The idea is that __contains__ should take a piece of data and tell us whether it matches a piece of data somewhere in the tree. So basically, we want the analogue of asking whether a list contains some element using in.

For a list, containment could be naturally implemented using a for-loop. So suppose we’re redefining the list class, __contains__ could be implemented something like this:

def __contains__(self, data):
    for d in self:
        if d == data:
            return True
    
    return False

The way we’ll implement __contains__ for Tree is analogous, but we have to take into account that the tree has a more interesting structure to it. We’ll do this by implementing two kinds of tree search algorithms:

In both kinds of search, we start at the top of the tree and work our way down, the question is which nodes we look at.