Problem:

Treap

  A binary search tree can be unbalanced depending on features of data. For example, if we insert $n$ elements into a binary search tree in ascending order, the tree become a list, leading to long search times. One of strategies is to randomly shuffle the elements to be inserted. However, we should consider to maintain the balanced binary tree where different operations can be performed one by one depending on requirement.

  We can maintain the balanced binary search tree by assigning a priority randomly selected to each node and by ordering nodes based on the following properties. Here, we assume that all priorities are distinct and also that all keys are distinct.

  
binary-search-tree property. If $v$ is a left child of $u$, then $v.key right child of $u$, then $u.key heap property. If $v$ is a child of $u$, then $v.priority 

  Insert

  To insert a new element into a Treap, first of all, insert a node which a randomly selected priority value is assigned in the same way for ordinal binary search tree. For example, the following figure shows the Treap after a node with key = 6 and priority = 90 is inserted.

It is clear that this Treap violates the heap property, so we need to modify the structure of the tree by rotate operations. The rotate operation is to change parent-child relation while maintaing the binary-search-tree property.

  The rotate operations can be implemented as follows.

  

    

rightRotate(Node t)
    Node s = t.left
    t.left = s.right
    s.right = t
    return s // the new root of subtree

leftRotate(Node t)
    Node s = t.right
    t.right = s.left
    s.left = t
    return s // the new root of subtree

  The following figure shows processes of the rotate operations after the insert operation to maintain the properties.

  The insert operation with rotate operations can be implemented as follows.

insert(Node t, int key, int priority)            // search the corresponding place recursively
    if t == NIL
        return Node(key, priority)               // create a new node when you reach a leaf
    if key == t.key
        return t                                 // ignore duplicated keys

    if key Delete

  To delete a node from the Treap, first of all, the target node should be moved until it becomes a leaf by rotate operations. Then, you can remove the node (the leaf). These processes can be implemented as follows.

delete(Node t, int key)                        // seach the target recursively
    if t == NIL
        return NIL
    if key  t.key
        t.right = delete(t.right, key)
    else
        return _delete(t, key)
    return t

_delete(Node t, int key)                       // if t is the target node
    if t.left == NIL && t.right == NIL         // if t is a leaf
        return NIL
    else if t.left == NIL                      // if t has only the right child, then perform left rotate
        t = leftRotate(t)
    else if t.right == NIL                     // if t has only the left child, then perform right rotate
        t = rightRotate(t)
    else                                       // if t has both the left and right child
        if t.left.priority > t.right.priority  // pull up the child with higher priority
            t = rightRotate(t)
        else
            t = leftRotate(t)
    return delete(t, key)

  Write a program which performs the following operations to a Treap $T$ based on the above described algorithm.

insert ($k$, $p$): Insert a node containing $k$ as key and $p$ as priority to $T$.

find ($k$): Report whether $T$ has a node containing $k$.

delete ($k$): Delete a node containing $k$.

print(): Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.

Input

    

In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k \; p$, find $k$, delete $k$ or print are given.

Output

For each find($k$) operation, print "yes" if $T$ has a node containing $k$, "no" if not.

  In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key.

Constraints

The number of operations $ \leq 200,000$

$0 \leq k, p  \leq 2,000,000,000$

The height of the binary tree does not exceed 50 if you employ the above algorithm

The keys in the binary search tree are all different.

The priorities in the binary search tree are all different.

The size of output $\leq 10$ MB

Sample Input 1

16
insert 35 99
insert 3 80
insert 1 53
insert 14 25
insert 80 76
insert 42 3
insert 86 47
insert 21 12
insert 7 10
insert 6 90
print
find 21
find 22
delete 35
delete 99
print

Sample Output 1

 1 3 6 7 14 21 35 42 80 86
 35 6 3 1 14 7 21 80 42 86
yes
no
 1 3 6 7 14 21 42 80 86
 6 3 1 80 14 7 21 42 86

Reference

  
Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

  
Randomized Search Trees, R. Seidel, C.R. Aragon.



Sample input:
16
insert 35 99
insert 3 80
insert 1 53
insert 14 25
insert 80 76
insert 42 3
insert 86 47
insert 21 12
insert 7 10
insert 6 90
print
find 21
find 22
delete 35
delete 99
print


Sample output:
 1 3 6 7 14 21 35 42 80 86
 35 6 3 1 14 7 21 80 42 86
