def persistent_rb_delete_fixup(T, S, x): p = pop(S) while x is not T.root and x.color == Black: r = pop(S) if x is p.left: w = rb.ParentlessNode.clone(p.right) p.right = w if w.color == Red: w.color = Black p.color = Red parentless_rb_left_rotate(T, p, r) r = w w = rb.ParentlessNode.clone(p.right) p.right = w if w.left.color == Black and w.right.color == Black: w.color = Red x = p else: if w.right.color == Black: w.left = rb.ParentlessNode.clone(w.left) w.left.color = Black w.color = Red parentless_rb_right_rotate(T, w, p) w = p.right w.color = p.color p.color = Black w.right = rb.ParentlessNode.clone(w.right) w.right.color = Black parentless_rb_left_rotate(T, p, r) x = T.root else: w = rb.ParentlessNode.clone(p.left) p.left = w if w.color == Red: w.color = Black p.color = Red parentless_rb_right_rotate(T, p, r) r = w w = rb.ParentlessNode.clone(p.left) p.left = w if w.left.color == Black and w.right.color == Black: w.color = Red x = p else: if w.left.color == Black: w.right = rb.ParentlessNode.clone(w.right) w.right.color = Black w.color = Red parentless_rb_left_rotate(T, w, p) w = p.left w.color = p.color p.color = Black w.left = rb.ParentlessNode.clone(w.left) w.left.color = Black parentless_rb_right_rotate(T, p, r) x = T.root p = r x.color = Black
def stack_dequeue(S): if stack_empty(S): raise RuntimeError('underflow') S_ = Array.indexed(1, S.length) S_.top = 0 while not stack_empty(S): push(S_, pop(S)) x = pop(S_) while not stack_empty(S_): push(S, pop(S_)) return x
def iterative_inorder_tree_walk(T): S = Array.indexed(1, _get_tree_size(T.root)) S.top = 0 x = T.root while not stack_empty(S) or x is not None: if x is not None: push(S, x) x = x.left else: x = pop(S) print(x.key) x = x.right
def parentless_rb_insert_fixup(T, S, z): p = pop(S) while p.color == Red: r = pop(S) if p is r.left: y = r.right if y.color == Red: y.color = p.color = Black r.color = Red z = r p = pop(S) else: if z is p.right: z, p = p, z parentless_rb_left_rotate(T, z, r) p.color = Black r.color = Red parentless_rb_right_rotate(T, r, pop(S)) else: y = r.left if y.color is Red: y.color = p.color = Black r.color = Red z = r p = pop(S) else: if z is p.left: z, p = p, z parentless_rb_right_rotate(T, z, r) p.color = Black r.color = Red parentless_rb_left_rotate(T, r, pop(S)) T.root.color = Black
def test_pop(self): size = 10 stack, _ = get_random_array(min_size=size, max_size=size) stack.top = random.randint(0, size) if stack.top == 0: assert_that(calling(pop).with_args(stack), raises(RuntimeError, 'underflow')) else: expected_keys = get_stack_elements(stack) del expected_keys[-1] expected_deleted = stack[stack.top] actual_deleted = pop(stack) assert_that(actual_deleted, is_(equal_to(expected_deleted))) actual_keys = get_stack_elements(stack) assert_that(actual_keys, is_(equal_to(expected_keys)))
def activity_scheduler(s, f): n = s.length A = Array.indexed(1, n) F = Array(list(rbetween(n, 1))) F.top = n B = RedBlackTree() # events contains triples (a, b, c) where a = 0 if the event is finish of an activity and 1 if it is start, # b as the activity number, and c as the start time or the finish time events = [(0, i + 1, finish_time) for i, finish_time in enumerate(f)] + \ [(1, i + 1, start_time) for i, start_time in enumerate(s)] events.sort(key=lambda e: (e[2], e[0])) for e in events: if e[0] == 1: hall_number = pop(F) A[e[1]] = hall_number rb_insert(B, Node(e[1], data=hall_number), sentinel=B.nil) else: hall = rb_search(B.root, e[1], sentinel=B.nil) push(F, hall.data) rb_delete(B, hall, sentinel=B.nil) return A
def _persistent_rb_insert_fixup(T, S, z): p = pop(S) while p.color == Red: r = pop(S) if p is r.left: y = r.right if y.color == Red: r.right = rb.ParentlessNode.clone(y) r.right.color = Black p.color = Black r.color = Red z = r p = pop(S) else: if z is p.right: z, p = p, z parentless_rb_left_rotate(T, z, r) p.color = Black r.color = Red parentless_rb_right_rotate(T, r, pop(S)) else: y = r.left if y.color is Red: r.left = rb.ParentlessNode.clone(y) r.left.color = Black p.color = Black r.color = Red z = r p = pop(S) else: if z is p.left: z, p = p, z parentless_rb_right_rotate(T, z, r) p.color = Black r.color = Red parentless_rb_left_rotate(T, r, pop(S)) T.root.color = Black
def effective_stack_dequeue(S1, S2): if stack_empty(S2): while not stack_empty(S1): push(S2, pop(S1)) return pop(S2)
def multipop(S, k): while not stack_empty(S) and k != 0: pop(S) k = k - 1
def huge_array_delete(T, S, x): k = x.key y = pop(S) S[T[k]] = y T[y.key] = T[k]