Ejemplo n.º 1
0
def splay(node, val, for_insert = False):
    left, left_right_most = None, None
    right, right_left_most = None, None
    while node:
        if val == node.value and not for_insert:
            break

        if val < node.value:
            if not node.left:
                if not for_insert:
                    break
            if node.left and val < node.left.value and node.left.left: #zig-zig
                child, grandchild = node.left, node.left.left
                child.right, node.left = node, child.right
                new_left_most= child
                node = grandchild
            else: #zig
                new_left_most = node
                node = node.left
            if not right_left_most:
                right = right_left_most = new_left_most
            else:
                right_left_most.left, new_left_most.left, right_left_most = (
                    new_left_most, None, new_left_most)
            continue
        if not node.right and not for_insert:
            break
        if node.right and val > node.right.value and node.right.right: #zig-zig
            child, grandchild = node.right, node.right.right
            child.left, node.right = node, child.left
            node = grandchild
            new_right_most= child
        else: #zig
            new_right_most = node
            node = node.right

        if not left_right_most:
            left = left_right_most = new_right_most
        else:
            left_right_most.right, new_right_most.right, left_right_most = (
                new_right_most, None, new_right_most)
    if not node:
        if not for_insert:
            return None
        node = Node(val)
    if left_right_most:
        left_right_most.right = node.left
    else:
        left = node.left
    if right_left_most:
        right_left_most.left = node.right
    else:
        right = node.right
        
    node.left, node.right = left, right
    return node
Ejemplo n.º 2
0
def splay(node, val, for_insert=False):
    left, left_right_most = None, None
    right, right_left_most = None, None
    while node:
        if val == node.value and not for_insert:
            break

        if val < node.value:
            if not node.left:
                if not for_insert:
                    break
            if node.left and val < node.left.value and node.left.left:  #zig-zig
                child, grandchild = node.left, node.left.left
                child.right, node.left = node, child.right
                new_left_most = child
                node = grandchild
            else:  #zig
                new_left_most = node
                node = node.left
            if not right_left_most:
                right = right_left_most = new_left_most
            else:
                right_left_most.left, new_left_most.left, right_left_most = (
                    new_left_most, None, new_left_most)
            continue
        if not node.right and not for_insert:
            break
        if node.right and val > node.right.value and node.right.right:  #zig-zig
            child, grandchild = node.right, node.right.right
            child.left, node.right = node, child.left
            node = grandchild
            new_right_most = child
        else:  #zig
            new_right_most = node
            node = node.right

        if not left_right_most:
            left = left_right_most = new_right_most
        else:
            left_right_most.right, new_right_most.right, left_right_most = (
                new_right_most, None, new_right_most)
    if not node:
        if not for_insert:
            return None
        node = Node(val)
    if left_right_most:
        left_right_most.right = node.left
    else:
        left = node.left
    if right_left_most:
        right_left_most.left = node.right
    else:
        right = node.right

    node.left, node.right = left, right
    return node
Ejemplo n.º 3
0
def make_node(value, parent=None, left=None, right=None):
    node = Node(value, left, right)
    node.parent = parent
    return node
Ejemplo n.º 4
0
def make_node(value, parent=None, left=None, right=None):
    node = Node(value, left, right)
    node.parent = parent
    return node