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 < u.key$ and if $v$ is a right child of $u$, then $u.key < v.key$  heap property. If $v$ is a child of $u$, then $v.priority < u.priority$  This combination of properties is why the tree is called Treap (tree + heap). An example of Treap is shown in the following figure.  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 < t.key                               // move to the left child
        t.left = insert(t.left, key, priority)   // update the pointer to the left child
        if t.priority < t.left.priority          // rotate right if the left child has higher priority
            t = rightRotate(t)
    else                                         // move to the right child
        t.right = insert(t.right, key, priority) // update the pointer to the right child
        if t.priority < t.right.priority         // rotate left if the right child has higher priority
            t = leftRotate(t)

  return t  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                             // search the target recursively
        t.left = delete(t.left, key)
    else 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.

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


parent_node = None    
    

counter = 0
        
def getRoot(u):
    while u.parent != None:
        u = u.parent
    return u


def printPreorder(u):
    if u == None:
        return
    print(" ", u.key, sep="", end="")
    printPreorder(u.left)
    printPreorder(u.right)

    
def printInorder(u):
    if u == None:
        return
    printInorder(u.left)
    print(" ", u.key, sep="", end="")
    printInorder(u.right)

    
def find(u, num):
    while u != None and u.key != num:
        if num < u.key:
            u = u.left
        else:
            u = u.right
    if u != None:
        print("yes")
    else:
        print("no")


def rightRotate(u):
    
    s = u.left
    u.left = s.right
    
    if u.left != None:
        u.left.parent = u
    
    s.right = u 
    
    s.parent = u.parent
    u.parent = s
    return s

def leftRotate(u):
    
    s = u.right
    u.right = s.left
    
    if u.right != None:
        u.right.parent = u
    
    s.left = u
    
    s.parent = u.parent
    u.parent = s
    return s

        
def insert(u, key, priority):
    
    global parent_node
    global counter
    if u == None:
        s = Nodes[counter]
        
        counter += 1
        s.key = key
        s.priority = priority
        
        if parent_node == None:
            parent_node = s
        return s
    
    if key == u.key:
        return u
    
    if key < u.key:
        u.left = insert(u.left, key, priority)
        
        u.left.parent = u
        if u.priority < u.left.priority:
            u = rightRotate(u)
    else:
        u.right = insert(u.right, key, priority)
        
        u.right.parent = u
        if u.priority < u.right.priority:
            u = leftRotate(u)
    
    return u
    

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


def _delete(u, key):
    
    if u.left == None and u.right == None:
        return None
    elif u.left == None:
        u = leftRotate(u)
    elif u.right == None:
        u = rightRotate(u)
    else:
        if u.left.priority > u.right.priority:
            u = rightRotate(u)
        else:
            u = leftRotate(u)
    return delete(u, key)
    
    
n = int(input())


Nodes = []
for i in range(n):
    tmp_node = Node(0, 0)
    Nodes.append(tmp_node)



for i in range(n):
    cmd = input().split()
    if cmd[0] == 'insert':
        key = int(cmd[1])
        priority = int(cmd[2])
        insert(parent_node, key, priority)
        
        
        parent_node = getRoot(parent_node)
        
    
    elif cmd[0] == 'print':
        printInorder(parent_node)
        print("")
        printPreorder(parent_node)
        print("")
    
    elif cmd[0] == 'find':
        key = int(cmd[1])
        find(parent_node, key)
    
    else:
        key = int(cmd[1])
        
        
        if parent_node.key != key:
            delete(parent_node, key)
            parent_node = getRoot(parent_node)
        elif parent_node.left != None:
            tmp_node = parent_node.left
            delete(parent_node, key)
            parent_node = getRoot(tmp_node)
        elif parent_node.right != None:
            tmp_node = parent_node.right
            delete(parent_node, key)
            parent_node = getRoot(tmp_node)
        else:
            delete(parent_node, key)
            parent_node = None

Solution (Python):
NIL = -1


class Node():
    __slots__ = ["key", "parent", "left", "right"]

    def __init__(self, key=NIL):
        self.key = key
        self.parent = NIL
        self.left = NIL
        self.right = NIL

