コード例 #1
0
ファイル: immutable_treap.py プロジェクト: agroce/cs562w16
    def __merge(A, B):
        if A is None:
            return B
        if B is None:
            return A

        if A.priority < B.priority:
            R = TreapNode(node=B)
            R.left = ImmutableTreap.__merge(A, B.left)
        else:
            R = TreapNode(node=A)
            R.right = ImmutableTreap.__merge(A.right, B)

        return R
コード例 #2
0
ファイル: immutable_treap.py プロジェクト: agroce/cs562w16
    def __insert(node, value, priority=None):
        if node is None:
            return TreapNode(value, priority)

        new = TreapNode(node=node)

        if new.value < value:
            new.right = ImmutableTreap.__insert(new.right, value, priority)
            if new.right.priority > new.priority:
                new = ImmutableTreap.__rotateLeft(new)
        else:
            new.left = ImmutableTreap.__insert(new.left, value, priority)
            if new.left.priority > new.priority:
                new = ImmutableTreap.__rotateRight(new)

        return new
コード例 #3
0
    def __insert(node, value, priority=None):
        if node is None:
            return TreapNode(value, priority)

        if node.value < value:
            node.right = MutableTreap.__insert(node.right, value, priority)
            if node.right.priority > node.priority:
                node = Treap.__rotateLeft(node)
        else:
            node.left = MutableTreap.__insert(node.left, value, priority)
            if node.left.priority > node.priority:
                node = Treap.__rotateRight(node)

        return node
コード例 #4
0
ファイル: immutable_treap.py プロジェクト: agroce/cs562w16
    def __delete(node, value):
        if node.left is None and node.right is None:
            return None

        new = TreapNode(node=node)

        if node.value > value:
            new.left = ImmutableTreap.__delete(new.left, value)
        elif node.value < value:
            new.right = ImmutableTreap.__delete(new.right, value)
        elif Treap.cmp(node.left, node.right) > 0:
            new.right = TreapNode(node=new.right)
            new = Treap.__rotateLeft(new)
            new.left = ImmutableTreap.__delete(new.left, value)
        else:
            new.left = TreapNode(node=new.left)
            new = Treap.__rotateRight(new)
            new.right = ImmutableTreap.__delete(new.right, value)

        return new