yes
no
 1 3 6 7 14 21 42 80 86
 6 3 1 80 14 7 21 42 86

Submission status: Accepted

Solution code:
class Node():
    def __init__(self, key, priority, parent, left, right):
        self.key = key
        self.priority = priority
        self.parent = None
        self.left = None
        self.right = None

def RightRotate(t):
    s = t.left
    t.left = s.right
    s.right = t
    return s

def LeftRotate(t):
    s = t.right
    t.right = s.left
    s.left = t
    return s

def insert(t, key, priority):
    if t == None:
        return Node(key, priority, None, None, None)
    
    if key == t.key:
        return t
    
    if key < t.key:
        t.left = insert(t.left, key, priority)
        if t.priority < t.left.priority :
            t = RightRotate(t)
    
    if key > t.key:
        t.right = insert(t.right, key, priority)
        if t.priority < t.right.priority:
            t = LeftRotate(t)

    return t

def delete(t, key):
    if t == None:
        return None
    if key < t.key:
        t.left = delete(t.left, key)
    elif key > t.key:
        t.right = delete(t.right, key)
    else:
        return _delete(t, key)
    
    return t

def _delete(t, key):
    if t.left == None and t.right == None:
        return None
    elif t.left == None:
        t = LeftRotate(t)
    elif t.right == None:
        t = RightRotate(t)
    else:
        if t.left.priority > t.right.priority:
            t = RightRotate(t)
        else:
            t = LeftRotate(t)
    return delete(t, key)

def find(t, key):
    if t == None:
        print("no")
        return 

    if key == t.key:
        print("yes")
    elif key < t.key:
        find(t.left, key)
    elif key > t.key:
        find(t.right, key)

def print_inorder(t):
    if t == None:
        return
    
    print_inorder(t.left)
    print(" "+str(t.key), end = "")
    print_inorder(t.right)

def print_preorder(t):
    if t == None:
        return 
    print(" "+str(t.key), end = "")
    print_preorder(t.left)
    print_preorder(t.right)

def Main():
    N = int(input())
    t = None

    for i in range(N):
        ope, *arg = input().split()

        if ope == "insert":
            key, priority = map(int, arg)
            t = insert(t, key, priority)

        elif ope == "delete":
            key = int(arg[0])
            t = delete(t, key)

        elif ope == "find":
            key = int(arg[0])
            find(t, key)

        elif ope == "print":
            print_inorder(t)
            print()
            print_preorder(t)
            print()

Main()

Submission status: Accepted

Solution code:
from random import random
from collections import deque
from copy import deepcopy
import sys
input = sys.stdin.readline

