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