BST
signature
The BST
signature is a minimalistic interface for a binary
search tree, which is a set of key-value pairs. The key type is ordered
and fixed by the Key
substructure
while the value type is polymorphic.
structure Key : ORDKEY
type 'a t
type 'a bst = 'a t
datatype 'a view =
LEAF
| NODE of { key : Key.t
, value : 'a
, left : 'a bst
, right : 'a bst }
exception Order
val expose : 'a bst → 'a view
val size : 'a bst → int
val empty : unit → 'a bst
val singleton : Key.t * 'a → 'a bst
val join : 'a bst * 'a bst → 'a bst
val joinMid : 'a bst * (Key.t * 'a) * 'a bst → 'a bst
val split : 'a bst * Key.t → 'a bst * 'a option * 'a bst
val $ : Key.t * 'a → 'a bst
structure Key : ORDKEY
Key
substructure defines the key type of a binary search
tree, providing comparison and other useful functions on keys.
type 'a t
type 'a bst = 'a t
'a t
maps keys of type
Key.t
to values of type 'a
.
The alias 'a bst
is for readability.
type 'a view =
LEAF | NODE of {key : Key.t, value : 'a, left : 'a bst, right : 'a bst}
exception Order
Order
is raised when the ordering invariant would be violated.val expose :
'a bst → 'a view
val size :
'a bst → int
val empty :
unit → 'a bst
val singleton :
Key.t * 'a → 'a bst
val join :
'a bst * 'a bst → 'a bst
join (A,B)
is the union of all
key-value pairs from both $A$ and $B$. Raises Order
otherwise.
val joinMid :
'a bst * (Key.t * 'a) * 'a bst → 'a bst
joinMid (A, (k, v), B)
is logically equivalent to
join (A, join (singleton (k, v), B)).
val split :
'a bst * Key.t → 'a bst * 'a option * 'a bst
split (T, k)
evaluates to $(L, v, R)$
where $L$ is a bst containing all keys from $T$ which are smaller than $k$, $R$
is a bst containing all keys larger than $k$, and $v$ is
the value associated with $k$ (or NONE
if $k$ is not in $T$).val $ :
Key.t * 'a → 'a bst
singleton
.