2021年4月10日星期六

Modifiying fixDown for a minheap

I'm learning how binary heap works, and came across this webpage which very clearly explains how to implement a maxheap. It contains this fixDown operation as well.

void fixDown(Item heap[], int heapsize, int k) {      while (2 * k <= heapsize) {          int j = 2 * k; // index of left child          if (j < heapsize && heap[j] < heap[j + 1]) j++;          if (heap[k] >= heap[j]) break;          swap(heap[k], heap[j]);          k = j;      }  }  

My question is how would I modify this fixDown for a minheap. My initial understanding was that I would change line 4 to heap[j] > heap[j + 1], but I'm trying to understand whether this is right or would I have to change line 5 operators to <= as well?

https://stackoverflow.com/questions/67041364/modifiying-fixdown-for-a-minheap April 11, 2021 at 12:06PM

没有评论:

发表评论