我目前正在一个必须创建最大堆的项目中。我目前正在使用堆的教科书版本,看起来像:
public MaxHeap(int initialCapacity) {
if (initialCapacity < DEFAULT_CAPACITY)
initialCapacity = DEFAULT_CAPACITY;
else
checkCapacity(initialCapacity);
@SuppressWarnings("unchecked")
T[] tempHeap = (T[]) new Comparable[initialCapacity + 1];
heap = tempHeap;
lastIndex = 0;
initialized = true;
}
public T getMax() {
checkInitialization();
T root = null;
if (!isEmpty())
root = heap[1];
return root;
}
public boolean isEmpty() {
return lastIndex < 1;
}
public int getSize() {
return lastIndex;
}
public void clear() {
checkInitialization();
while (lastIndex > -1) {
heap[lastIndex] = null;
lastIndex--;
}
lastIndex = 0;
}
public void add(T newEntry) {
checkInitialization();
int newIndex = lastIndex + 1;
int parentIndex = newIndex / 2;
while ((parentIndex > 0) && newEntry.compareTo(heap[parentIndex]) > 0) {
heap[newIndex] = heap[parentIndex];
newIndex = parentIndex;
parentIndex = newIndex / 2;
}
heap[newIndex] = newEntry;
lastIndex++;
ensureCapacity();
}
public int getSwaps()
{
return swaps;
}
public T removeMax() {
checkInitialization();
T root = null;
if (!isEmpty()) {
root = heap[1];
heap[1] = heap[lastIndex];
lastIndex--;
reheap(1);
}
return root;
}
private void reheap(int rootIndex) {
boolean done = false;
T orphan = heap[rootIndex];
int leftChildIndex = 2 * rootIndex;
while (!done && (leftChildIndex <= lastIndex)) {
int largerChildIndex = leftChildIndex;
int rightChildIndex = leftChildIndex + 1;
if ((rightChildIndex <= lastIndex) && heap[rightChildIndex].compareTo(heap[largerChildIndex]) > 0) {
largerChildIndex = rightChildIndex;
}
if (orphan.compareTo(heap[largerChildIndex]) < 0) {
heap[rootIndex] = heap[largerChildIndex];
rootIndex = largerChildIndex;
leftChildIndex = 2 * rootIndex;
} else
done = true;
}
heap[rootIndex] = orphan;
}
我应该在多个地方计算掉期并打印总金额吗?如果可以,我应该在哪里计算?我以前曾尝试在add方法中枚举swaps ++,但我认为这不是正确的方法。
答案 0 :(得分:1)
您必须同时计算add(T newEntry)
方法中调用的reHeap
方法和removeMax
方法中的交换。
在reHeap
中,从顶部开始,从removeMax调用它时,在移除max(在Max Heap情况下)之后,将根替换为最后一个元素,然后需要平衡堆。因此,堆递归地下降到最后一层以达到平衡,这可能需要交换。
编辑:
在reHeap
的以下代码块内添加交换:
if (orphan.compareTo(heap[largerChildIndex]) < 0) {
heap[rootIndex] = heap[largerChildIndex];
rootIndex = largerChildIndex;
leftChildIndex = 2 * rootIndex;
// increment the swap here as inside this block of reHeap only swap takes place.
swap++
}
答案 1 :(得分:1)
这些是实现它的正确方法吗?
所以添加方法将是:
public void add(T newEntry) {
checkInitialization();
int newIndex = lastIndex + 1;
int parentIndex = newIndex / 2;
while ((parentIndex > 0) && newEntry.compareTo(heap[parentIndex]) > 0) {
heap[newIndex] = heap[parentIndex];
newIndex = parentIndex;
parentIndex = newIndex / 2;
swap++;
}
heap[newIndex] = newEntry;
lastIndex++;
ensureCapacity();
}
,重新组合将是:
private void reheap(int rootIndex) {
boolean done = false;
T orphan = heap[rootIndex];
int leftChildIndex = 2 * rootIndex;
while (!done && (leftChildIndex <= lastIndex)) {
int largerChildIndex = leftChildIndex;
int rightChildIndex = leftChildIndex + 1;
if ((rightChildIndex <= lastIndex) && heap[rightChildIndex].compareTo(heap[largerChildIndex]) > 0) {
largerChildIndex = rightChildIndex;
}
if (orphan.compareTo(heap[largerChildIndex]) < 0) {
heap[rootIndex] = heap[largerChildIndex];
rootIndex = largerChildIndex;
leftChildIndex = 2 * rootIndex;
swap++;
} else
done = true;
}
heap[rootIndex] = orphan;
}