I read several slides, like this one's last page, where the describe the search algorithm. However, I have a basic question. The data lie in a 2D space.
I first build a Binary Search Tree based on the x value of the points. Every inner node holds a BST based on the y value of the points that lie in the subtree of that inner node.
Then I think I should search for the points that lie in the range query [x1, x2] and then check if for that points the requested [y1, y2] range query is satisfied. However, the algorithm suggests that you should search in the y-based BST of an inner node, if the range of the inner node is inside [x1, x2], but I don't get that.
If we do that, then in an example I have, we will search (without a reason) the y-based BST of the root. Check the example:
------ 0 ---------------------
| |
---- -3 ---- ---- 4 ------
| | | |
---- -4 - -2 --- 3 --- 5
| | / \ | | / \
-5 (-3,4) (-2,2)(0,7) 2 (4,-4) (5,3)(6,-1)
/ \ / \
(-5,6) (-4,0) (2,1) (3,6)
And the range query I wish to perform is (-oo, 1) x (0, 5)*.
If I look at the root, it has value 0, thus it's enclosed in (-oo, 1), so if I follow the algorithm I am going to search the whole y-based tree of the root?
That should be a tree that contains all the points, so there is no point in continuing searching in x-based tree. Moreover, that will result in more visited nodes than the necessary.
I am implementing that in c++, if that matters.
*Performing a range query for x's in the range [-inf, 1] and y's in the range [0, 5].