5

Given a binary tree, each of it's nodes contains an item with range, for instance, one, particular node may contain a range of ( 1 to 1.23456 ]

If the query element is less than or greater than the described range, it inspects the respective child. For example, it is 1.3

As follows, we will be looking over the right branch, performing 2 "if" checks to see if it fits in the range of the element.

Even though balanced Binary Search Tree (BST) is an elegant way of traversing quickly through a dataset, the amount of "if" checks grows significantly if there are more and more children. It becomes even more of a problem, when it has to be done several million times per second.

Is there an elegant way of storing objects such that given an element with a value (1.3 for example), its value can be simply fed into something as Dictionary? This would quickly retrieve the proper element to whose range this value fits or null if it fits none.

However, dictionary doesn't check against ranges, instead, it expects a single value. Therefore, is there a data structure which can provide an item if supplied key fits within the item's range?

Here a person has similar problem, however he finds out that the memory is wasted. He is being advised to BST approach, but is it the only solution?

Sorry if there is an evident answer, I may missed it.

Community
  • 1
  • 1
  • 1
    Why do you think a Dictionary with range-keys would reduce the `if()`s ? What is the exact problem (with the known solutions) anyway? – H H Oct 19 '14 at 19:16
  • @DimitrisFousteris, the solution is a sorted list, still doesn't solve my problem of using the if checks many times in a row. If that's unavoidable, is there a super-efficient way to check if an item lies within a given range without if(1 < x <= 1.2345) ? Binary search is fine by me, but the if checks are not –  Oct 19 '14 at 19:17
  • Also because I think I accedently got rid of Dimitris' comment when I marked and then unmarked it as a duplicate: "Possible duplicate of http://stackoverflow.com/questions/2147505/a-dictionary-object-that-uses-ranges-of-values-for-keys " I am not confidant enough to use my C# gold badge to close this question with a single vote. – Scott Chamberlain Oct 19 '14 at 19:21
  • @ScottChamberlain, how could I incorporate a sorted list so that the key (1.3 for example) will quickly retrieve the required element which specifies a range into which 1.3 fits? Thank you! –  Oct 19 '14 at 19:27
  • 1
    I've never heard of someone describe a search on a balanced BST as "the amount of "if" checks grows significantly". The number of comparisons grows as O(log n), which is usually considered slow. – Mike Zboray Oct 19 '14 at 19:28
  • BST is the best option but you can also use Skip Lists. – brz Oct 19 '14 at 19:29
  • The problem I am faced with is a volumetric point location (simillar to panar one), however, I have to do it several times and checking with "if" across several BBSTs has to be improved (potentially with giving up on BST approach) –  Oct 19 '14 at 19:31

1 Answers1

5

Are you asking about interval trees? Interval trees allow you get all the elements on the interval x..y within O(logn) time. For C# implementation I have used the libary called IntervalTreeLib and it worked nicely.

In computer science, an interval tree is an ordered tree data structure to hold intervals. Specifically, it allows one to efficiently find all intervals that overlap with any given interval or point. It is often used for windowing queries, for instance, to find all roads on a computerized map inside a rectangular viewport, or to find all visible elements inside a three-dimensional scene. A similar data structure is the segment tree.

Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
  • It does work nicely with the overlapping intervals, but I wished there was a structure with constant retrieval time given a key against the structure's ranges –  Oct 19 '14 at 19:41
  • @AwkwardSilence To get `O(1)` you need to be able to generate a constant time hashcode to create lookup to find the node in the collection you are trying to find. It is not easy to get working hashcodes for ranges, so without a lot of specialized code for your unique data set the best you can do is `O(log n)` via a binary search. – Scott Chamberlain Oct 19 '14 at 19:45
  • @AwkwardSilence, it really depends of your data and the use case. I'd start with the IntervalTreeLib, and see how it works. Don't forget that logN grows VERY slowly and you might be pleasantly surprised by the speed, don't forget that Dictionary is not really a constant retrieval(collisions). logn(million)=6. I can help you further with few solutions, but you need to answer questions such as: 1) are the ranges non-overlapping, 2) are the ranges static(eg you can't add/remove ranges once the data structure has been built), 3) how much memory would you be trading away for more speed? – Erti-Chris Eelmaa Oct 19 '14 at 19:57
  • @ChrisEelmaa, thank you Chris, In planar point location, because the polygonal areas are convex and are not made from many vertices, the structure doesn't grow in size,- I don't have to use something as persistent BSTs. I can re-build the tree after a new vertex is introduced/removed from the set. My situation doesn't require the ranges to be overlapping and in fact there are no empty values in between of the ranges of elements. At maximum, there are 5-10 elements, so I didn't think BST is the way to go. However, it seems to be the only reasonable approach to check against elements' ranges :/ –  Oct 19 '14 at 20:56
  • @AwkwardSilence it seems still a bit unclear to me. I suggest you to modify your post and add all the details you can come up with. If your whole data set is going to be only 10-15 elements, nothing beats the good old naive linear scan. – Erti-Chris Eelmaa Oct 19 '14 at 21:37