def extract_tree(node, ki):
    """Extracts the ki best-tree.
    Args:
        node (KTreeNode): the top node of the tree.
        ki (int): the 0-based index of the tree requested. 0 <= ki < k
    Returns:
        (KNodeTree, Fraction): the ki-best tree and the Pm associated with it
    """
    def inner(node, ki):
        """Extracts the ki best-tree. This function only returns the tree."""
        row = node.Bs[ki]
        new_node = node.clone_without_children()
        if all(elem == 0 for elem in row):
            return new_node
        else:
            new_children = list(inner(c, int(r) - 1)
                                for c, r in zip(node.children, row))
            new_node.children = new_children
            return new_node
    ki_tree = inner(node, ki)
    pm = ki_tree.pms[ki]
    return (ki_tree, pm)