def tree(node, string=''):
    """
    Recursive function that goes through all the nodes in a graph and creates the Huffman code for each one.
    :param node:
    :param string:
    :return: d: dictionary where each character is given a Huffman code
    """
    if type(node) is str:
        return {node: string}

    left, right = node.children()

    d = dict()
    d.update(tree(left, string + '0'))
    d.update(tree(right, string + '1'))
    return d