Rank Tree in C++

后端 未结 4 1959
一生所求
一生所求 2020-12-19 01:41

We need ADT having search and rank features. That is, in addition to the interface of STL map, a function \'int get_rank(key)\' is required.

Standard implementation

4条回答
  •  Happy的楠姐
    2020-12-19 02:18

    The rank of the given key K is the number of keys which are less or equal to K.

    E.g., let set s = {1, 3, 4, 6, 9}. Then rank(1) = 1, rank(4) = 3, rank(9) = 5.

    The STL function distance() can be used for computing the rank of an element x appearing in the set s.

    rank = distance(s.begin(), s.find(x));

    The problem is that its time complexity is O(n).

    Note that proposed two maps (or sets) indexed by key and by rank is not correct solution. The problem is that a change of one element affects ranks of many others. E.g., adding element 0 to the set s above change the ranks of all existing elements: s' = {0, 1, 3, 4, 6, 9}. rank(1) = 2, rank(4) = 4, rank(9) = 6.

    Thanks.

提交回复
热议问题