I'm writing an implementation of an R-Tree based on Guttman's original paper. I was thinking about using an R-Tree for a program I'm writing that involves a lot of rectangles on the screen that can be moved/re-sized with the mouse.
I want to efficiently only select rectangles that are in a particular rectangle and draw them (instead of iterating through potentially 100+ items and checking if bounds intersect). The problem that I'm finding after a couple reads of Guttman's paper is that z-order can't be maintained for 2D objects.
For example, if I move an object, it'd be deleted and then re-inserted. When it's re-inserted, the node it's inserted to wouldn't be able to keep track of the proper order. Most implementation's of an R-Tree that I've seen use an array and just find the empty position. The re-insertion would essentially destroy any z-order positioning.
So when I go to draw all rectangles that intersect with a rectangle, the order they're returned isn't necessarily correct.
Am I wrong on this assumption? I was thinking instead of using an array, I could use an AVL or Red-Black tree and use a Comparer
that compares on z-index to insert into the tree. This way, z-order is always maintained (and it's the most important factor).
I was also just thinking of sorting them when they're returned, but this could be more expensive I'm thinking.