В вашей текущей реализации похоже, что у вас нет базового сценария для остановки рекурсии.
Помните, что вам нужен базовый случай в рекурсивной функции (в данном случае, ваша MaxHeapify
функция), и она не выглядит таковой.
Вот пример MaxHeap , который может быть изобретательным, чтобы посмотреть
// A recursive function to max heapify the given
// subtree. This function assumes that the left and
// right subtrees are already heapified, we only need
// to fix the root.
private void maxHeapify(int pos)
{
if (isLeaf(pos))
return;
if (Heap[pos] < Heap[leftChild(pos)] ||
Heap[pos] < Heap[rightChild(pos)]) {
if (Heap[leftChild(pos)] > Heap[rightChild(pos)]) {
swap(pos, leftChild(pos));
maxHeapify(leftChild(pos));
}
else {
swap(pos, rightChild(pos));
maxHeapify(rightChild(pos));
}
}
}
Здесь вы можете увидеть нижний регистр:
if (isLeaf(pos))
return;
Вам необходимо добавить базовый регистр в рекурсивную функцию.