algorithm - Finding the closest intervals in an interval tree that do not contain the query -


i have implemented interval tree in java outlined in clrs algorithm book (it uses red-black tree underlying structure). in book (and far i've seen online), discusses how find node interval contains number being queried.

in case, if number being queried not fall interval, want know 'closest' nodes are, i.e., intervals lie before , after query. have accomplished using following functions. each node contains interval (int low, int high), min , max values of , subtrees.

public node[] findprevnext(int query) {     if (tree.root.isnull())         return null;     else {         node prev = findprev(query, tree.root, new node());         node next = findnext(query, tree.root, new node());         node[] result = {prev, next};         return result;     } }  private node findprev(int query, node x, node prev) {     if (x.interval.high < query && x.interval.high > prev.interval.high)         prev = x;     if (!x.left.isnull())         prev = findprev(query, x.left, prev);     if (!x.right.isnull())         prev = findprev(query, x.right, prev);     return prev; }  private node findnext(int query, node x, node next) {     if (x.interval.low > query && x.interval.low < next.interval.low)         next = x;     if (!x.left.isnull())         next = findnext(query, x.left, next);     if (!x.right.isnull())         next = findnext(query, x.right, next);     return next; } 

the problem, of course, functions findprev() , findnext() both traverse whole tree , don't take advantage of tree's structure. there way perform query in o(lgn) time?

i've considered possibility of creating second interval tree contain interval gaps, , doing query on there. nodes contain information elements before , after gap (i have attempted of yet unsuccessful in implementing this).

edit: note, function findprevnext() called after attempting find query fails. so, query known not fall in given interval beforehand.

the java treemap, implements red-black tree, implements methods headmap , tailmap return portions of map less query point (for headmap) or greater query point (for tailmap). if implement similar these methods tree should allow linear traversal query point find n closest intervals, rather having traverse entire tree.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -