Title: K: 5
1K 5
P 9
K1
K 8
P2
P 6
K 7
P 4
2K 5
P 9
K1
K 8
P2
P 6
Is the treap a heap?
K 7
P 4
3P 9
P2
P 6
For every node v, the search key in v is greater
than or equal to those in the children of v
P 4
4K 5
P 9
K1
K 8
P2
P 6
Not a complete tree! NO!
K 7
P 4
5K 5
P 9
K1
K 8
P2
P 6
Is the treap a Binary Search Tree?
K 7
P 4
6K 5
K1
K 8
BST? Yes!
K 7
7K 5
K1
K 8
All keys smaller than the root are stored in the
left subtreeAll keys larger than the root are
sorted in the right subtree
K 7
8(K, P) (5,9) (7,4) (8,6) (1,2)
K 5
K 8
K 7
K1
P 9
P2
P 6
P 4
9(K, P) (5,9) (7,4) (8,6) (1,2)
K 5
P 9
K 8
P 6
K 7
P 4
K1
P2
10K 5
P 9
K 8
P 6
K 7
P 4
K1
P2
11K 5
P 9
K 8
P 6
K 7
P 4
K1
P2
12K 5
P 9
K1
K 8
P2
P 6
Assume no duplicate key / priority, only one
treap is possible
K 7
P 4
13(2,5) (5,2) (3,1) (4,7) (9,4) (8,3)
K2
K5
K3
K4
K9
K8
P5
P2
P1
P7
P4
P3
14Arrange from left to right,
Smallest key? Biggest key
K9
K8
K5
K4
K2
K3
P4
P3
P2
P7
P5
P1
15K4
P7
K2
Without destroying left to right
arrangement, Shift the nodes up and down
P5
K9
P4
Biggest priority
K8
P3
Smallest priority
K5
P2
K3
P1
16K4
P7
K2
P5
K9
P4
K8
P3
K5
P2
K3
P1
17K4
P7
K9
K2
P4
P5
K8
K3
P3
P1
K5
P2