如何设计混合哈希表和堆的数据结构

发布于 2024-10-11 04:26:53 字数 600 浏览 4 评论 0原文

我的问题如下:

给定一个三元组列表,它们存储在一个名为 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

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

谢谢!

My question is as following:

Given a list of triple, and they are stored in a data structure called hash_heap(I am not sure about the name,just mean it should be the mixture of the hashtable and heap). And I hope the data structure provides following methods,

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

Is it possbile to implement some data structure like this? I know hashtable could support the first method and heap could support the second and third method, but I do not know how to mix them together. Any ideas?

Thanks!

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

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

发布评论

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

评论(1

瑾夏年华 2024-10-18 04:26:53

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

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

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

There is a very simple data structure that meets your stated requirements.

Use a hash table (keyed on the first col) and in addition store a pointer to your minimum element (by the third col). Then, index_by_first_col is a hashtable lookup, get_min_by_third_col is a pointer dereference, and insert_new_elt is a hashtable insert and a comparison to determine if the min pointer needs to be updated.

Note that this gives O(n) deletion (of the minimum), but you didn't state any requirements on deletion.

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