【0】README
0.1) google 搜索出来的答案,真的很让我伤心, 全是大粪啊, 你们进行了多少次的比较, 应该是6次吧,我也是醉完了, 写个非大粪的version (我这里的比较只有两次);
【2】源代码如下(注意数据结构):
// get the index storing the maximum among elements under left, parent and right
int maxIndex(int left, int parent, int right, BinaryHeap bh)
{ int maxIndex; maxIndex = left;if(bh->elements[parent] > bh->elements[maxIndex]) maxIndex = parent; else if(bh->elements[right] > bh->elements[maxIndex])maxIndex = right;return maxIndex;
}// get the index storing the minimum among elements under left, parent and right
int minIndex(int left, int parent, int right, BinaryHeap bh)
{ int minIndex; minIndex = left;if(bh->elements[parent] < bh->elements[minIndex]) minIndex = parent; else if(bh->elements[right] < bh->elements[minIndex])minIndex = right;return minIndex;
}