支持无重叠区间合并的区间树算法

发布于 2024-08-27 15:27:06 字数 438 浏览 6 评论 0原文

我正在寻找一种类似于 CLR 中的红黑间隔树的间隔树算法,但默认情况下支持间隔合并,以便永远不会有任何重叠的间隔。

换句话说,如果您有一棵包含两个区间 [2,3] 和 [5,6] 的树,并且添加了区间 [4,4],则结果将是一棵仅包含一个区间 [2,6] 的树。

谢谢

更新:我正在考虑的用例是计算传递闭包。间隔集用于存储后继集,因为它们发现非常紧凑< /a>.但是,如果将区间集表示为链表,我发现在某些情况下它们可能会变得非常大,因此找到插入点所需的时间也会变得非常大。因此我对区间树感兴趣。此外,可能会有相当多的一棵树与另一棵树的合并(即集合或运算) - 如果两棵树都很大,那么最好使用两棵树的有序遍历来创建一棵新树,而不是每个间隔的重复插入。

I'm looking for an interval tree algorithm similar to the red-black interval tree in CLR but that supports merging of intervals by default so that there are never any overlapping intervals.

In other words if you had a tree containing two intervals [2,3] and [5,6] and you added the interval [4,4], the result would be a tree containing just one interval [2,6].

Thanks

Update: the use case I'm considering is calculating transitive closure. Interval sets are used to store the successor sets because they have been found to be quite compact. But if you represent interval sets just as a linked list I have found that in some situations they can become quite large and hence so does the time required to find the insertion point. Hence my interest in interval trees. Also there may be quite a lot of merging one tree with another (i.e. a set OR operation) - if both trees are large then it may be better to create a new tree using inorder walks of both trees rather than repeated insertions of each interval.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2024-09-03 15:27:06

我看到的问题是插入一个大的区间会消除树的一大块,使得恢复红黑不变量变得困难。

我认为使用展开树会更容易,如下所示。为简单起见,每棵树包含两个哨兵,一个区间 A 位于所有其他区间的左侧,一个区间 Z 位于右侧。插入区间I时,将I的前驱H展开到树的根部。该树看起来像

   H
  / \
...  X
    / \
  ... ...

现在分离 X 并将 I 的后继 J 展开到根。

   H       J
  /       / \
...     ... ...

此时所有与I重叠的区间都在J的左子树中。分离该子树并将其所有节点放入空闲列表中,必要时扩展I。让 I 成为 HJ 的父级

     I
    / \
   H   J
  /     \
...     ...

,并继续我们的快乐之路。此操作是 O(log n) 摊销的,其中 n 是树节点的数量(这可以通过检查展开树势函数并进行大量代数来证明)。


我应该补充一点,通过插入一棵树的根然后合并左右子树,存在自然递归的树到树合并。我不知道如何立即分析它。

The problem I see is that inserting a large interval can wipe out a large chunk of the tree, making it difficult to recover the red-black invariants.

I think it would be easier to use a splay tree, as follows. For simplicity, each tree contains two sentinels, an interval A to the left of all other intervals and an interval Z to the right. When inserting an interval I, splay I's predecessor-to-be H to the root of the tree. The tree looks like

   H
  / \
...  X
    / \
  ... ...

Now detach X and splay I's successor-to-be J to the root.

   H       J
  /       / \
...     ... ...

At this point all of the intervals that overlap I are in J's left subtree. Detach that subtree and put all of its nodes on the free list, extending I if necessary. Make I the parent of H and J

     I
    / \
   H   J
  /     \
...     ...

and continue on our merry way. This operation is O(log n) amortized, where n is the number of tree nodes (this can be proved by examining the splay tree potential function and doing a lot of algebra).


I should add that there's a natural recursive tree-to-tree merge by inserting the root of one tree and then merging the left and right subtrees. I don't know how to analyze it off-hand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文