二叉堆是否支持减键操作?

发布于 2024-11-05 20:57:46 字数 310 浏览 0 评论 0原文

根据http://en.wikipedia.org/wiki/Heap_%28data_struct%29#Comparison_of_theoretic_bounds_for_variants,需要 θ(logn) (转换为 O(logn))来执行减键操作。但是,似乎没有站点包含具有减少键操作的二进制堆实现。

鉴于网络上缺乏实现,是否可以在二进制堆中执行减少键操作?

According to http://en.wikipedia.org/wiki/Heap_%28data_structure%29#Comparison_of_theoretic_bounds_for_variants, it takes Θ(logn) (which translates to O(logn)) to perform the decrease-key operation. However, there seems to be no site that includes a binary heap implementation with a decrease-key operation.

Given therefore the lack of implementations on the web, is it possible to perform the decrease-key operation in a binary heap?

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

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

发布评论

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

评论(1

呆橘 2024-11-12 20:57:46

我发现:

  • 为了在 O(logn) 中执行减少键,我们必须提前知道相应元素的位置。哈希映射和良好的哈希函数可以保证 O(1) 摊余时间。每次修改后,我们都必须更新哈希映射,这需要 O(logn)。
  • 确定元素的位置后,如果元素的优先级高于其父级(与插入类似),则向上移动元素;如果元素的优先级低于其子级之一(与插入类似),则向下移动元素(与插入类似)删除)并更新哈希映射中修改的元素的位置。

I figured this out:

  • In order to perform a decrease-key in O(logn), we have to know the location of the corresponding element in advance. A hash map and a good hash function can guarantee O(1) amortized time. After each modification, we have to update the hash map, which takes O(logn).
  • After determining the location of our element, we move our element up in case it has a greater priority than its parent (in a similar manner to insertion) or down if it has a lower priority than one of its children (in a similar manner to deletion) and update the modified elements' locations in the hash map.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文