(tree, k)
return [n for _, n in heap]
Return `k` topmost clusters from hierarchical clustering.

    :param Tree root: Root cluster.
    :param int k: Number of top clusters.

    :rtype: list of :class:`Tree` instances
Return `k` topmost clusters from hierarchical clustering.
Return k topmost clusters from hierarchical clustering .
def top_clusters(tree, k):
    """
    Return `k` topmost clusters from hierarchical clustering.

    :param Tree root: Root cluster.
    :param int k: Number of top clusters.

    :rtype: list of :class:`Tree` instances
    """
    def item(node):
        return ((node.is_leaf, -node.value.height), node)

    heap = [item(tree)]

    while len(heap) < k:
        _, cl = heap[0]  # peek
        if cl.is_leaf:
            assert all(n.is_leaf for _, n in heap)
            break
        key, cl = heapq.heappop(heap)
        left, right = cl.left, cl.right
        heapq.heappush(heap, item(left))
        heapq.heappush(heap, item(right))

    return [n for _, n in heap]
def top_clusters ( tree , k ) : def item ( node ) : return ( ( node . is_leaf , - node . value . height ) , node ) heap = [ item ( tree ) ] while len ( heap ) < k : _ , cl = heap [ 0 ] # peek if cl . is_leaf : assert all ( n . is_leaf for _ , n in heap ) break key , cl = heapq . heappop ( heap ) left , right = cl . left , cl . right heapq . heappush ( heap , item ( left ) ) heapq . heappush ( heap , item ( right ) ) return [ n for _ , n in heap ]