Data structures 表示不相交段的有序集的结构

Data structures 表示不相交段的有序集的结构,data-structures,Data Structures,我有一组有序的不相交线段 S = {[a1 b1] [a2 b2] ... [an bn]} 带有b1+1

我有一组有序的不相交线段

S = {[a1 b1] [a2 b2] ... [an bn]}
带有
b1+1
等。所有数字都是整数


我正在寻找一种数据结构,用
insert
操作来表示该集合。我知道插入段
[ab]
s
中的段之间没有交集,但是如果
b+1=ak
bk+1=a
对于某些
k

节点的值为一对整数的链表如何。 我们需要一个稍微修改的
insert
操作,我们称之为
insertAndMerge

def insertAndMerge(segment, head) {
    curr = head;
    // find element to insert after
    while (segment.a < curr.b) {
        curr = curr.next;
    }

    // if bk + 1 = a case occurs, merge with existing element, otherwise insert the new one
    if (curr.b + 1 = segment.a) {
        curr.b = segment.b;
    } else {
        segment.next = curr.next;
        curr.next = segment;
        curr = curr.next;
    }

    // the insertion or merge above could trigger the b + 1 = ak case, necessitating a merge with the element after it
    if (curr.b + 1 = curr.next.a) {
        toRemove = curr.next;
        curr.next = toRemove.next;
        curr.b = toRemove.b;
        delete toRemove;
    }
}
def insertAndMerge(段、头){
curr=头;
//查找要在后面插入的元素
而(a段
为了简单起见,我省略了一些小案例,但你明白了。另外,在你目前提出的问题中,不清楚a