[英]Heapify method is not working properly for max heap
I am preparing for a data structures and algorithms final exam.我正在准备数据结构和算法期末考试。 I am trying to work through all of the data structures we have learnt this semester and program them by my self to help prepare me for the final.
我正在努力研究我们本学期学到的所有数据结构,并自行编写程序以帮助我为期末考试做准备。 I am working on the max heap right now which includes a inserting (with a heapify) and a retrieve max.
我现在正在处理最大堆,其中包括插入(使用堆化)和检索最大值。 I am stuck on the heapifying/swapping of the parent and the child.
我被困在父母和孩子的堆积/交换上。 It seems that the heapify is not working as I get back an array in the order of the way the numbers were inserted.
似乎 heapify 不起作用,因为我按照插入数字的顺序取回了一个数组。 Here is what I have so far.
这是我到目前为止所拥有的。
private int getLeftChildIndex(int index)
{
return (2*index + 1);
}
private int getLeftChildValue(int index)
{
return heap[2*index + 1];
}
private int getRightChildIndex(int index)
{
return (2*index + 2);
}
private int getRightChildValue(int index)
{
return heap[2*index + 2];
}
private int getParentIndex(int index)
{
return ((int) Math.ceil((index - 2)/2));
}
private void swap(int child, int parent)
{
int temp = heap[parent];
heap[parent] = heap[child];
heap[child] = temp;
}
private void insert(int num)
{
heap[heapSize] = num;
heapSize++;
int index = heapSize - 1;
while (getParentIndex(index) > 0 && heap[index] > heap[getParentIndex(index)])
{
swap(index, getParentIndex(index));
index = getParentIndex(index);
}
}
public static void main(String[] args)
{
HeapTest heap = new HeapTest();
heap.insert(15);
heap.insert(5);
heap.insert(10);
heap.insert(30);
}
which just gives an array of the form [15,5,10,30] for example.例如,它只是给出了 [15,5,10,30] 形式的数组。 This array for a max heap should be of the form [30,15,10,5]
这个最大堆数组的形式应该是 [30,15,10,5]
Im expecting this: [15,5,10,30] -> [15,30,10,5] -> [30,15,10,5]我期待这个:[15,5,10,30] -> [15,30,10,5] -> [30,15,10,5]
Could any one provide some insight as to why the heapify part of insert is not working?任何人都可以提供一些关于为什么插入的堆化部分不起作用的见解吗?
Thanks!!谢谢!!
There are a couple of issues I can see.我可以看到几个问题。
consider what Math.ceil((index - 2)/2)
would return for index 3. This would return 0 rather than 1 because the /2
is an integer operation.考虑
Math.ceil((index - 2)/2)
将为索引 3 返回什么。这将返回 0 而不是 1,因为/2
是 integer 操作。 Changing that to /2.0
will fix it.将其更改为
/2.0
将修复它。 Or even simpler would be to make use of integer arithmetic with (index - 1) / 2
which is clearer and more efficient.或者更简单的方法是使用 integer 算法和
(index - 1) / 2
,这样更清晰,更高效。
getParentIndex(index) > 0
means that you are ignoring the root node at index 0 - currently your code will never swap that item. getParentIndex(index) > 0
意味着您将忽略索引 0 处的根节点 - 目前您的代码永远不会交换该项目。 changing to >=
will fix it.更改为
>=
将修复它。
There could well be other issues but those are 2 I can see by inspection.很可能还有其他问题,但这些是 2 我可以通过检查看到。 Either unit testing or interactive debugging would have uncovered those issues.
单元测试或交互式调试都会发现这些问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.