Is there any data structure in C++ STL for performing insertion, searching and retrieval of kth element in log(n)?

好久不见. 提交于 2021-02-11 04:29:50

问题


I need a data structure in c++ STL for performing insertion, searching and retrieval of kth element in log(n)

(Note: k is a variable and not a constant)

I have a class like

class myClass{
  int id;
  //other variables
};

and my comparator is just based on this id and no two elements will have the same id.

Is there a way to do this using STL or I've to write log(n) functions manually to maintain the array in sorted order at any point of time?


回答1:


Afaik, there is no such datastructure. Of course, std::set is close to this, but not quite. It is a red black tree. If each node of this red black tree was annotated with the tree weight (the number of nodes in the subtree rooted at this node), then a retrieve(k) query would be possible. As there is no such weight annotation (as it takes valuable memory and makes insert/delete more complex as weights have to be updated), it is impossible to answer such a query efficently with any search tree.

If you want to build such a datastructure, use a conventional search tree implementation (red-black,AVL,B-Tree,...) and add a weight field to each node that counts the number of entries in its subtree. Then searching for the k-th entry is quite simple:

Sketch:

  1. Check the weight of the child nodes, and find the child c which has the largest weight (accumulated from left) that is not greater than k
  2. Subtract from k all weights of children that are left of c.
  3. Descend down to c and call this procedure recursively.

In case of a binary search tree, the algorithm is quite simple since each node only has two children. For a B-tree (which is likely more efficient), you have to account as many children as the node contains.

Of course, you must update the weight on insert/delete: Go up the tree from the insert/delete position and increment/decrement the weight of each node up to the root. Also, you must exchange the weights of nodes when you do rotations (or splits/merges in the B-tree case).

Another idea would be a skip-list where the skips are annotated with the number of elements they skip. But this implementation is not trivial, since you have to update the skip length of each skip above an element that is inserted or deleted, so adjusting a binary search tree is less hassle IMHO.

Edit: I found a C implementation of a 2-3-4 tree (B-tree), check out the links at the bottom of this page: http://www.chiark.greenend.org.uk/~sgtatham/algorithms/cbtree.html




回答2:


You can not achieve what you want with simple array or any other of the built-in containers. You can use a more advanced data structure for instance a skip list or a modified red-black tree(the backing datastructure of std::set).

You can get the k-th element of an arbitrary array in linear time and if the array is sorted you can do that in constant time, but still the insert will require shifting all the subsequent elements which is linear in the worst case.

As for std::set you will need additional data to be stored at each node to be able to get the k-th element efficiently and unfortunately you can not modify the node structure.



来源:https://stackoverflow.com/questions/23906274/is-there-any-data-structure-in-c-stl-for-performing-insertion-searching-and-r

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