class NodeHeap(Node):
    __slots__ = ["priority"]

    def __init__(self,key=NIL, priority=NIL):
        super().__init__(key)
        self.priority = priority

class BinaryTree():
    def __init__(self):
        self.root = NIL

    def _isnil(self, x):
        if isinstance(x, int):
            return True
        else:
            return False

    def insert(self, key):
        if self.root == NIL:
            self.root = Node(key)
            return

        now = self.root
        while now != NIL:
            parent = now
            if   key < now.key:
                now = now.left
            elif key > now.key:
                now = now.right
            else:
                raise

        insert = self.get_newnode(key)
        insert.parent = parent

        if   insert.key < parent.key:
            parent.left = insert
        elif insert.key > parent.key:
            parent.right = insert
        else:
            raise

    def find(self, key):
        now = self.root
        while now != NIL and now.key != key:
            if   key < now.key:
                now = now.left
            elif key > now.key:
                now = now.right
            else:
                raise

        return now

    def delete(self, key):
        z = self.find(key)
        if self._isnil(z):
            return

        if z.left == NIL or z.right == NIL:
            y = z
        else:
            y = self.get_successor(z)

        if y.left != NIL:
            x = y.left
        else:
            x = y.right

        if x != NIL:
            x.parent = y.parent

        if y.parent == NIL:
            
            self.root = x
        elif y == y.parent.left:
            y.parent.left = x
        else:
            y.parent.right = x

        if y != z:
            z.key = y.key

    def get_successor(self, z):
        if z.right != NIL:
            return self.get_minimum(z.right)

        y = z.parent
        while y != NIL and z == y.right:
            z = y
            y = y.parent

        return y

    def get_minimum(self, x):
        while x.left != NIL:
            x = x.left

        return x

    def print_inorder(self):
        self._treewalk_inorder(self.root)
        print()

    def _treewalk_inorder(self, node):
        if self._isnil(node):
            return

        self._treewalk_inorder(node.left)
        print(f" {node.key}", end="")
        self._treewalk_inorder(node.right)

    def print_preorder(self):
        self._treewalk_preorder(self.root)
        print()

    def _treewalk_preorder(self, node):
        if self._isnil(node):
            return

        print(f" {node.key}", end="")
        self._treewalk_preorder(node.left)
        self._treewalk_preorder(node.right)

class Treap(BinaryTree):
    def __init__(self):
        self.root = NIL

    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 insert(self, key, priority):
        self.root = self._insert_main(self.root, key, priority)

    def _insert_main(self, node, key, priority):
        if node == NIL:
            return NodeHeap(key, priority)

        if   key > node.key:
            node.right = self._insert_main(node.right, key, priority)
            if node.priority < node.right.priority:
                node = self._left_rotate(node)
        elif key < node.key:
            node.left = self._insert_main(node.left, key, priority)
            if node.priority < node.left.priority:
                node = self._right_rotate(node)
        else:
            raise

        return node

    def delete(self, key):
        self.root = self._delete_main(self.root, key)

    def _delete_main(self, node, key):
        if node == NIL:
            return node

        if key < node.key:
            node.left = self._delete_main(node.left, key)
        elif key > node.key:
            node.right = self._delete_main(node.right, key)
        else:
            return self._delete_rotate(node, key)

        return node

    def _delete_rotate(self, node, key):
        if node.left == NIL and node.right == NIL:
            return NIL
        elif node.left == NIL:
            node = self._left_rotate(node)
        elif node.right == NIL:
            node = self._right_rotate(node)
        else:
            if node.left.priority > node.right.priority:
                node = self._right_rotate(node)
            else:
                node = self._left_rotate(node)

        return self._delete_main(node, key)


m = int(input())

treap = Treap()

for _ in range(m):

    command, *list_num = input().split()

    if   command == "insert":
        k = int(list_num[0])
        p = int(list_num[1])
        treap.insert(k, p)
    elif command == "find":
        k = int(list_num[0])
        if isinstance(treap.find(k), int):
            print("no")
        else:
            print("yes")
    elif command == "delete":
        k = int(list_num[0])
        treap.delete(k)
    elif command == "print":
        treap.print_inorder()
        treap.print_preorder()
    else:
        raise

