Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Data Structure/Algorithm for insert/delete/rank/select queries

So far, I know that self-balancing BST like AVL tree and Red Black Tree can do these operations in O(log n) times.

However, to use these structures, we must implement AVL tree or RB tree ourselves.

I have heard that there is an algorithm/ implementation of these four operations without using self-balancing BST.

With our own defined structure, we need to write so many lines. However, I have heard that there is a possibility of supporting these four operators in less than 100 lines code :\

Do you guys have any ideas on how this should be done?

Other than BST, is there any other possible options?

like image 443
FiveFiftyFive Avatar asked Oct 21 '25 07:10

FiveFiftyFive


1 Answers

As mentioned in the comments, if you want to maintain a set of integers and you can do a coordinate compression as a preprocessing step (e.g. because your algorithm is offline and knows all the future queries), you can use a binary-indexed tree to support insert/delete/rank/select of numbers in O(log n) per operation. Here's an example in C++:

int tree[N];  // where N is the number of compressed coordinates
const int maxl = floor(log(N)/log(2));

void insert(int i) { // 1 <= i <= N
  for (; i <= N; i += i & -i) ++tree[i];
}

void remove(int i) { // 1 <= i <= N
  for (; i <= N; i += i & -i) --tree[i];
}

int rank(int i) { // 1 <= i <= N
  int sum = 0;
  for (; i; i -= i & -i) sum += tree[i];
  return sum;
}

int select(int k) { // k is 1-based
  int ans = 0, s = 0;
  for (int i = maxl; i >= 0; --i) // maxl = largest i s.t. (1<<i) <= N
    if (s + tree[ans + (1<<i)] < k) {
      ans += 1<<i;
      s += tree[ans];
    }
  return ans+1;
}

The select function is somewhat magical. It reuses the results from higher bits to compute the prefix sum of ans + (1<<i) in O(1), which is pretty cool IMHO :) This way it only takes time O(log n), instead of the O(log^2 n) that is easy to achieve using a standard binary search.

like image 62
Niklas B. Avatar answered Oct 25 '25 01:10

Niklas B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!