170 likes | 298 Vues
This article explores the properties of treaps, discussing whether they qualify as heaps or binary search trees (BSTs). A treap is defined by keys and priorities, with the invariant that for every node, the search key is greater than or equal to the keys of its children. We will clarify that unlike complete trees, treaps do not enforce a strict left-to-right arrangement of nodes. This piece also addresses the arrangement of keys by priority, providing insights into their structure and usage in data organization.
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