class Treap:
    def __init__(self, iterable=None):
        # vertex = [left, right, key, priority, #descendants, sum of descendants]
        self.root = None
        if iterable: self._construct(iterable)
    
    def _construct(self, iterable):
        for it in iterable: self.insert(it)
    
    def __len__(self): return self._count(self.root)
    
    @staticmethod
    def _count(v): return v[4] if v is not None else 0

    def _rotate(self, v, direction): # rotate the vertex v to the given direction
        c = v[1 - direction] # direction == 1: right rotation
        v[1 - direction], t = c[direction], c[1 - direction]
        c[direction] = v
        # update vertex's information
        n_desc, sum_desc = c[4:] = v[4:]
        v[4:] = n_desc - 1 - self._count(t), sum_desc - (c[2] if c[2] else 0) - (t[5] if t else 0)
        return c # new parent
    
    def __contains__(self, key): # check whether the given key is in the treap
        if self.root is None: return False
        v = self.root
        while v:
            k = v[2]
            if k == key: return True
            v = v[key > k] # key > v[2] -> v = v[1] (right child)
        return False
    
    def __getitem__(self, i): # get the i-th smallest element in the treap
        count = self._count
        if i < 0: i = count(self.root) + i
        if i >= count(self.root) or i < 0: raise IndexError
        v = self.root
        while True:
            n_left = count(v[0])
            if i == n_left: return v[2]
            elif i > n_left: i -= n_left + 1; v = v[1]
            else: v = v[0]
    
    def __repr__(self): # visualize the treap
        if not self.root: return 'Treap([])'
        elements = []
        q = deque([self.root])
        while q:
            v = q.popleft()
            if v is None: elements.append(v); continue
            elements.append((v[2], v[3]))
            q.append(v[0]); q.append(v[1])
        return 'Treap({})'.format(str(elements))
    
    @classmethod
    def _treap(cls, treapnode):
        tp = cls(); tp.root = deepcopy(treapnode)
        return tp
            
    def sort(self):
        if not self.root: return []
        elements = []
        stack = [(self.root, 0)]
        while stack:
            v, st = stack.pop()
            [left, right, k, _, _, _] = v
            if st == 0:
                if right: stack.append((right, 0))
                stack.append((v, 1))
                if left: stack.append((left, 0))
            if st == 1: elements.append(k)
        return elements
    
    def bisect(self, key): # bisect_right
        if self.root is None: return 0
        v = self.root; idx = 0
        count = self._count
        while v:
            left, right, k, _, _, _ = v
            if key >= k: idx += count(left) + 1; v = right
            else: v = left
        return idx
    
    def insert(self, key, priority=None):
        if priority is None: priority = random()
        rotate = self._rotate
        v = self.root; p = None; direction = None
        stack = []
        while v:
            direction = (key >= v[2])
            stack.append((v, direction))
            v, p = v[direction], v
        v = [None, None, key, priority, 1, key]
        while stack:
            p, direction = stack.pop()
            p[direction] = v
            p[4] += 1; p[5] += key # update vertex's information
            if p[3] >= v[3]: break
            v = rotate(p, 1 - direction)
        else: self.root = v
        for p, _ in stack: p[4] += 1; p[5] += key # update vertex's information
        return self.root
    
    def delete(self, key):
        v = self.root; p = None; direction = None
        stack = []
        while v:
            if key == v[2]: break # vertex to be deleted has been found
            direction = (key > v[2])
            stack.append((v, direction))
            v, p = v[direction], v
        else: return self.root # the given key is not in the treap
        rotate = self._rotate
        while v[0] or v[1]: # while v is not a leaf
            left, right, _, _, _, _ = v
            direction = (left[3] > right[3]) if left and right else (right is None)
            p = rotate(v, direction)
            stack.append((p, direction)); v = p[direction]
        v = None # delete
        while stack:
            p, direction = stack.pop()
            p[direction] = v
            p[4] -= 1; p[5] -= (key if key else 0) # update vertex's information
            v = p
        self.root = v
        return self.root
    
    def merge(self, other):
        r, o = self.root, other.root
        temp_v = [r, o, None, float('inf'), 1 + r[4] + o[4], r[5] + o[5]]
        virtual = self._treap(temp_v)
        self.root = virtual.delete(None)
        return self.root
    
    def split(self, i):
        count = self._count
        if i < 0: i = count(self.root) + i
        if i >= count(self.root) or i < 0: raise IndexError
        rotate = self._rotate
        v = self.root; p = None; direction = None
        stack = []
        while v:
            n_left = count(v[0])
            direction = (i > n_left)
            stack.append((v, direction))
            if direction: i -= n_left + 1
            v, p = v[direction], v
        v = [None, None, None, float('inf'), 1, 0]
        while stack:
            p, direction = stack.pop()
            p[direction] = v
            p[4] += 1; p[5] += 0 # update vertex's information
            v = rotate(p, 1 - direction)
        l, r = v[:2]
        self.root = l
        return self._treap(r)

def print_treap(tr):
    preorder = ['']
    inorder = ['']
    def dfs(vtx):
        v = str(vtx[2])
        preorder.append(v)
        if vtx[0]: dfs(vtx[0])
        inorder.append(v)
        if vtx[1]: dfs(vtx[1])
    dfs(tr.root)
    print(' '.join(inorder))
    print(' '.join(preorder))
    return
    
m = int(input())
tr = Treap()
for _ in range(m):
    query, *args, = input().split()
    if query == 'insert':
        k, p = map(int, args)
        tr.insert(k, p)
    elif query == 'find':
        k = int(args[0])
        print('yes' if k in tr else 'no')
    elif query == 'delete':
        k = int(args[0])
        tr.delete(k)
    else:
        print_treap(tr)

Submission status: Accepted

Solution code:
# coding: utf-8

