170 likes | 293 Views
K: 5. P: 9. K:1. K: 8. P:2. P: 6. K: 7. P: 4. K: 5. P: 9. K:1. K: 8. P:2. P: 6. Is the treap a heap?. K: 7. P: 4. P: 9. P:2. 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. K: 5. P: 9. K:1. K: 8. P:2. P: 6.
E N D
K: 5 P: 9 K:1 K: 8 P:2 P: 6 K: 7 P: 4
K: 5 P: 9 K:1 K: 8 P:2 P: 6 Is the treap a heap? K: 7 P: 4
P: 9 P:2 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
K: 5 P: 9 K:1 K: 8 P:2 P: 6 Not a complete tree! NO! K: 7 P: 4
K: 5 P: 9 K:1 K: 8 P:2 P: 6 Is the treap a Binary Search Tree? K: 7 P: 4
K: 5 K:1 K: 8 BST? Yes! K: 7
K: 5 K:1 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
(K, P) (5,9) (7,4) (8,6) (1,2) K: 5 K: 7 K: 8 K:1 P: 9 P:2 P: 4 P: 6
(K, P) (5,9) (7,4) (8,6) (1,2) K: 5 P: 9 K: 8 P: 6 K: 7 P: 4 K:1 P:2
K: 5 P: 9 K: 8 P: 6 K: 7 P: 4 K:1 P:2
K: 5 P: 9 K: 8 P: 6 K: 7 P: 4 K:1 P:2
K: 5 P: 9 K:1 K: 8 P:2 P: 6 Assume no duplicate key / priority, only one treap is possible K: 7 P: 4
(2,5) (5,2) (3,1) (4,7) (9,4) (8,3) K:2 K:5 K:3 K:4 K:9 K:8 P:5 P:2 P:1 P:7 P:4 P:3
Arrange from left to right, Smallest key Biggest key K:8 K:9 K:4 K:5 K:2 K:3 P:3 P:4 P:7 P:2 P:5 P:1
K:4 P:7 K:2 Without destroying left to right arrangement, Shift the “nodes” up and down P:5 K:9 P:4 Biggest priority K:8 P:3 Smallest priority K:5 P:2 K:3 P:1
K:4 P:7 K:2 P:5 K:9 P:4 K:8 P:3 K:5 P:2 K:3 P:1
K:4 P:7 K:9 K:2 P:4 P:5 K:8 K:3 P:3 P:1 K:5 P:2