英文:
Difference between indexed and sequential data structure
问题
什么是索引数据结构和顺序数据结构之间的确切区别?例如,HashSet 是一种索引数据结构,而 TreeSet 则是顺序数据结构。
英文:
What exactly is the difference between indexed and sequential data structures? For example, HashSet is an indexed data structure but TreeSet is sequential.
答案1
得分: 1
使用索引时,您可以直接读取或写入数据结构中的任何位置。如果访问是顺序的,您必须遍历所有元素,直到达到所需的元素(类似迭代next
方法)。
这也意味着对于索引结构,无论其大小如何,访问时间都是恒定的,而对于顺序结构,随着大小的增加而增加。
这适用于假设索引的内部访问实现(实际上只是可以以许多方式实现的访问操作)基于某种映射,并且顺序结构使用某种链接列表。这应该与所提问题的精神保持一致。
例如,根据实现方式不同,Python中的列表在内部实现直接访问,出于明显的性能原因,除了用户可以使用索引访问它们,而不是使用next方法。
英文:
When using an index you can read or write data directly to any position in the data structure. If the access is sequential, you have to go through all the elements until you reach the wanted one (like iterating a next
method).
This also means access time is constant for an indexed structure, no matter its size, while it increases with the size for a sequential structure.
This applies supposing that the internal implementation of access for indexes (that are actually just an access operation that can be implemented in many ways) is based on some kind of mapping, and that the sequential structure uses some kind of linked lists. This should be aligned with the spirit of the formulated question.
For an example on how this can be different depending on the implementation, lists in Python implement direct access internally, for obvious performance reasons, besides being accessed by the user with an index, not with next methods.
答案2
得分: 1
在Java中,这些术语并没有固定的含义。这些含义将从日常英语用法中获得,并从你列出的类型的检查中获得。
“Indexed”意味着与数据结构相关联,并且可用于引用数据结构的元素的索引,最基本的例子是数组,通过整数偏移进行索引,或者是Map
,通过键值进行索引。请注意,这省略了数据结构具有自然顺序的意义。我们可以看到数组有一个自然顺序,但Map
没有。
“Sequential”意味着数据结构具有可以用于对数据结构的元素排序的顺序。有一种感觉应该是自然顺序,但该术语还可以意味着对数据结构施加了一种顺序,从而实现了迭代操作。
在这两种情况下,含义都不应包括对特定操作的引用。数据结构可以支持读取、写入或迭代,但不一定支持所有这些操作。例如,顺序数据结构可以支持“findFirst”操作,但不允许迭代作为外部操作。
对于这两种类型,即HashSet
和TreeMap
,由于这些是实现类型,这些术语可以用来描述数据结构的一般属性,或者可以用来描述实现的属性。我不确定这是否非常有用,因为实现可以改变。
请注意,“indexed”并不意味着“sequential”,除非索引值本身是连续的。
英文:
Within java, those terms don't have set meanings. The meanings will be obtained from everyday English usage, and from an examination of the types which you listed.
'Indexed' would mean that there is an index associated with a data structure and which can be used to reference elements of the data structure, with the most basic example being an array, which is indexed by integer offset, or Map
, which is indexed by key values. Note that this omits a sense of the data structure having a natural order. We can see that arrays have a natural order but Map
does not.
'Sequential' would mean that the data structure has an order which may be used to order operations on the elements of the data structure. There is a sense that this should be a natural order, but the term could also mean that there is an order imposed on the data structure which enables iterative operations.
In neither case should the meanings include a reference to particular operations. A data structure may support reading, or writing, or iteration, but won't necessarily support any or all of these. For example, a sequential data structure could support a findFirst
operation without allowing iteration as an external operation.
For the two referenced types, HashSet
and TreeMap
, since these are implementation types, the terms can be used to describe the general properties of the data structure, or can be used to describe properties of the implementation. I'm not sure that is very useful, since the implementation can change.
Note that 'indexed' doesn't imply 'sequential', unless the index values are themselves sequential.
答案3
得分: 0
让我们从更广泛的数据结构领域回答这个问题,而不仅仅关注于Java或任何其他实现。
索引数据结构
基于一个更一般的随机/直接访问数据结构概念。
使用这些数据结构的主要优点是,它们在读取和写入操作上的时间复杂度都是O(1)。
例如,当您定义数组时,一系列相同大小的小内存片段(块)在物理RAM中分配,并且每个这些片段都有唯一的、但连续链接的内存地址。这意味着,例如,如果array[0]
存储在0100X001
,则array[1]
将在0100X010
分配,如果插槽占用1位(如果更多,相应地您将需要为每个插槽添加那么多位);
顺序数据结构
另一方面,在计算机科学领域中没有明确定义,它们根据实现方式对读写操作具有不同的时间复杂度,但大多数情况下都不是O(1)。
在大多数情况下,顺序数据结构的实现方式是:
- 每个元素(除了第一个元素)都有指向其直接前驱的引用;
- 每个元素(除了最后一个元素)都有指向其直接后继的引用。
英文:
Let us answer this question in a broader, Data Structures domain, rather than concentrating on Java, or any other implementation, as such.
Indexed data structures
are based on a more general - Random/Direct Access Data Structures concept.
Main benefit of using these data structures is, that they have O(1) time complexity, for both - read and write operations.
For instance, when you define the array, a continuous chain of the same-sized, little memory slices (blocks) are allocated in a physical RAM and each of those slices has, unique, but continuously chained memory address. That means, if, for example, array[0]
is stored at 0100X001
, array[1]
will be allocated at 0100X010
if the slot occupies 1 bit (if more, correspondingly you will have to add that number of bits to each slot);
Sequential Data Structure
on the other hand, are not clearly defined in the Computer Science domain, they, depending on the implementation, have different time complexities for read and write operations, but mostly - it's never O(1).
In the most cases, Sequential Data Structures are implemented in a way, that:
- Each element, except the first, has a reference to its immediate predecessor;
- Each element, except the last, has a reference to its immediate successor.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论