algorithm - Data structure for a set of keys with efficient find-missing -


i'm looking data structure supports following operations integer keys k ranging 0 m-1.

  • o(1) or o(log n) insert(k), erase(k), lookup(k).
  • o(1) or o(log n) special operation find_missing_key() returns key not present in structure.
  • o(n) or o(n log n) space. in particular. should not o(m).

an obvious implementation "list-of-free-keys" structure, implemented heap; take o(m) space. there data structure fulfills of requirements?

use binary segment tree.

each node in tree represents range of integers [a,b], , either leaf [a,a] or divides 2 nodes representing ranges [a,m] , [m+1, b] m (a+b)/2.

only expand nodes when necessary, have root node range [0,m-1] (or [0,m) if prefer)

in each node, keep count of how many used/free spots have in subtree.

insertion, lookup, , deletion of x o(log n): keep subdividing until reach [x,x], , update on path node root.

find_missing_key o(log n): since know size of each segment , how many free elements in it, can decide @ each node whether go left or right in order find free element.

(edit: incidentally, allows find first, or last, or i_th free element, @ no additional cost.)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -