Extract from the given table a tree for word length, taking only
        prefixes of prefix length (if greater than 0) into account to compute
        successors.
        
        :param table: the table to extract the tree from;
        :param length: the length of words generated by the extracted tree;
                       greater or equal to 1;
        :param prefix: if greater than 0, the length of the prefixes used for
                       computing successors;
        :param flatten: whether to flatten the table or not;
        :return: the tree corresponding to words of length from table.

def from_table(cls, table, length, prefix=0, flatten=False):
        """
        Extract from the given table a tree for word length, taking only
        prefixes of prefix length (if greater than 0) into account to compute
        successors.
        
        :param table: the table to extract the tree from;
        :param length: the length of words generated by the extracted tree;
                       greater or equal to 1;
        :param prefix: if greater than 0, the length of the prefixes used for
                       computing successors;
        :param flatten: whether to flatten the table or not;
        :return: the tree corresponding to words of length from table.
        """
        # Build the expanded tree with necessary suffix and length
        tree = defaultdict(dict)  # The tree
        pending = {(">", 0)}  # The nodes to expand
        while pending:
            suffix, size = pending.pop()
            if size < length:
                choices = table.weighted_choices(suffix, exclude={"<"},
                                                 flatten=flatten)
                # The word length is not reached yet, expand
                for successor, weight in choices.items():
                    expanded = suffix + successor
                    if prefix > 0:
                        expanded = expanded[-prefix:]
                    new_node = (expanded, size + 1)
                    tree[(suffix, size)][new_node] = weight
                    pending.add(new_node)
            else:
                choices = table.weighted_choices(suffix, flatten=flatten)
                # The word length is reached, only add < if present
                if "<" in choices:
                    tree[(suffix, size)][("<", size + 1)] = 1
                else:
                    tree[(suffix, size)] = dict()
        return cls(cls.trim_tree(tree))