7

I need an IntervalTree or RangeTree implementation in Java, and am having trouble finding one with working deletion support.

There's a built-in one at sun.jvm.hotspot.utilities.IntervalTree, but the deleteNode method in the RBTree superclass states:

/**
 * FIXME: this does not work properly yet for augmented red-black
 * trees since it doesn't update nodes. Need to figure out exactly
 * from which points we need to propagate updates upwards.
 */

Trying to delete nodes from a tree ends up throwing the exception:

Node's max endpoint was not updated properly

How difficult would it be to properly implement delete functionality in a subclass of the sun.jvm.hotspot.utilities.IntervalTree? Or is there another Interval Tree implementation which already implements this correctly?

Currently I'm just wiping out the tree and re-populating it every time there's a deletion, which is far from ideal (note: setting DEBUGGING=false in the RBTree sped things up tremendously).

jedierikb
  • 11,486
  • 17
  • 88
  • 156
Sam Barnum
  • 9,932
  • 3
  • 48
  • 57

5 Answers5

5

I ended up modifying the sun.jvm.hotspot.utilities.IntervalTree to maintain a Set of deleted nodes. When doing a search, I exclude any items in this set. Not ideal, but this was a lot easier than getting "real" deletion working. Once the deleted set gets too large, I rebuild the tree.

Sam Barnum
  • 9,932
  • 3
  • 48
  • 57
1

This project has a RangeTree implementation that might be of more use to you. The sun packages might be ok for quick-and-dirty use, but I would not recommend building anything important relying on them. Sun may not keep them stable.

Yishai
  • 84,976
  • 26
  • 176
  • 250
  • Thanks for the link, Yishai. I'm looking at the docs http://olduvai.sourceforge.net/tj/tj-javadoc-public/TreeJuxtaposer/RangeTree.html and don't see any way to get a list of nodes for a range, or modify the tree once created. It also looks like there's some dependency leakage on the GUI project they're using it with. My guess is this is very specific to that project's needs, and not a general-purpose RangeTree. Have you used this implementation? – Sam Barnum Sep 13 '09 at 19:14
  • @Sam, no I haven't used it. It was just the alternative that I could find. Since it is open source, it might give you a better basis to start with than subclassing the sun implementation. – Yishai Sep 13 '09 at 19:28
0

there's a c# implementation based on an augmented AVL tree @ http://code.google.com/p/intervaltree/ . translation to java shouldn't be too difficult.

cos
  • 87
  • 1
0

I found an open-source implementation with deletion, but it neeeds some effort to make it fully functional.

It's a module of larger open-source project Gephi, but here are the sources and javadoc. If you want a jar you can install the Gephi, and it's in:

/gephi/modules/org-gephi-data-attributes-api.jar

The delete method there, removes all intervals overlapping with the input interval (instead of just the input one). However I found in the soruces that there are private methods that remove a specific node (which stores one interval). Also the private search methods return nodes.

So I think with some little effort it's possible to refactor the code and have this - 'delete specific interval' feature. The fastest and most dirty way would be to just make the private methods/Node class public. But since it's an open source project, maybe it could evolve in future into some good standard implementation of interval tree.

Marcin
  • 755
  • 1
  • 7
  • 12
0

I don't know your exact requirements but a non-static Segment Tree might work for you. If so, have a look at Layout Management SW, specifically the SegmentTree package. It has the remove feature you need.

If you decide to roll your own, might I suggest open sourcing it? I'm surprised there isn't an open and easy Interval Tree available already.

GaryF
  • 22,828
  • 9
  • 56
  • 72