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
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
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
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