- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试实现算法书 here 中给出的 max heapify 算法书中的算法是
MAX-HEAPIFY(A,i)
1 l<-LEFT(i)
2 r<-RIGHT(i)
3 if l<=heap-size[A] and A[l]>A[i]
4 then largest<--l
5 else largest<--i
6 if r<=heap-size[A] and A[r]>A[largest]
7 then largest <->r
8 if largest!=i
9 then exchange A[i]<->A[largest]
10 MAX-HEAPIFY(A,largest)
我的问题是我的数组是从零开始的。在书中他们假设如果父索引是 i 那么左 child 是 2i 右 child 是 2i+1 但那是他们的索引从 1 开始的时候。在我的如果它是零那么我应该如何计算左右 child 的索引?
最佳答案
左 child 在 2i+1,右 child 在 2(i+1) = 2i+2。
您可以通过调用基于 1 的索引 j、定义 i = j - 1 并观察它来证明这是正确的
j = i + 1
left + 1 = 2j = 2(i+1) = 2i+2
right + 1 = 2j+1 = 2(i+1) + 1
所以
left = 2i+1
right = 2(i+1)
(您也可以通过过度分配单个元素来省去一些麻烦。)
关于c - 来自 Cormen 的 max heapify 算法,具有零索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6444547/
我正在尝试构建一个最小堆,但未能获得正确的结果。我不确定可能出了什么问题。 输入 = 209 97 298 54 110 27 250 455 139 181 446 206 478 90 88 输出
我从以下位置找到了堆排序的代码:http://rosettacode.org/wiki/Sorting_algorithms/Heapsort#C 我的理解方式(这在某些地方是错误的)是 heapso
下面是堆类。我正在尝试对堆进行排序,但我的 max_heapify 函数有问题。我插入了值 [10, 9, 7, 6, 5, 4, 3] 并且我的堆排序打印给定的输出。给定的输出和期望的输出在类下面给
我正在学习一门关于数据结构和算法的类(class),我应该在其中实现堆排序算法以在指定的时间范围内运行。下面是两个实现: def generateSwaps(): size=self._n f
def heapify(A): for root in xrange(len(A)//2-1, -1, -1): rootVal = A[root] child
我想插入一些节点,看来这个功能有问题。可以插入但不能执行heapify。我想知道添加新元素后索引是否困惑。欢迎任何评论。谢谢。 def heapify(arr, n, i): smallest
这是我最近遇到的面试问题之一。 给定完整或几乎完整二叉树的根地址,我们必须编写一个函数将树转换为最大堆。 这里不涉及数组。树已经构建完毕。 例如, 1
我有一个关于堆排序的问题。我不知道我的代码有什么问题。该程序仅更改了表中的第二个和最后一个位置。这是我的代码(没有主要功能): #include #define DUZO 100000000 i
我正在尝试制作一种适用于我的自定义容器的自定义(而不是使用 STL 方法)堆排序方法。我有迭代器,但是我找不到 make_heap 算法的描述。据我所知,它的作用类似于排列元素,使它们看起来像一个二叉
问题是我正在尝试修复最大堆,但它不起作用,因为错误不断发生。我一直在遵循几本书中的伪代码,但仍然显示错误。我尝试使用 = 进行交换,将 A[i] 交换为 A[largest],但出现错误 class
一旦调用了remove min函数,我如何在java中“堆化”基于数组的最小堆(这只是获取索引1处的元素并将其删除,然后用数组中的最后一项替换它)。我很困惑在删除最小值发生后如何将数组再次放入最小堆中
每次 heapq.heapify 函数更改我的堆列表中的元素时,我都希望得到回调通知(顺便说一句,这是跟踪列表中的对象以及它们的索引如何获取所需要的改变了)。 我的计划是从 list 继承并重写 __
Tarjan 的“数据结构和网络算法”在 leftiest heaps 中陈述了 heapify 函数如下: heap function heapify (list q); do |q| ≥ 2
我试着看http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-alg
我正在用 Ruby 实现 Heapify 算法(即将二叉树转换为堆),并认为下面的算法有效。我担心的是我进行了过多的递归调用。 第一次和第二次递归调用是从左右子树中选择最高节点,第三次递归调用是在我交
我一直在研究算法入门教科书中的一些算法,特别是我正在尝试让二叉堆 100% 正确地工作。我有一种奇怪的感觉,我正在使用的示例不正确,我想知道是否有人可以帮助我指明正确的方向。 给定数组 int[ ]
我对最大堆排序伪代码的一部分感到困惑。下降到 2 是什么意思? HeapSort(A) Build-Max-Heap(A) for i down to 2 do exchange A[1] A[i]
在CLRS书中,自上而下的heapify构建堆的复杂度为O(n)。也可以通过反复调用插入来建立堆,其最坏情况下的复杂度为nlg(n)。 我的问题是:对于后一种方法性能较差的原因,是否有任何见解? 我问
我有 b = heapify([5,4,9,1]) 如果我做一个 type(b) 它说它是 NoneType 而不是列表类型,有人知道我做错了什么吗?? 最佳答案 heapify() 方法就地转换列表
我正在为计算机科学类(class)创建一个堆实现,我想知道下面的递归函数是否会从一个还不是堆的数组对象中创建一个堆。代码如下: void Heap::Heapify(int i) { int
我是一名优秀的程序员,十分优秀!