algorithm - 如下所述,为场景 A 和 B 选择哪种数据结构

标签 algorithm data-structures

<分区>

我在问题库中找到了以下内容,我正在寻求一些帮助。

For each of the following situations, select the best data structure and justify your selection.
The data structures should be selected from the following possibilities: unordered list, ordered array, heap, hash table, binary search tree.

(a) (4 points) Your structure needs to store a potentially very large number of records, with the data being added as it arrives. You need to be able to retrieve a record by its primary key, and these keys are random with respect to the order in which the data arrives. Records also may be deleted at random times, and all modifications to the data need to be completed just after they are submitted by the users. You have no idea how large the dataset could be, but the data structure implementation needs to be ready in a few weeks. While you are designing the program, the actual programming is going to be done by a co-op student.

对于答案,我认为 BST 是最好的选择。

由于大小不明确,hashtable不是一个好的选择。
既然有删除的事,堆也不行。

我的推理是否正确?

(b) (4 points) You are managing data for the inventory of a large warehouse store. New items (with new product keys) are added and deleted from the inventory system every week, but this is done while stores are closed for 12 consecutive hours.

Quantities of items are changed frequently: incremented as they are stocked, and decremented as they are sold. Stocking and selling items requires the item to be retrieved from the system using its product key.

It is also important that the system be robust, well-tested, and have predictable behaviour. Delays in retrieving an item are not acceptable, since it could cause problems for the sales staff. The system will potentially be used for a long time, though largely it is only the front end that is likely to be modified.

对于这部分,我想到了堆排序,但我不知道如何证明我的答案是正确的。

你能帮帮我吗?

最佳答案

(a) 需要快速插入和删除,你需要基于键检索。因此我会选择哈希表或二叉搜索树。但是,由于事先不知道大小并且有最后期限限制,所以我认为二叉搜索树是最好的选择。

(b) 您有足够的时间在插入/删除后处理数据,但需要 O(1) 随机访问。有序数组应该可以解决问题。

关于algorithm - 如下所述,为场景 A 和 B 选择哪种数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20411966/

相关文章:

algorithm - O(in) 是二次复杂度的线性复杂度吗?还是取决于k?

math - f(n) = 1000n + 4500lgn + 54n O(n) 吗?

c - 链表 C 的段错误

algorithm - 具有最多节点的二叉树中的级别

c# - 查找最接近的 "nice"标尺图 block 宽度

c# - 如何使用 C# 中的 struct 提供显示为 info 的数据?

java - 找到数组中等于和的最小元素

c - C 中的结构(指针用法和显示关系)

algorithm - 在保持顺序的同时对循环缓冲区进行分区

java - 为什么默认对象的 hashCode 在不同的设备上返回不同的值?