jjzjj

带有比较器的java heapify方法

我正在尝试编写一个类HeapQueue。我将根的左child存储在2*indexOfRoot+1索引处,将右child存储在2*indexOfRoot+2处。publicclassHeapQueueimplementsPriorityQueue,BinaryHeap{publicListqueue;publicComparatorcomparator;publicHeapQueue(){queue=newArrayList();}publicvoidsetComparator(Comparatorcomparator){this.comparator=comparator;heapif

java - Max Heapify 算法结果

我一直在研究算法入门教科书中的一些算法,特别是我正在尝试让二叉堆100%正确地工作。我有一种奇怪的感觉,我正在使用的示例不正确,我想知道是否有人可以帮助我指明正确的方向。给定数组int[]arr={1,2,3,4,7,8,9,10,14,16};我从MaxHeapify得到的结果是[16,14,9,10,7,8,3,1,4,2]但是,在进行了一些Google搜索之后,我发现使用这个精确数组作为示例的人期望的结果是:[16,14,10,8,7,9,3,2,4,1]令我困惑的是,我的MaxHeapify方法给出的结果满足堆属性,但它与预期的不同。下面是我在Java中的实现publicsta

c++ - 我是否正确实现了 "Heapify"算法?

我正在为计算机科学类(class)创建一个堆实现,我想知道下面的递归函数是否会从一个还不是堆的数组对象中创建一个堆。代码如下:voidHeap::Heapify(inti){inttemp,l,r,heapify;l=LeftChild(i);//gettheleftchildr=RightChild(i);//gettherightchild//ifoneofthechildrenisbiggerthantheindexif((Data[i]Data[r]){//swapparentwithleftchildtemp=Data[i];Data[i]=Data[l];Data[l]=t

c++ - 我是否正确实现了 "Heapify"算法?

我正在为计算机科学类(class)创建一个堆实现,我想知道下面的递归函数是否会从一个还不是堆的数组对象中创建一个堆。代码如下:voidHeap::Heapify(inti){inttemp,l,r,heapify;l=LeftChild(i);//gettheleftchildr=RightChild(i);//gettherightchild//ifoneofthechildrenisbiggerthantheindexif((Data[i]Data[r]){//swapparentwithleftchildtemp=Data[i];Data[i]=Data[l];Data[l]=t