【发布时间】:2017-08-24 02:58:18
【问题描述】:
测试用例:
8,7,5,2,3,6,9(不是最小堆)(这是 buildHeap 函数的元素 A*)
2,3,5,7,8,6,9(调用构建堆后的最小堆)
3,5,6,7,8,9(在调用 deleteMin 之后)这是不正确的
应该是这个3,7,5,9,8,6
我似乎无法找到 deleteMin 的问题,我知道我的 heapify 正在工作,但我可能没有看到任何东西。
Element Heap::deleteMin(Heap& heap){
Element deleted = heap.H[0];
heap.H[0] = heap.H[heap.size-1];
heap.size--;
cout<<deleted.getKey()<<" has been deleted from heap"<<endl;
for(int i=heap.capacity/2-1;i>=0;--i)
heapify(heap,i);
return deleted;
}
void Heap::heapify(Heap& heap,int index){
int smallest = 0;
int left = 2*index;
int right = 2*index+1;
if(left < heap.size && heap.H[left].getKey() < heap.H[index].getKey())
smallest=left;
else
smallest=index;
if(right < heap.size && heap.H[right].getKey() < heap.H[smallest].getKey())
smallest=right;
if(smallest != index){
int swapKey = heap.H[index].getKey();
heap.H[index].setKey(heap.H[smallest].getKey());
heap.H[smallest].setKey(swapKey);
heapify(heap,smallest);
}
}
void Heap::buildHeap(Heap& heap, Element* A){
for(int j=0;j<heap.capacity;++j){
heap.insert(heap,A[j]);
for(int i=heap.capacity/2-1;i>=0;--i)
heapify(heap,i);
}
}
【问题讨论】:
-
您是否尝试使用调试器单步执行您的代码?
-
我们不知道“heapify”应该做什么,所以除了“heapify 正在做它正在做的事情”之外我们不能说什么。但是,我会注意到您的语句“if(smallest != index)”,其中您将数组中的值与数组的索引进行比较,这看起来肯定不正确 - 例如,如果您将 100 添加到所有数组中的值(如 108,107、105 等),那么对于 7 个元素的数组,该条件将始终返回 false - 它看起来不正确。
-
@racraman:
heapify是实现堆时的常见操作。它在堆中筛选一个项目。它通常也称为siftDown。如果您更仔细地检查heapify,您会发现smallest是数组的索引,而不是数组中的值。
标签: c++ heap heapsort binary-heap