Exemplo n.º 1
0
def parentless_rb_insert(T, z):
    path_length = _get_path_length_from_root_to_leaf(T, z)
    S = Array.indexed(1, path_length + 1)
    S.top = 0
    y = T.nil
    push(S, y)
    x = T.root
    while x is not T.nil:
        y = x
        push(S, y)
        if z.key < x.key:
            x = x.left
        else:
            x = x.right
    if y is 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
    parentless_rb_insert_fixup(T, S, z)
Exemplo n.º 2
0
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
Exemplo n.º 3
0
    def test_push(self):
        size = 10
        stack, _ = get_random_array(min_size=size, max_size=size)
        stack.top = random.randint(0, size - 1)
        x = random.randint(0, 999)
        expected_keys = get_stack_elements(stack) + [x]

        push(stack, x)

        actual_keys = get_stack_elements(stack)
        assert_that(actual_keys, is_(equal_to(expected_keys)))
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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
Exemplo n.º 6
0
def persistent_rb_insert(T, z):
    path_length = _get_path_length_from_root_to_node(T, z)
    S = Array.indexed(1, path_length + 1)
    S.top = 0
    y = T.nil
    x = T.root
    T_ = RedBlackTree(sentinel=T.nil)
    y_ = T_.nil
    push(S, y_)
    while x is not T.nil:
        y = x
        x_ = rb.ParentlessNode.clone(x)
        if y_ is T_.nil:
            T_.root = x_
        else:
            if x is y_.left:
                y_.left = x_
            else:
                y_.right = x_
        y_ = x_
        push(S, y_)
        if z.key < x.key:
            x = x.left
        else:
            x = x.right
    if y is T.nil:
        T_.root = z
    else:
        if z.key < y.key:
            y_.left = z
        else:
            y_.right = z
    z.left = z.right = T.nil
    z.color = Red
    _persistent_rb_insert_fixup(T_, S, z)
    return T_
Exemplo n.º 7
0
def effective_stack_dequeue(S1, S2):
    if stack_empty(S2):
        while not stack_empty(S1):
            push(S2, pop(S1))
    return pop(S2)
Exemplo n.º 8
0
def effective_stack_enqueue(S1, x):
    push(S1, x)
Exemplo n.º 9
0
def huge_array_insert(T, S, x):
    push(S, x)
    T[x.key] = S.top
Exemplo n.º 10
0
def persistent_rb_delete(T, z):
    T_ = RedBlackTree()
    T_.root = T_.nil = T.nil
    if z.left is T.nil or z.right is T.nil:
        y = z
    else:
        y = rb_successor(z, T.nil)
    path_length = _get_path_length_from_root_to_node(T, y)
    S = Array.indexed(1, path_length + 1)
    S.top = 0
    p = T.root
    r = T.nil
    p_ = r_ = T_.nil
    push(S, p_)
    z_ = T.nil
    while p is not y:
        p_ = rb.ParentlessNode.clone(p)
        push(S, p_)
        if p is z:
            z_ = p_
        if r_ is T_.nil:
            T_.root = p_
        else:
            if p is r_.left:
                r_.left = p_
            else:
                r_.right = p_
        r = p
        r_ = p_
        if y.key < p.key:
            p = p.left
        else:
            p = p.right
    if y.left is not T.nil:
        x = y.left
    else:
        x = y.right
    if y.color == Black:
        if x is not T.nil:
            x_ = rb.ParentlessNode.clone(x)
        else:
            x_ = T.nil
        if y is T.root:
            T_.root = x_
        else:
            if y is r.left:
                p_.left = x_
            else:
                p_.right = x_
        if y is not z:
            z_.key = y.key
            z_.data = y.data
        persistent_rb_delete_fixup(T_, S, x_)
    else:
        if y is r.left:
            p_.left = x
        else:
            p_.right = x
        if y is not z:
            z_.key = y.key
            z_.data = y.data
    return T_
Exemplo n.º 11
0
def stack_enqueue(S, x):
    push(S, x)