是否可以在不重建堆的情况下从两个堆中建造最大堆?

发布于 2025-01-30 19:51:47 字数 1044 浏览 2 评论 0原文

我最近参加了计算机科学考试,有一个问题。

有两个最大 - 蜂座(已实现数组)。您需要提出一种算法 合并这两个Max-Heaps,并创建一个新的Max-Heap(已实现的数组)

问题的新的Max-Heap(Array实现)解决方案非常直观,这是:

  1. 合并
  2. 两个阵列调用堆重建

我在Internet中看到的

,并遇到了相同类型的解决方案。但是,我写了一个类似的解决方案,我无法自己驳斥自己的解决方案。

我的算法

  1. 创建index1和index2,该指向heaparr1和heaparr2的第一个元素
  2. 创建一个新的堆数组,该数组的大小为HeaParr1.size + Heaparr22.Size
  3. 在循环中
  4. 比较heaparr1和heaparr2元素的索引元素
  5. 以更大为准,写入结果数组并递增该数组所采集的数组的索引,直到两个数组全部遍历。

例如

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:

  1. Merge two array
  2. 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

  1. Create index1 and index2 which points first element of heapArr1 and heapArr2
  2. Create a new heap array which has size of heapArr1.size + heapArr2.size
  3. In while loop
  4. compare index1 element of heapArr1 and index2 element of heapArr2
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不会写诗 2025-02-06 19:51:47

此算法正确吗?

不。

有人可以提供反驳数据集吗?

进行此输入:

  • 第一个堆:10、2、9

      10
       / \
      2 9
     
  • 第二堆:8、1、4

      8
       / \
      1 4
     

应用算法 - 括号指示每个堆中的电流索引:

堆1堆2结果堆
[10],2,9[8],1,4,1,410
10,[2],9[8],1,410,8
10,[2],98,[1],410,8,2
10,2,[9]8,[1],[1],[1],[1], 410,8,2,9(违法)
10,2,9 []8,[1],410,8,2,9,1
10,2,9 [] []8,1,[4]10, [4] 10,10 8,2,9,1,4(违规)
      10
     /   \
    8     2
   / \   /
  9   1 4 

结果不是有效的堆,因为9不应是8个孩子,因为9更大。 4不应该是2的孩子,因为4更大。

Is this algorithm correct?

No.

can someone gave the refutation data set?

Take this input:

  • First Heap: 10, 2, 9

        10
       /  \
      2    9
    
  • Second Heap: 8, 1, 4

        8
       / \
      1   4
    

Apply the algorithm -- the brackets indicate the current index in each heap:

Heap 1Heap 2Result heap
[10],2,9[8],1,410
10,[2],9[8],1,410,8
10,[2],98,[1],410,8,2
10,2,[9]8,[1],410,8,2,9 (violation)
10,2,9[]8,[1],410,8,2,9,1
10,2,9[]8,1,[4]10,8,2,9,1,4 (violation)
      10
     /   \
    8     2
   / \   /
  9   1 4 

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.

痴情换悲伤 2025-02-06 19:51:47

这可能是尺寸相等的堆的可能解决方案。

  1. 假设2最大(或min)二进制堆(A和B)。
  2. 首先比较根,一个较小的根(假设a)可以安全地假定为另一个子树的候选者(b),因此将其分配为B的任何一个孩子(假设左),
  3. 因为B的孩子现在是B的被替换,您有2个新堆考虑IE B的原始孩子
  4. ,现在您有一个空白的空间,即合适的孩子。
    此过程递归将B的Chilren合并,并将结果堆分配为B的合适孩子。

这只是花哨的指针操纵,实际上并没有构建堆,而是利用您已经拥有堆的事实。

道歉,无法更形式地表达它。如果您发现有问题,请纠正我。这只是一般的想法,不考虑实施具体的细节。

This could be one possible solution for heaps of equal size.

  1. Assume 2 max(or min) binary heaps(A and B).
  2. First compare the roots, the one which is smaller(assume A) can safely be assumed to be candidate for the subtree of the other one(B), so assign it as any one child of B(assume left)
  3. Since B's children are now being replaced you have 2 new heaps to consider i.e B's original children
  4. And now you have an empty space i.e the right child of B. Follow
    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文