调整堆的算法

Begin
adjustHeap(A[], i) // Heapify takes in our heap and index of current root node of subtree to be heapified
{
left=leftChild();
right=rightChild();
largest=i;
if left<=A.heap-size AND A[left]>A[i] {
largest=left;
}
if right<=A.heap-size AND A[right]>A[i] {
largest=right;
}

if largest NOT EQUAL i {
swap A[i] and A[largest]
adjustHeap[A, largest)
}
END

 

你可能感兴趣的:(算法)