Solution (Python):
class Node():
    def __init__(self, key, pri):
        self.key = key
        self.pri = pri
        self.left = self.right = None
         
 
def insert(t, key, pri):
    if t == None:
        return Node(key, pri) 
    if key == t.key:
        return t                             
 
    if key < t.key:
        t.left = insert(t.left, key, pri)
        if t.pri < t.left.pri:
            t = rightRotate(t)
    else:
        t.right = insert(t.right, key, pri)
        if t.pri < t.right.pri:
            t = leftRotate(t)
 
    return t
         
def rightRotate(node):
    global root
    a = node.left
    node.left = a.right
    a.right = node
    if node == root:
        root = a
    return a
 
def leftRotate(node):
    global root
    a = node.right
    node.right = a.left
    a.left = node
    if node == root:
        root = a
    return a
     
         
def pre(node):
    if node == None:return
    print('',node.key, end = '');pre(node.left);pre(node.right)
     
def ino(node):
    if node == None:return
    ino(node.left);print('',node.key,end='');ino(node.right)
 
def find(node, key):
    if node == None:
        print('no');return
    if node.key == key:
        print('yes');return
    elif node.key > key:
        find(node.left, key)
    else:
        find(node.right, key)
 
def delete(t, key):
    global root
    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)
    elif key == 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.pri > t.right.pri:
                t = rightRotate(t)
            else:
                t = leftRotate(t)
        return delete(t, key)
    return t
 
     
 
n = int(input())


root = None


for j in range(n):
    s, *i = input().split()
    if s[0] == 'i':root = insert(root,int(i[0]), int(i[1]))
    elif s[0] == 'f':
        find(root, int(i[0]))
    elif s[0] == 'd':
        delete(root, int(i[0]))
    else:
        ino(root);print();pre(root);print()

Solution (Python):
class Node:
    def __init__(self,key,pri,left=None,right=None):
        self.left = left
        self.right = right
        self.key = key
        self.pri = pri
 
def insert(t,key,pri):
    if t == None :
        return Node(key,pri)
    if key == t.key :
        return t
    if key < t.key :
        t.left = insert(t.left, key, pri)
        if t.pri < t.left.pri :
            t = rrotate(t)
    else:
        t.right = insert(t.right, key, pri)
        if t.pri < t.right.pri :
            t = lrotate(t)
    return t
 
 
def rrotate(t):
    s = t.left
    t.left = s.right
    s.right = t
    return s

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

 
def find(t,key):
    if t == None: return None
    elif t.key == key:return t
    elif t.key > key :return find(t.left,key)
    else: return find(t.right,key)
 
def erase(t, key):
    if t == None : return None
    if key == t.key: 
        if t.left == None and t.right == None:return None
        elif t.left == None : t = lrotate(t)
        elif t.right == None : t = rrotate(t)
        else:
            if t.left.pri > t.right.pri:t = rrotate(t)
            else: t = lrotate(t)
        return erase(t, key)
    elif key < t.key : t.left = erase(t.left, key)
    else : t.right = erase(t.right, key)
    return t
 
def inorder(t):
    if t.left != None:inorder(t.left)
    print(" "+str(t.key), end='')
    if t.right != None: inorder(t.right)
 
def preorder(t):
    print(" "+str(t.key), end='')
    if t.left != None :preorder(t.left)
    if t.right != None : preorder(t.right)

if __name__=="__main__": 
        m=(int)(input())
        root = None
        for i in range(m):
            order = input().split()
            if order[0] == "insert" :root = insert(root,(int)(order[1]),(int)(order[2]))
            elif order[0] == "delete" :root = erase(root,(int)(order[1]))
            elif order[0] == "find" :
                res = find(root,(int)(order[1]))
                if res == None :print("no")
                else:print("yes")
            else:
                inorder(root)
                print()
                preorder(root)
                print()

Solution (Python):
import sys


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


def right_rotate(t: Node):
    s = t.left
    t.left = s.right
    s.right = t
    return s


def left_rotate(t: Node):
    s = t.right
    t.right = s.left
    s.left = t
    return s


