Why 'set' data structure is told to be unordered?

痞子三分冷 提交于 2021-02-08 11:55:46

问题


Well, I think it's perfectly ordered, because position of the elements depends on their hash function, thus if that objects are immutable, then after you put them in the set, their position will remain unchanged. And everytime you, say, print your set you'll have exact the same elements order. Sure, you can't predict their position until their hash function is calculated, but anyway.


回答1:


Generic / abstract data type

The definition of set from Aho, Hopcroft, Ullmann: 'Data Structures and Algorithms', Addison-Wesely, 1987:

A set is a collection of members (or elements); [...] . All members of a set are different, [...]

The abstract data type set does not have the characteristic of ordered or unordered. There are some methods defined which operate on a set - but none of them has something to do with ordering (e.g see Martin Richards: 'Data Structures and Algorithms').

Two sets are seen equal, if each element from one set is also inside the other - and there are no additional elements.

When you write down a set (and therefore all the elements of a set) you need to write them down in some order. Note that this is just a representation of the appropriate set.

Example: A set which contains the elements one, two and three can be written down as {1, 2, 3}, {1, 3, 2}, {3, 1, 2} and so on. These are only different representations of the same set.

Specific implementations

In different programming languages sets are implemented in different ways with different use cases in mind.

In some languages (like python, JAVA) the standard set implementations do not expose ordering in their interfaces.

In some languages (like C++) the standard set implementation exposes ordering in their interfaces.

Example (C++):

Internally, the elements in a set are always sorted from lower to higher following a specific strict weak ordering criterion set on container construction.

template < class Key, class Compare = less, class Allocator = allocator > class set;

(see C++ set).




回答2:


The hash function is not (usually) an externally visible or modifiable parameter of a set; moreover, even if you have an implementation where the hash function is known and well characterized, you can't specify the behavior when hash values collide.

The usual summary of this is that the implementation of the set may impose an order, but the interface does not.




回答3:


To which definition of set are you referring? In my understanding, «set» is a name for a data structure that contains a number of unique elements and usually allows addition and deletion. Everything else is not guaranteed and may be subject to implementation.

It doesn't say there is no order, but there is no specific order for every valid implementation. The use of hashtables is common, but using any type of list or tree is also possible.

So the order might be through a hashfunction (already lots of possible implementations), or related to order of addition or ...




回答4:


After you add a new element to HashSet, it gets modified and the new element may get positioned anywhere depending on the hash value calculation. Thus the previous order is not maintained.



来源:https://stackoverflow.com/questions/10310608/why-set-data-structure-is-told-to-be-unordered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!