我正在阅读Cormen 的《算法导论》第 14 章(增强数据结构),他在其中谈到了区间树。下面是他提到的区间树背后的设计方法。
第一步:底层数据结构
我们选择一棵红黑树,其中每个节点x包含一个区间x:int , x的键是该区间的低端点 x.int.low。因此,数据结构的中序树遍历按低端点排序的顺序列出了区间。
这可以通过声明一个具有min和max的节点来完成。compareTo函数应该只比较x.int.low。
Step 2: Additional information
In addition to the intervals themselves, each node x contains a value x.max, which is the maximum value of any interval endpoint stored in the sub-tree rooted at x.
Step 3: Maintaining the information
We must verify that insertion and deletion take O(lg n) time on an interval tree of n nodes. We can determine x.max given interval x.int and the max values of node x’s children:
x:max = max(x.int.high; x.left.max; x.right.max)
Step 4: Developing new operations
The only new operation we need is
INTERVAL-SEARCH
(T,i), which finds a node in tree T whose interval overlaps interval i. If there is no interval that overlaps i in the tree, the procedure returns a pointer to the sentinel T:nil.
I can implement this by AVL tree but out of curiosity want to know whether we can augment existing libraries in java like TreeSet or other collection entity to fit to above design. If so, can you please help in a sample code or example?