二进制堆中的删除

发布于 2024-12-07 03:19:13 字数 529 浏览 1 评论 0原文

我只是想学习二进制堆,并对在二进制堆中执行删除操作有疑问。 我读到我们可以从二进制堆中删除一个元素,并且需要重新堆化它。

但在以下链接中,它显示不可用:

http://en.wikibooks.org/wiki/Data_Structures /权衡

                Binary Search  AVL Tree   Binary Heap (min)  Binomial Queue (min)

Find            O(log n)       O(log n)   unavailable         unavailable
Delete element  O(log n        O(log n)   unavailable         unavailable

我对此有点困惑。

预先感谢您的所有澄清。

I am just trying to learn binary heap and have a doubt regarding doing delete operation in binary heap.
I have read that we can delete an element from binary heap and we need to reheapify it.

But at the following link, it says unavailable:

http://en.wikibooks.org/wiki/Data_Structures/Tradeoffs

                Binary Search  AVL Tree   Binary Heap (min)  Binomial Queue (min)

Find            O(log n)       O(log n)   unavailable         unavailable
Delete element  O(log n        O(log n)   unavailable         unavailable

I am little confused about it.

Thanks in advance for all of the clarifications.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

左耳近心 2024-12-14 03:19:13

二叉堆和其他优先级队列结构通常不支持一般的“删除元素”操作;您需要一个额外的数据结构来跟踪堆中每个元素的索引,例如哈希表。如果有的话,您可以

Binary heaps and other priority queue structures don't usually support a general "delete element" operation; you need an additional data structure that keeps track of each element's index in the heap, e.g. a hash table. If you have that, you can implement a general delete operation as

  • find-element, O(1) expected time with a hash table
  • decrease key to less than the minimum, O(lg n) time
  • delete-min and update the hash table, O(lg n) combined expected time.
眉目亦如画i 2024-12-14 03:19:13

可以进行常规删除,就像删除最小/最大一样。 “问题”是你必须检查上移和下移(即:当“最后一个”节点占据空缺位置时,它可能会被高估或低估。因为它仍然不能两者兼而有之,因为显然原因,很容易检查正确性。

剩下的唯一问题是查找。上面的答案指出您可以在 O(lg n) 中查找元素,但在我的实现中,我通常会构建一个。堆的指向元素而不是元素的指针(在上移/下移期间复制更便宜),我在 Element 类型中添加了一个“位置”变量,这样,给定元素 E 时,它可以跟踪堆中元素指针的索引。 ,我可以在恒定时间内找到它在堆中的位置,

显然,这并不是针对每个实现的。

A regular delete is possible, just like a DeleteMin/Max. The "problem" is that you have to check for both up- and downshifts (i.e.: when the "last" node takes up the vacant spot, it can be over- or underevaluated. Since it still can't be both, for obvious reasons, it's easy to check for correctness.

The only problem that remains is the Find. The answer above states that you can Find Element in O(lg n), but I wouldn't know how. In my implementations, I generally build a Heap of pointers-to-elements rather than elements (cheaper copying during up/downshifts). I add a "position" variable to the Element type, which keeps track of the index of the Element's pointer in the Heap. This way, given an element E, I can find it's position in the Heap in constant time.

Obviously, this isn't cut out for every implementation.

℡寂寞咖啡 2024-12-14 03:19:13

我很困惑为什么在您的问题的链接中提到二进制堆的删除操作不可用。二叉堆中的删除是很有可能的,它由二叉堆的另外两个操作组成。
https://en.wikipedia.org/wiki/Binary_heap

我认为你知道所有其他二叉堆的操作

从二叉堆中删除一个键需要 2 行代码/操作。假设您要删除索引 x 处的项目。将其值减小到尽可能小的整数。这就是 Integer.MIN_VALUE。由于它是所有整数中的最低值,因此当 decreaseItem(int index, int newVal) 执行完成时,它将转到根位置。然后调用 extractMin() 方法提取根。

// Complexity: O(lg n)
public void deleteItem(int index) {
    // Assign lowest value possible so that it will reach to root
    decreaseItem(index, Integer.MIN_VALUE);
    // Then extract min will remove that item from heap tree. correct ?
    extractMin();
}

完整代码: BinaryHeap_Demo .java

I am confused why delete operation of binary heap is mentioned as unavailable in the link of your question. Deletion in binary heap quite possible and it's composition of 2 other operations of binary heap.
https://en.wikipedia.org/wiki/Binary_heap

I am considering you know all other operations of Binary Heap

Deleting a key from binary heap requires 2 lines of code/operation. Suppose you want to delete item at index x. Decrease it's value to lowest integer possible. That's Integer.MIN_VALUE. Since it's lowest value of all integer it will go to root position when decreaseItem(int index, int newVal) execution done. Afterwards extract the root invoking extractMin() method.

// Complexity: O(lg n)
public void deleteItem(int index) {
    // Assign lowest value possible so that it will reach to root
    decreaseItem(index, Integer.MIN_VALUE);
    // Then extract min will remove that item from heap tree. correct ?
    extractMin();
}

Full Code: BinaryHeap_Demo.java

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