【问题标题】:How to design a data structure which is a mixture of hashtable and heap如何设计混合哈希表和堆的数据结构
【发布时间】:2011-01-10 06:02:31
【问题描述】:

我的问题如下:

给定一个三元组列表,它们存储在一个名为 hash_heap 的数据结构中(我不确定名称,只是表示它应该是哈希表和堆的混合体)。我希望数据结构提供以下方法,

index_by_first_col(key) // the method could help find the a triple stored in it by matching the first column. It expects the searching is running at constant time
get_min_by_third_col() // the method get the minimum triple sort according to the third column, it is also expects the method is running at constant time.
insert_new_elt(triple) // add new trip, running at constant time

是否可以实现一些这样的数据结构?我知道 hashtable 可以支持第一种方法,而 heap 可以支持第二种和第三种方法,但我不知道如何将它们混合在一起。有什么想法吗?

谢谢!

【问题讨论】:

标签: algorithm heap hashtable


【解决方案1】:

有一个非常简单的数据结构可以满足您陈述的要求。

使用哈希表(在第一个 col 上键入)并另外存储一个指向最小元素的指针(通过第三个 col)。然后,index_by_first_col 是哈希表查找,get_min_by_third_col 是指针解引用,insert_new_elt 是哈希表插入和比较以确定是否需要更新 min 指针。

请注意,这给出了 O(n) 次删除(最少),但您没有说明删除的任何要求。

【讨论】:

  • 其实我需要delete操作,'get_min_by_third_col'更准确的描述应该是pop_min_by_third_col
猜你喜欢
  • 1970-01-01
  • 2011-01-01
  • 2010-12-07
  • 2011-07-22
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 2017-11-08
  • 1970-01-01
相关资源
最近更新 更多