11 Treap
(require (planet krhari/pfds:1:5/treap)) |
Treaps are binary search trees in which each node has both a search key and a priority. Its keys are sorted in inorder and its each node priority is smaller than the priorities of its children. Because of this, a treap is a binary search tree for the keys and a heap for its priorities. This implementation uses random priorities to achieve good average-case performance.
Provides worst case running time of O(log(n)) for the operations insert, find-min/max and delete-min/max.
(Treap A) |
Example: |
> (treap < 1 2 3 4 5 6) |
- : (Treap Positive-Fixnum) |
#<Treap> |
In the above example, the treap obtained will have elements 1 thru’ 6 with < as the comparison function.
Examples: |
> (empty? (treap < 1 2 3 4 5 6)) |
- : Boolean |
#f |
> (empty? (treap <)) |
- : Boolean |
#t |
Example: |
> (insert 10 (treap < 1 2 3)) |
- : (Treap Positive-Fixnum) |
#<Treap> |
In the above example, insert adds the element 10 to (treap < 1 2 3).
(find-min/max treap) → A |
treap : (Treap A) |
Examples: |
> (find-min/max (treap < 1 2 3 4 5 6)) |
- : Positive-Fixnum |
1 |
> (find-min/max (treap > 1 2 3 4 5 6)) |
- : Positive-Fixnum |
6 |
> (find-min/max (treap <)) |
find-min/max: given treap is empty |
(delete-min/max treap) → (Treap A) |
treap : (Treap A) |
Examples: |
> (delete-min/max (treap < 1 2 3 4 5 6)) |
- : (Treap Positive-Fixnum) |
#<Treap> |
> (delete-min/max (treap > 1 2 3 4 5 6)) |
- : (Treap Positive-Fixnum) |
#<Treap> |
> (delete-min/max (treap <)) |
delete-min/max: given treap is empty |
In the above example, (delete-min/max (treap < 1 2 3 4 5 6)), deletes the element 1(min) from the treap. And (delete-min/max (treap > 1 2 3 4 5 6)), deletes the element 6(max) from the treap.
Examples: |
> (delete 6 (treap < 1 2 3 4 5 6)) |
- : (Treap Positive-Fixnum) |
#<Treap> |
> (delete 3 (treap > 1 2 3 4 5 6)) |
- : (Treap Positive-Fixnum) |
#<Treap> |
> (delete 10 ((inst treap Integer) <)) |
delete: given treap is empty |
In the above example, (delete 6 (treap < 1 2 3 4 5 6)), deletes the 6 returns (treap < 1 2 3 4 5). And (delete 3 (treap > 1 2 3 4 5 6)) returns (treap > 1 2 4 5 6).
(treap->list treap) → (Listof A) |
treap : (Treap A) |
Examples: |
> (treap->list (treap > 1 2 3 4 5 6)) |
- : (Listof Positive-Fixnum) |
'(2 4 6 5 3 1) |
> (treap->list (treap < 1 2 3 4 5 6)) |
- : (Listof Positive-Fixnum) |
'(1 6 2 3 4 5) |
(map comparer func treap1 treap2 ...) → (Treap A) |
comparer : (C C -> Boolean) |
func : (A B ... B -> C) |
treap1 : (Treap A) |
treap2 : (Treap B) |
Examples: |
> (treap->list (map < add1 (treap < 1 2 3 4 5 6))) |
- : (Listof Exact-Positive-Integer) |
'(3 2 7 4 5 6) |
> (treap->list (map < * (treap < 1 2 3 4 5 6) (treap < 1 2 3 4 5 6))) |
- : (Listof Exact-Positive-Integer) |
'(12 6 5 5 12 24) |
(fold func init treap1 treap2 ...) → C |
func : (C A B ... B -> C) |
init : C |
treap1 : (Treap A) |
treap2 : (Treap B) |
fold currently does not produce correct results when the given function is non-commutative.
Examples: |
> (fold + 0 (treap < 1 2 3 4 5 6)) |
- : Exact-Nonnegative-Integer |
21 |
> (fold * 1 (treap < 1 2 3 4 5 6) (treap < 1 2 3 4 5 6)) |
- : Exact-Positive-Integer |
518400 |
Examples: |
> (define trp (treap < 1 2 3 4 5 6)) |
> (treap->list (filter (λ: ([x : Integer]) (> x 5)) trp)) |
- : (Listof Positive-Fixnum) |
'(6) |
> (treap->list (filter (λ: ([x : Integer]) (< x 5)) trp)) |
- : (Listof Positive-Fixnum) |
'(4 2 1 3) |
> (treap->list (filter (λ: ([x : Integer]) (<= x 5)) trp)) |
- : (Listof Positive-Fixnum) |
'(2 1 5 4 3) |
Examples: | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(2 1 5 3 4) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(5 6) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(6) |
(andmap func treap1 treap2 ...) → Boolean |
func : (A B ... B -> Boolean) |
treap1 : (Treap A) |
treap2 : (Treap B) |
Examples: |
> (andmap even? (treap < 1 2 3 4 5 6)) |
- : Boolean |
#f |
> (andmap odd? (treap < 1 2 3 4 5 6)) |
- : Boolean |
#f |
> (andmap positive? (treap < 1 2 3 4 5 6)) |
- : Boolean |
#t |
> (andmap negative? (treap < -1 -2)) |
- : Boolean |
#t |
(ormap func treap1 treap2 ...) → Boolean |
func : (A B ... B -> Boolean) |
treap1 : (Treap A) |
treap2 : (Treap B) |
Examples: |
> (ormap even? (treap < 1 2 3 4 5 6)) |
- : Boolean |
#t |
> (ormap odd? (treap < 1 2 3 4 5 6)) |
- : Boolean |
#t |
> (ormap positive? (treap < -1 -2 3 4 -5 6)) |
- : Boolean |
#t |
> (ormap negative? (treap < 1 -2)) |
- : Boolean |
#t |
(build-treap size func comp) → (Treap A) |
size : Natural |
func : (Natural -> A) |
comp : (A A -> Boolean) |
Examples: |
> (treap->list (build-treap 5 (λ:([x : Integer]) (add1 x)) <)) |
- : (Listof Integer) |
'(2 1 3 5 4) |
> (treap->list (build-treap 5 (λ:([x : Integer]) (* x x)) <)) |
- : (Listof Integer) |
'(16 0 1 4 9) |