class TreapNode:
    def __init__(self, id, priority):
        self.id = id
        self.priority = priority
        self.left = None
        self.right = None
    
    def __str__(self):
        return 'id={}, priority={}'.format(self.id, self.priority)


    def _rotate_left(self):
        r = self.right
        self.right = r.left
        r.left = self
        return r
    
    def _rotate_right(self):
        l = self.left
        self.left = l.right
        l.right = self
        return l
    
    
    def _delete_node(self):
        if self.left is None and self.right is None:
            return None
        elif self.left is None:
            node = self._rotate_left()
        elif self.right is None:
            node = self._rotate_right()
        else:
            if self.left.priority < self.right.priority:
                node = self._rotate_left()
            else:
                node = self._rotate_right()
                
        return node.delete(self.id)

        
    def insert(self, id, priority):
        if self.id == id:
            return self
            
        if id < self.id:
            if self.left:
                self.left = self.left.insert(id, priority)
            else:
                self.left = TreapNode(id, priority)
            
            if self.priority < self.left.priority:
                return self._rotate_right()
        else:
            if self.right:
                self.right = self.right.insert(id, priority)
            else:
                self.right = TreapNode(id, priority)
                
            if self.priority < self.right.priority:
                return self._rotate_left()
                
        return self

    def delete(self, id):
        if id < self.id:
            if self.left:
                self.left = self.left.delete(id)
        elif id > self.id:
            if self.right:
                self.right = self.right.delete(id)
        else:
            return self._delete_node()
            
        return self
    
                
    def inorder(self, result):
        if self.left:
            self.left.inorder(result)
            
        result.append(self.id)
        
        if self.right:
            self.right.inorder(result)
    
    
    def preorder(self, result):
        result.append(self.id)
        
        if self.left:
            self.left.preorder(result)
            
        if self.right:
            self.right.preorder(result)

    def find(self, id):
        if self.id == id:
            return True
        if id < self.id:
            if self.left and self.left.find(id):
                return True
        else:
            if self.right and self.right.find(id):
                return True
        return False

class Treap:
    def __init__(self):
        self.root = None
    
    def insert_node(self, id, priority):
        if self.root:
            self.root = self.root.insert(id, priority)
        else:
            self.root = TreapNode(id, priority)
    
    def delete_node(self, id):
        if self.root:
            self.root = self.root.delete(id)
    
    def find_node(self, id):
        if self.root:
            return self.root.find(id)
        else:
            return False
            
    def walk_inorder(self):
        result = []
        if self.root:
            self.root.inorder(result)
        return result
    
    def walk_preorder(self):
        result = []
        if self.root:
            self.root.preorder(result)
        return result
    

def main():
    n = int(input())
    treap = Treap()
    
    for _ in range(n):
        cmd = input()
        if cmd[0] == 'p':
            result = treap.walk_inorder()
            print('', *result)
            
            result = treap.walk_preorder()
            print('', *result)

        elif cmd[0] == 'i':
            id, priority = [int(i) for i in cmd.split()[1:3]]
            treap.insert_node(id, priority)
            
        elif cmd[0] == 'd':
            id = int(cmd.split()[1])
            treap.delete_node(id)
            
        elif cmd[0] == 'f':
            id = int(cmd.split()[1])
            if treap.find_node(id):
                print('yes')
            else:
                print('no')

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
import sys
readline = sys.stdin.readline
write = sys.stdout.write

def rotate(nd, d):
    c = nd[d]
    if d:
        nd[1] = c[0]
        c[0] = nd
    else:
        nd[0] = c[1]
        c[1] = nd
    return c


root = None
def insert(val, pri):
    global root
    st = []
    dr = []
    x = root
    while x:
        st.append(x)
        if x[2] == val:
            return
        d = (x[2] < val)
        dr.append(d)
        x = x[d]
    nd = [None, None, val, pri]
    while st:
        x = st.pop(); d = dr.pop()
        c = x[d] = nd
        if x[3] >= c[3]:
            break
        rotate(x, d)
    else:
        root = nd

def __delete(nd):
    st = []; dr = []
    while nd[0] or nd[1]:
        l = nd[0]; r = nd[1]
        d = (l[3] <= r[3]) if l and r else (l is None)
        st.append(rotate(nd, d))
        dr.append(d ^ 1)
    nd = x = None
    while st:
        nd = x; x = st.pop(); d = dr.pop()
        x[d] = nd
    return x

def delete(val):
    global root
    x = root

    y = None
    while x:
        if val == x[2]:
            break
        y = x; d = (x[2] < val)
        x = x[d]
    else:
        return

    if y:
        y[d] = __delete(x)
    else:
        root = __delete(x)

def find(val):
    x = root
    while x:
        if val == x[2]:
            return 1
        x = x[x[2] < val]
    return 0