def insert(t: Node, key: int, pri: int):
    if t is None:
        return Node(key, pri)
    if key == t.key:
        return t
    if key < t.key:
        t.left = insert(t.left, key, pri)
        if t.pri < t.left.pri:
            t = right_rotate(t)
    else:
        t.right = insert(t.right, key, pri)
        if t.pri < t.right.pri:
            t = left_rotate(t)
    return t


def erase(t: Node, key: int):
    if t is None:
        return None
    if key == t.key:
        if t.left is None and t.right is None:
            return None
        elif t.left is None:
            t = left_rotate(t)
        elif t.right is None:
            t = right_rotate(t)
        else:
            if t.left.pri > t.right.pri:
                t = right_rotate(t)
            else:
                t = left_rotate(t)
        return erase(t, key)
    if key < t.key:
        t.left = erase(t.left, key)
    else:
        t.right = erase(t.right, key)
    return t


def find(t: Node, x: int):
    while t is not None:
        if t.key == x:
            return 1
        elif t.key > x:
            t = t.left
        else:
            t = t.right
    return 0


def inorder(t: Node):
    if t.left is not None:
        inorder(t.left)
    print(" {}".format(t.key), end='')

    if t.right is not None:
        inorder(t.right)
    return


def preorder(t: Node):
    print(" {}".format(t.key), end='')

    if t.left is not None:
        preorder(t.left)
    if t.right is not None:
        preorder(t.right)
    return


def print_key(t: Node):
    inorder(t)
    print("\n", end='')
    preorder(t)
    print("\n", end='')
    return


line = sys.stdin.readline()
n = int(line)
root = None
for i in range(n):
    line = sys.stdin.readline().split()
    if line[0] == 'insert':
        k, p = int(line[1]), int(line[2])
        root = insert(root, k, p)
    elif line[0] == 'delete':
        k = int(line[1])
        root = erase(root, k)
    elif line[0] == 'find':
        k = int(line[1])
        if find(root, k):
            print("yes")
        else:
            print("no")
    else:
        print_key(root)

Solution (Python):
import sys
import time
input = sys.stdin.readline

def left_rot(node):
    s = node['right'].copy()
    node['right'] = s['left']
    s['left'] = node
    return s

def right_rot(node):
    s = node['left'].copy()
    node['left'] = s['right']
    s['right'] = node
    return s

def insert(node,k,p):
    if(node is None):
        child = {
            'left' : None,
            'right' : None,
            'key' : k,
            'priority' : p}
        return child
    if(k == node['key']):
        return node
    
    if(k < node['key']):
        node['left'] = insert(node['left'],k,p)
        
        if(node['priority'] < node['left']['priority']):
            node = right_rot(node)
    
    else:
        node['right'] = insert(node['right'],k,p)
        
        if(node['priority'] < node['right']['priority']):
            node = left_rot(node)

    return node

def find(node, k):
    if(node is None):
        return 'no'
    if(node['key'] == k):
        return 'yes'
    if(k < node['key']):
        return find(node['left'],k)
    else:
        return find(node['right'],k)

def delete(node, k):
    if(node is None):
        return None

    if(node['key'] == k):
        if(node['left'] is None and node['right'] is None):
            return None
        elif(node['left'] is None):
            node = left_rot(node)
        elif(node['right'] is None):
            node = right_rot(node)
        else:
            if(node['left']['priority'] > node['right']['priority']):
                node = right_rot(node)
            else:
                node = left_rot(node)
        return delete(node, k)
    if(k < node['key']):
        node['left'] = delete(node['left'], k)
    else:
        node['right'] = delete(node['right'], k)

    return node

def print_treap(node,values):
    if(node is None):
        return values
    values.append(node['key'])
    values = print_treap(node['left'],values)
    values = print_treap(node['right'],values)
    return values

num = int(input())
req = [input().split() for _ in range(num)]
node = None
values = []
for q in req:
    if(q[0] == 'insert'):
        node = insert(node, int(q[1]),int(q[2]))
    elif(q[0] == 'find'):
        print(find(node,int(q[1])))
    elif(q[0] == 'delete'):
        node = delete(node, int(q[1]))
    else:
        print_treap(node, values)
        print(" "+" ".join(map(str, sorted(values))))
        print(" "+" ".join(map(str, values)))
        values.clear()

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

