algorithm - Constrained Set -


developing parts of project needed implement data set structure should allow me subsets depending on minimun-maximun key value:

constainedset <- set((key,value)*) subset <- constainedset.match(constraint = "val1 <= key < val2") 

then subset should containt elements constainedset matching "val1 <= key < val2" restriction. is, elements key greater or equal val1 lesser val2.

for example, if had subset one:

constrainedset <- {(1,hand),(2,eye),(3,nose)} 

then operation like:

subset <- constainedset.match(constraint = "1 <= key < 3") 

ought result generate subset:

subset <- {(1,hand),(2,eye)} 

i developed solution each element stored in vector of triplets like

(minkey,maxkey+1,value)  

i keep vector sorted minkey , maxkey having minkey order higher priority maxkey order. each call "match" performs binary search on vector.

if not wrong worst case time complexities are:

  • o(n) each call "match".
  • o(n) insertions.

where n number of elements in set.

is there better solution?

store in balanced binary tree key.

lookup, insertion: o(log n).

if have copy results, that's going o(n), if no copy needed subset can represented pair of "iterators", can return pair of min , max nodes tree, whole thing going o(log n).


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -