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 .
syntax
( Treap A)
- : #(struct:Treap
((-> Positive-Byte Positive-Byte Boolean)
(U (Node Positive-Byte) Null)
Integer))
#<Treap>
In the above example, the treap obtained will have elements 1 thru’ 6 with < as the comparison function.
Added in version 0.1 of package pfds.
- : #(struct:Treap
((-> Positive-Byte Positive-Byte Boolean)
(U (Node Positive-Byte) Null)
Integer))
#<Treap>
In the above example, insert adds the element 10 to (treap<123).
procedure
( find-min/max treap)→A
- : Integer [more precisely: Positive-Byte]
1
- : Integer [more precisely: Positive-Byte]
6
find-min/max: given treap is empty
procedure
( delete-min/max treap)→(Treap A)
- : #(struct:Treap
((-> Positive-Byte Positive-Byte Boolean)
(U (Node Positive-Byte) Null)
Integer))
#<Treap>
- : #(struct:Treap
((-> Positive-Byte Positive-Byte Boolean)
(U (Node Positive-Byte) Null)
Integer))
#<Treap>
delete-min/max: given treap is empty
In the above example, (delete-min/max (treap<123456)), deletes the element 1(min) from the treap. And (delete-min/max (treap>123456)), deletes the element 6(max) from the treap.
- : #(struct:Treap
((-> Positive-Byte Positive-Byte Boolean)
(U (Node Positive-Byte) Null)
Integer))
#<Treap>
- : #(struct:Treap
((-> Positive-Byte Positive-Byte Boolean)
(U (Node Positive-Byte) Null)
Integer))
#<Treap>
eval:17:0: Type Checker: Polymorphic function `delete' could
not be applied to arguments:
Argument 1:
Expected: A
Given:Positive-Byte
Argument 2:
Expected: #(struct:Treap ((-> A A Boolean) (U (Node A)
Null) Integer))
Given:#(struct:Treap ((-> Nothing Nothing Boolean) (U
(Node Nothing) Null) Integer))
in: <
In the above example, (delete 6(treap<123456)), deletes the 6 returns (treap<12345). And (delete 3(treap>123456)) returns (treap>12456).
procedure
( treap->list treap)→(ListofA)
- : (Listof Positive-Byte)
'(5 6 2 4 3 1)
- : (Listof Positive-Byte)
'(2 1 3 4 5 6)
comparer:(CC->Boolean)func:(AB...B->C)
- : (Listof Positive-Index)
'(3 2 7 5 4 6)
- : (Listof Positive-Index)
'(5 2 24 24 6 15)
procedure
( fold funcinittreap1treap2...)→C
func:(CAB...B->C)init:C
fold currently does not produce correct results when the given function is non-commutative.
- : Integer [more precisely: Nonnegative-Integer]
21
- : Integer [more precisely: Positive-Integer]
518400
> (definetrp(treap<123456))- : (Listof Positive-Byte)
'(6)
- : (Listof Positive-Byte)
'(2 1 3 4)
- : (Listof Positive-Byte)
'(5 3 2 1 4)
(treap<123456)))- : (Listof Positive-Byte)
'(1 2 3 4 5)
(treap<123456)))- : (Listof Positive-Byte)
'(6 5)
(treap<123456)))- : (Listof Positive-Byte)
'(6)
procedure
( andmap functreap1treap2...)→Boolean
func:(AB...B->Boolean)
- : Boolean
#f
- : Boolean
#f
- : Boolean
#t
- : Boolean
#t
procedure
( ormap functreap1treap2...)→Boolean
func:(AB...B->Boolean)
- : Boolean
#t
- : Boolean
#t
- : Boolean
#t
- : Boolean
#t
procedure
( build-treap sizefunccomp)→(Treap A)
size:Naturalfunc:(Natural->A)comp:(AA->Boolean)
- : (Listof Integer)
'(5 1 2 3 4)
- : (Listof Integer)
'(4 0 1 16 9)
The following functions allow the user to manipulate node priorities. Treaps containing nodes with manually set priorities cannot guarantee O(log(n)) running times, and care should be taken in selecting appropriate priorities if one wants to avoid O(n) running times.
procedure
( treap/priority compa...)→(Treap A)
comp:(AA->Boolean)a:(PairofAReal)
- : #(struct:Treap
((-> Positive-Byte Positive-Byte Boolean)
(U (Node Positive-Byte) Null)
Integer))
#<Treap>
In the above example, the treap obtained will have elements 1 thru 6 with < as the comparison function.
Added in version 0.1 of package pfds.
procedure
( insert/priority aprioritytreap)→(Treap A)
a:Apriority:Real
- : #(struct:Treap
((-> Positive-Byte Positive-Byte Boolean)
(U (Node Positive-Byte) Null)
Integer))
#<Treap>
- : Integer [more precisely: Positive-Byte]
10
- : Integer [more precisely: Positive-Byte]
1
In the above examples, insert/priority adds the element 10 to (treap<123) with varying priorities.
Added in version 0.1 of package pfds.
- : Integer [more precisely: Positive-Byte]
1
- : Symbol
'c
The root node is always the node with the lowest priority.
Added in version 0.1 of package pfds.
procedure
( root/priority treap)→Real
- : Real
2
Added in version 0.1 of package pfds.
procedure
( delete-root treap)→(Treap A)
- : Symbol
'a
Added in version 0.1 of package pfds.