def insert(t, key, pri):
    if t == None:
        return Node(key, pri) 
    if key == t.key:
        return t                             

    if key < t.key:
        t.left = insert(t.left, key, pri)
        if t.pri < t.left.pri:
            t = rightRotate(t)
    else:
        t.right = insert(t.right, key, pri)
        if t.pri < t.right.pri:
            t = leftRotate(t)

    return t
        
def rightRotate(node):
    global root
    a = node.left
    node.left = a.right
    a.right = node
    if node == root:
        root = a
    return a

def leftRotate(node):
    global root
    a = node.right
    node.right = a.left
    a.left = node
    if node == root:
        root = a
    return a
    
        
def pre(node):
    if node == None:return
    print('',node.key, end = '');pre(node.left);pre(node.right)
    
def ino(node):
    if node == None:return
    ino(node.left);print('',node.key,end='');ino(node.right)

def find(node, key):
    if node == None:
        print('no');return
    if node.key == key:
        print('yes');return
    elif node.key > key:
        find(node.left, key)
    else:
        find(node.right, key)

def delete(t, key):
    global root
    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)
    elif key == 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.pri > t.right.pri:
                t = rightRotate(t)
            else:
                t = leftRotate(t)
        return delete(t, key)
    return t

    

n = int(input())
root = None
for j in range(n):
    s, *i = input().split()
    if s[0] == 'i':root = insert(root,int(i[0]), int(i[1]))
    elif s[0] == 'f':
        find(root, int(i[0]))
    elif s[0] == 'd':
        delete(root, int(i[0]))
    else:
        ino(root);print();pre(root);print()

Solution (Python):


class Node:
    def __init__(self, key, priority, left=None, right=None):
        self.key = key
        self.priority = priority
        self.left = left
        self.right = right
        self.parent = None
        self.ans = []

class Treap:
    def __init__(self, root=None):
        self.root = root
    
    def insert(self, node, key, priority):
        if node == 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 = self.rightRotate(node)
        else:
            node.right = self.insert(node.right, key, priority)
            if node.priority < node.right.priority:
                node = self.leftRotate(node)
        return node
            
        
    def rightRotate(self, node):
        tmp = node.left
        node.left = tmp.right
        tmp.right = node
        return tmp
        
    def leftRotate(self, node):
        tmp = node.right
        node.right = tmp.left
        tmp.left = node
        return tmp
    
    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._delete(node, key)
        return node
    
    def _delete(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.priority > node.right.priority:
                node = self.rightRotate(node)
            else:
                node = self.leftRotate(node)
        return self.delete(node, key)
        
    def find(self, node, key):
        if key == node.key:
            return node
        if key < node.key:
            if node.left != None:
                return self.find(node.left, key)
            else:
                return None
        if key > node.key:
            if node.right != None:
                return self.find(node.right, key)
            else:
                return None
    
    def resetAns(self):
        self.ans = []
    
    def getPreorder(self, node):
        self.ans.append(node)
        if node.left != None:
            self.getPreorder(node.left)
        if node.right != None:
            self.getPreorder(node.right)
    
    def getInorder(self, node):
        if node.left != None:
            self.getInorder(node.left)
        self.ans.append(node)
        if node.right != None:
            self.getInorder(node.right)
            
    def printAns(self):
        for nd in self.ans:
            print(" " + str(nd.key),end="")
        print()

m = int(input().rstrip())
tr = Treap()
for i in range(m):
    line = input().rstrip().split()
    if line[0] == "insert":
        tr.root = tr.insert(tr.root, int(line[1]), int(line[2]))
    elif line[0] == "find":
        if tr.find(tr.root, int(line[1])) != None:
            print("yes")
        else:
            print("no")
    elif line[0] == "delete":
        tr.root = tr.delete(tr.root, int(line[1]))
    else:
        tr.resetAns()
        tr.getInorder(tr.root)
        tr.printAns()
        tr.resetAns()
        tr.getPreorder(tr.root)
        tr.printAns()