def debug():
    s0 = [""]
    s1 = [""]

    def dfs(nd):
        v = str(nd[2])
        s0.append(v)
        if nd[0]:
            dfs(nd[0])
        s1.append(v)
        if nd[1]:
            dfs(nd[1])
    dfs(root)
    return " ".join(s1), " ".join(s0)


M = int(readline())
ans = []
for m in range(M):
    cmd, *V, = readline().split()
    if cmd == "print":
        ans.extend(debug())
    elif cmd == "find":
        ans.append("yes" if find(int(V[0])) else "no")
    elif cmd == "delete":
        delete(int(V[0]))
    else:
        val, pri = map(int, V)
        insert(val, pri)
write("\n".join(ans))
write("\n")

Submission status: Accepted

Solution code:
class Node:
    def __init__(self, key, pri):
        self.left = None
        self.right = None
        self.key = key
        self.pri = pri

    def print_pre(self):
        yield self.key
        if self.left:
            for k in self.left.print_pre(): yield k
        if self.right:
            for k in self.right.print_pre(): yield k

    def print_in(self):
        if self.left:
            for k in self.left.print_in(): yield k
        yield self.key
        if self.right:
            for k in self.right.print_in(): yield k

class Tree:
  root = None

  def rightRotate(self, node):
    s = node.left
    node.left = s.right
    s.right = node
    if self.root == node:
      self.root = s
    return s

  def leftRotate(self, node):
    s = node.right
    node.right = s.left
    s.left = node
    if self.root == node:
      self.root = s
    return s

  def insert1(self, node, key, pri):
    if node == None:
      return Node(key, pri)
    elif key == node.key:
      return node

    elif key < node.key:
      node.left = self.insert1(node.left, key, pri)
      if node.pri < node.left.pri:
        node = self.rightRotate(node)
    else:
      node.right = self.insert1(node.right, key, pri)
      if node.pri < node.right.pri:
        node = self.leftRotate(node)
    return node

  def insert(self, key, pri):
    self.insert1(self.root, key, pri)
    if self.root == None:
      self.root = Node(key, pri)

  def delete(self, node, key):
    if node == None:
      return None
    if key < node.key:
      node.left = self.delete(node.left, key)
    elif key > node.key:
      node.right = self.delete(node.right, key)
    else:
      return self.delete1(node, key)
    return node

  def delete1(self, node, key):
    if node.left == None and node.right == None:
      return None
    elif node.left == None:
      node = self.leftRotate(node)
    elif node.right == None:
      node = self.rightRotate(node)
    else:
      if node.left.pri > node.right.pri:
        node = self.rightRotate(node)
      else:
        node = self.leftRotate(node)
    return self.delete(node, key)

  def find(self, key):
        y = None
        x = self.root
        while x:
            y = x
            if key == x.key:
              break
            elif key < x.key:
              x = x.left
            else:
              x = x.right
            
        if key == y.key:
            return y

  def print(self):
        print(' ', end='')
        print(*self.root.print_in())
        print(' ', end='')
        print(*self.root.print_pre())

tree = Tree()
m = int(input())

while m:
  l = input().split()
  if len(l) == 3:
    tree.insert(int(l[1]), int(l[2]))
  elif len(l) == 2 and l[0] == "find":
    y = tree.find(int(l[1]))
    if y:
      print("yes")
    else:
      print("no")
  elif len(l) == 2 and l[0] == "delete":
    tree.delete(tree.root, int(l[1]))
  else:
    tree.print()
  m -= 1

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
print = sys.stdout.write

class Node:
    __slots__ = ["key", "priority", "left", "right"]
    def __init__(self, key, priority):
        self.key = key
        self.priority = priority
        self.left = None
        self.right = None
        
class Treap:
    def insert(self, node, key, priority):
        if node is None:
            return Node(key, priority)
        if key == node.key:
            return node
        if key < node.key:
            node.left = self.insert(node.left, key, priority)
            if node.priority < node.left.priority:
                node = right_rotate(node)
        else:
            node.right = self.insert(node.right, key, priority)
            if node.priority < node.right.priority:
                node = left_rotate(node)
        return node

    def delete(self, node, key):
        if node is None:
            return None
        if key < node.key:
            node.left = self.delete(node.left, key)
        elif key > node.key:
            node.right = self.delete(node.right, key)
        else:
            return self._delete(node, key)
        return node
    
    def _delete(self, node, key):
        if node.left is None and node.right is None:
            return None
        if node.left is None:
            node = left_rotate(node)
        elif node.right is None:
            node = right_rotate(node)
        else:
            if node.left.priority > node.right.priority:
                node = right_rotate(node)
            else:
                node = left_rotate(node)
        return self.delete(node, key)
    
    def find(self, node, key):
        if node is None:
            print("no\n")
            return
        while node:
            if node.key == key:
                print("yes\n")
                return
            node = node.left if key < node.key else node.right
        print("no\n")
        return
            

