【问题标题】:best data structure suitable for event-calender application适用于事件日历应用的最佳数据结构
【发布时间】:2015-12-07 16:07:24
【问题描述】:

我有一个代表一个事件的类。 所以我们可以通过创建这个类的对象来创建一个事件。构造函数接受description, start time, end time, list of invites, location 并返回一个用于唯一标识此对象的id。

Create events : (description, start time, end time, list of invites, location) -> id

现在这个类有另一个方法,它接受一个 id 并返回一个相应的事件对象

Get event by id : id -> event object

同样我们可以更新一个事件

Update event -> description, start time, end time

现在我们要做的第四个操作是我们要过滤掉一个时间范围内的所有事件

get event : start time, end time

现在我必须使用/设计一个可以有效执行这 4 种操作的数据结构。 (我的意思是我们现在可以使用文件存储或缓存技术)

对于第一个三个操作,我认为我可以使用 Has map/table,它将 id 作为键和对象作为值。 但在这种情况下,我将无法有效地执行第四次操作。

虽然我可以再次使用 trie 数据结构。

Root Node->
      Month nodes (12 nodes)
      each month node will be having 30(or 31/28) children nodes. 
      and then those children nodes will be having time stamp nodes.

但在这种情况下,前三个操作变得有点低效。

如何结合这两种数据结构来解决这个问题并高效地执行所有 4 种操作。或者有没有更好的数据结构可以完成这项任务。还是我们必须创建自己的自定义数据结构?

【问题讨论】:

    标签: algorithm data-structures hashmap binary-search-tree


    【解决方案1】:

    正如您所说,您可以将hashmap 用于第一个树操作。对于第 4 次操作,您可以维护 (start_time_of_event,id) 的排序列表。现在要查找(start,end) 之间的所有事件,您可以在这个排序数组上使用两次二进制搜索并找到(start,end) 之间的所有事件。此方法的空间开销很小,因为您只需要存储事件的idstart_date 的副本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多