Recursive functions on trees

Last time we talked about the HTMLTree type we’ll be using to represent trees. Today, we’ll discuss how to write functions that process tree-structured data.

We developed the following functions, along with examples; see lecture capture for details.

def count_tag(doc: Tag, tag: str) -> int:
    subtotal = sum([count_tag(subdoc, tag) for subdoc in doc.children])
    if doc.tag == tag:
        return subtotal + 1
    return subtotal

def all_text(doc: Tag) -> List[str]:
    if doc.tag == "text":
        return [doc.text]
    return [text for subdoc in doc.children for text in all_text(subdoc)]