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 α t
type α bst = α t
datatype α view =
LEAF
| NODE of { key : Key.t
, value : α
, left : α bst
, right : α bst }
exception Order
val expose : α bst → α view
val size : α bst → int
val empty : unit → α bst
val singleton : Key.t * α → α bst
val join : α bst * α bst → α bst
val joinMid : α bst * (Key.t * α) * α bst → α bst
val split : α bst * Key.t → α bst * α option * α bst
val $ : Key.t * α → α bst
structure Key : ORDKEY
Key
substructure defines the key type of a binary search
tree, providing comparison and other useful functions on keys.
type α t
type α bst = α t
type α view =
LEAF | NODE of {key : Key.t, value : α, left : α bst, right : α bst}
exception Order
Order
is raised whenever tree operations would violate the ordering invariant.val expose :
α bst → α view
val size :
α bst → int
val empty :
unit → α bst
val singleton :
Key.t * α → α bst
val join :
α bst * α bst → α bst
(join (A, B))
evaluates to the union
of $A$ and $B$.val joinMid :
α bst * (Key.t * α) * α bst → α bst
(joinMid (A, (k, v), B))
is logically equivalent to
(join (A, join (singleton (k, v), B))).
val split :
α bst * Key.t → α bst * α option * α bst
(split (T, k))
evaluates to $(L, x, R)$
where $L$ is a bst containing all keys less than $k$, $R$
is a bst containing all keys greater than $k$, and $x$ is
the value associated with $k$ (or NONE
, if $k \not\in T$).val $ :
Key.t * α → α bst
singleton
.