Red ebony with the same key several times: store collections in nodes or store them as multiple nodes?

Apparently, you could do this, but the first is more common.

Why did you choose the latter and how does it work?

I read this: http://www.drdobbs.com/cpp/stls-red-black-trees/184410531 ; which made me think that they did it. It says:

insert_always is a state variable that tells rb_tree whether multiple instances of the same key value are allowed. This variable is set by the constructor and is used by STL to distinguish between multitude and multiset, as well as between map and multimap. set and map can have only one occurrence of a certain key, while multiset and multimap can have several occurrences.

Although now I think that this does not necessarily mean it. They can still use containers.

I think that all nodes with the same key should be in the row, because you either need to store all nodes with the same key on the right side or on the left side. Therefore, if you store equal nodes to the right and insert 1000 1 and 2, you will have basically a linked list that destroys the properties of red ebony.

Is there a reason why I can’t find a lot on it that this is just a bad idea?

+4
source share
2 answers

bottom side of the repository as several nodes:

  • expands the size of the tree, which makes the search slower.

  • if you want to get all the values ​​for the key K, you need the time M * log (N), where N is the number of shared nodes, M is the number of values ​​for the key K, if you do not enter additional code (which complicates the data structure) to implement the associated list for these values. (if storing the collection, only log (N) takes complexity in time, and it's easy to implement)

  • more expensive to remove. using the multi-node method, you need to remove the node for each deletion, but with storage-storage you need to delete node K when the last value of the K key is deleted.

You cannot think of any good side of the multi-node method.

+2
source

Binary search trees, by definition, cannot contain duplicates. If you use them to create a sorted list, throwing duplicates will lead to an incorrect result. I am working on implementing Red Black trees in PHP when I come across a duplicate problem. We are going to use the tree for sorting and searching. I am considering adding an occurrence value to the node data type. When a duplicate occurs, an increment simply occurs. When traversing the tree to get the result, simply repeat the value by the number of occurrences. I think that I will still have a valid BST and will not have a whole chain of duplicate values ​​that preserve optimal search time.

+2
source

Source: https://habr.com/ru/post/1436754/


All Articles