def right_rotate(node):
    child = node.left
    node.left, child.right = child.right, node
    return child

def left_rotate(node):
    child = node.right
    node.right, child.left = child.left, node
    return child

def print_preorder(node):
    print(" {}".format(node.key))
    if node.left:
        print_preorder(node.left)
    if node.right:
        print_preorder(node.right)

def print_inorder(node):
    if node.left:
        print_inorder(node.left)
    print(" {}".format(node.key))
    if node.right:
        print_inorder(node.right)

if __name__ == "__main__":
    n = int(input())
    treap = Treap()
    root = None
    for _ in range(n):
        operation, *num = input().split()
        if operation[0] == "i":
            root = treap.insert(root, int(num[0]), int(num[1]))
        elif operation[0] == "f":
            treap.find(root, int(num[0]))
        elif operation[0] == "d":
            root = treap.delete(root, int(num[0]))
        elif root is not None:
            print_inorder(root)
            print("\n")
            print_preorder(root)
            print("\n")

Submission status: Accepted

Solution code:
class Node:
    def __init__(self, key, priority):
        self.key = key
        self.priority = priority
        self.left = None
        self.right = None
class Treap:
    def __init__(self):
        self.root = None
        self.order_list = []
    def right_rotate(self, t):
        s = t.left
        t.left = s.right
        s.right = t
        return s
    def left_rotate(self, t):
        s = t.right
        t.right = s.left
        s.left = t
        return s
    def find(self, key):
        x = self.root
        while x != None:
            if key == x.key:
                return x
            elif key < x.key:
                x = x.left
            else:
                x = x.right
        return None
    def insert(self, t, key, priority):
        if t == None:
            return Node(key, priority)
        if key == t.key:
            return t
        if key < t.key:
            t.left = self.insert(t.left, key, priority)
            if priority > t.priority:
                t = self.right_rotate(t)
        else:
            t.right = self.insert(t.right, key, priority)
            if priority > t.priority:
                t = self.left_rotate(t)
        return t
    def delete(self, t, key):
        if t == None:
            return None
        if key < t.key:
            t.left = self.delete(t.left, key)
        elif key > t.key:
            t.right = self.delete(t.right, key)
        else:
            return self.__delete(t, key)
        return t
    def __delete(self, t, key):
        if t.left == None and t.right == None:
            return None
        elif t.left == None:
            t = self.left_rotate(t)
        elif t.right == None:
            t = self.right_rotate(t)
        else:
            if t.left.priority < t.right.priority:
                t = self.left_rotate(t)
            else:
                t = self.right_rotate(t)
        return self.delete(t, key)
    def walk_preorder(self, node):
        if node == None:
            return None
        self.order_list.append(node.key)
        self.walk_preorder(node.left)
        self.walk_preorder(node.right)
    def walk_inorder(self, node):
        if node == None:
            return None
        self.walk_inorder(node.left)
        self.order_list.append(node.key)
        self.walk_inorder(node.right)
    def print_nodes(self):
        self.order_list = []
        self.walk_inorder(self.root)
        inorder_str = ' '.join(map(str, self.order_list))
        print(' {}'.format(inorder_str))
        self.order_list = []
        self.walk_preorder(self.root)
        preorder_str = ' '.join(map(str, self.order_list))
        print(' {}'.format(preorder_str))
n = int(input())
tree = Treap()
for _ in range(n):
    command = input().split(' ')
    if len(command) == 1:
        tree.print_nodes()
    elif len(command) == 2:
        opecode, key = command[0], int(command[1])
        if opecode == 'delete':
            tree.root = tree.delete(tree.root, key)
        elif opecode == 'find':
            if tree.find(key) != None:
                print('yes')
            else:
                print('no')
    else:
        opecode, key, priority = command[0], int(command[1]), int(command[2])
        tree.root = tree.insert(tree.root, key, priority)