是否可以在不重建堆的情况下从两个堆中建造最大堆?
我最近参加了计算机科学考试,有一个问题。
有两个最大 - 蜂座(已实现数组)。您需要提出一种算法 合并这两个Max-Heaps,并创建一个新的Max-Heap(已实现的数组)
问题的新的Max-Heap(Array实现)解决方案非常直观,这是:
- 合并
- 两个阵列调用堆重建
我在Internet中看到的
,并遇到了相同类型的解决方案。但是,我写了一个类似的解决方案,我无法自己驳斥自己的解决方案。
我的算法
- 创建index1和index2,该指向heaparr1和heaparr2的第一个元素
- 创建一个新的堆数组,该数组的大小为HeaParr1.size + Heaparr22.Size
- 在循环中
- 比较heaparr1和heaparr2元素的索引元素
- 以更大为准,写入结果数组并递增该数组所采集的数组的索引,直到两个数组全部遍历。
例如
heap1:12-5 -6-2-3
heap2:15-13-4-1-0
我们将首先比较15和12
。
HEAP2 : EM>现在比较13和12
resultheap:15-13
比较4和12
resultheap:15-13-13-12
比较4和5
resultheap:15-13-12-4
如果我们这样继续,我们有
resultheap :15-13-13-12 -5-6-4-2-2-3-1-1-0。它也是堆
该算法正确吗?还是有人可以提供反驳数据集?
I recently took my Computer Science exam and there was a question like that.
There are two max-heaps (array implemented). You need to come up with an algorithm that
merges these two max-heaps and creates a new max-heap (array implemented)
Solution of the question is very intuitive that is:
- Merge two array
- Call heap rebuild
I looked in the Internet and encountered with same type of solutions.
However, I wrote a solution like that which I could not refute own my own.
My Algorithm
- Create index1 and index2 which points first element of heapArr1 and heapArr2
- Create a new heap array which has size of heapArr1.size + heapArr2.size
- In while loop
- compare index1 element of heapArr1 and index2 element of heapArr2
- Whichever is greater, write the result array and increment the index of the array that element taken until two arrays all traversed.
For example
Heap1: 12-5 -6-2-3
Heap2: 15-13-4-1-0
We wil first compare 15 and 12. Write 15
resultHeap: 15
Now compare 13 and 12
resultHeap: 15 - 13
Compare 4 and 12
resultHeap: 15 - 13 - 12
Compare 4 and 5
resultHeap: 15 - 13 - 12 - 4
if we go on like that we have
resultHeap: 15 - 13 - 12 - 5 - 6 - 4 - 2 - 3 - 1 - 0. And it is also a heap
Is this algorithm correct? Or can someone gave the refutation data set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不。
进行此输入:
第一个堆:10、2、9
第二堆:8、1、4
应用算法 - 括号指示每个堆中的电流索引:
结果不是有效的堆,因为9不应是8个孩子,因为9更大。 4不应该是2的孩子,因为4更大。
No.
Take this input:
First Heap: 10, 2, 9
Second Heap: 8, 1, 4
Apply the algorithm -- the brackets indicate the current index in each heap:
The result is not a valid heap as 9 should not be a child of 8, since 9 is greater. And 4 should not be the child of 2, since 4 is greater.
这可能是尺寸相等的堆的可能解决方案。
此过程递归将B的Chilren合并,并将结果堆分配为B的合适孩子。
这只是花哨的指针操纵,实际上并没有构建堆,而是利用您已经拥有堆的事实。
道歉,无法更形式地表达它。如果您发现有问题,请纠正我。这只是一般的想法,不考虑实施具体的细节。
This could be one possible solution for heaps of equal size.
this procedure recursively to merge B's chilren and assign the resultant heap as the right child of B.
This is just fancy pointer manipulation and it isn't actually building a heap, rather taking advantage of the fact that you already have heaps.
Apologies for not being able to express it more formally. Please correct me if you find something wrong. This is just the general idea and doesn't consider implementation specific details.