INTERVAL-LEFT-ROTATE(T, x) y = x.right x.right = y.left if y.left != T.nill y.left.p = x y.p = x.p if x.p == T.nil T.root = y else if x == x.p.left x.p.left = y else x.p.right = y y.left = x x.p = y y.max = x.max x.max = max{x.left.max, x.int.high, x.right.max}
INTERVAL-SEARCH'(T, i) y = T.nil x = T.root while x != T.nil if i overlap x.int y = x // 更优秀的只会在x的左子树中。 x = x.left else if x.left != T.nil and x.left.max >= i.low x = x.left else x = x.right
GEN-INTERVAL(T, x, i) if i overlap x.int INSERT(L, x.int) if x.left != T.nil and x.left.max >= i.low GEN-INTERVAL(T, x.left, x) if x.right != T.nil and x.int.low <= i.high and x.right.max >= i.low GEN-INTERVAL(T, x.right, x) INTERVAL-ENUMERATE-OVERLAP(T, i) // L是一个全局变量,用来存储所有被遍历的区间。 Let L be an array GEN-INTERVAL(T, T.root, i) return L
INTERVAL-SEARCH-EXACTLY(T, i) x = T.root while x != T.nil and not(i.low == x.int.low and i.high == x.int.high) if i.low < x.int.low or (i.low == x.int.low and i.high < x.int.high) x = x.left else x = x.right return x
RB-INSERT-MINGAP(T, z) x = T.root y = T.nil while x != T.nil y = x if z.key < x.key x = x.left else x = x.right z.p = y if y == T.nil T.root = z else if z.key < y.key y.left = z else y.right = z z.left = T.nil z.right = T.nil z.color = RED z.mingap = ∞ // 先维护好其前驱和后继 x = z y = x.p while y != T.nil and z == x.right x = y y = y.p s = y p = s.prev p.succ = z s.prev = z w = z while w != T.nil RB-UPDATE-MINGAP(T, w) w = w.p OS-INSERT-FIXUP-MINGAP(T, w)
OS-DELETE-MINGAP(T, z) y = z y-original-color = y.color if z.left == T.nil x = z.right RB-TRANSPLANT(T, z, z.right) else if z.right == T.nil x = z.left RB-TRANSPLANT(T, z, z.left) else y = RB-MINIMUM(T, z.right) y-original-color = y.color x = y.right if y != z.right RB-TRANSPLANT(T, y, y.right) y.right = z.right y.right.p = y else x.p = y RB-TRANSPLANT(T, z, y) y.left = z.left y.rank' = z.rank' y.left.p = y y.color = z.color w = x while w != T.nil RB-UPDATE-MINGAP(T, w) w = w.p if y-original-color == BLACK OS-DELETE-FIXUP-MINGAP(T, x)
CHECK-RECTANGLE-INTERSEC(R, n) let E be a new array for i = 1 to n if C[i].x > C[i].X exchange C[i].x with C[i].X if C[i].y > C[i].Y exchange C[i].y with C[i].Y let e, e' be new edges let z be a new node z.int.low = C[i].y z.int.high = C[i].Y e.x, e.z, e.type = C[i].x, z, 1 e'.x, e'.z, e'.type = C[i].X, z, -1 INSERT(E, e) INSERT(E, e') sort E using this rule : e.x < e'.x or (e.x == e'.x and e.type < e'.eype) Let T be a new interval tree for e in E if e.type == 1 if INTERVAL-SEARCH(T, x.int) != T.nil return True else INTERVAL-INSERT(T, x) else INTERVAL-DELETE(T, x) return False