Module Splay_tree0.Make_without_reduction
Parameters
Signature
include Ppx_sexp_conv_lib.Sexpable.S with type t := t
val t_of_sexp : Sexplib0.Sexp.t -> tval sexp_of_t : t -> Sexplib0.Sexp.t
type key= Key.t
val sexp_of_key : key -> Ppx_sexp_conv_lib.Sexp.tval key_of_sexp : Ppx_sexp_conv_lib.Sexp.t -> key
type data= Data.t
val sexp_of_data : data -> Ppx_sexp_conv_lib.Sexp.tval data_of_sexp : Ppx_sexp_conv_lib.Sexp.t -> data
val empty : tval of_alist : (key * data) list -> t Core_kernel.Or_error.tval of_alist_exn : (key * data) list -> tval to_alist : t -> (key * data) listval is_empty : t -> boolval length : t -> intval accum : t -> accumval keys : t -> key listval data : t -> data listval mem : t -> key -> boolval find : t -> key -> data optionval set : t -> key:key -> data:data -> tval remove : t -> key -> tval remove_min : t -> (key * data * t) optionval remove_max : t -> (key * data * t) optionval remove_after : t -> key -> (key * data * t) optionval remove_before : t -> key -> (key * data * t) optionval map : t -> f:(data -> data) -> tval map_range : t -> min_key:key -> max_key:key -> f:((key * data) list -> (key * data) list) -> tval nth : t -> int -> (key * data) optionval rank : t -> key -> intrank t keyis the number of nodes before wherekeywould be inserted. In other words, the length of the left subtree after asplit t key.
val search : t -> f:(left:accum -> right:accum -> [ `Right | `Left ]) -> (key * data) optionsearchimplements bisection search overtbased on theaccumvalues of its prefixes.Let's consider a
tconsisting of four elementsa; b; c; d(see diagram below) (we'll refer to all of key, data, and accum ofaas justa; we'll also assumeR.combine = (+)).Then there are five possible positions to evaluate
fat (numbered 0..4 below):- | position: 0 1 2 3 4 | ------------------------- | element: | a | b | c | d | | ------------------------- | left: 0 a a+b a+b+c a+b+c+d | right: a+b+c+d b+c+d c+d d 0 | f ~left ~right: R R R L L
The function
f ~left ~rightwill be called for a particular position and it takes the sum of the elements to the left and to the right of this position. This meansleft + rightwill be equal toaccum t.The return value of
fmust indicate the direction where the desired element is. In the diagram abovef ~left:(a+b) ~right:(c+d)returns`Right, whereasf ~left:(a+b+c) ~right:dreturns`Left, which makescthe desired element.Therefore,
searchwill returnc. Iffreturns the same value at all positions,searchreturnsNone.For it to make sense,
fmust be monotonic: callingfon every possible position from left to right should produce a prefix of`Rightvalues followed by a suffix of`Leftvalues.Example:
If the values are positive integers and reduction operation is sum, then you can find the last node where the prefix sum before it is at most
xwith the followingf:let f ~left ~right:_ = if x < left then `Left else `Right
module Partition : sig ... endval partition : ?min_key:key -> ?max_key:key -> t -> Partition.tval subrange : ?min_key:key -> ?max_key:key -> t -> tsubrange t ?min_key ?max_keyis equivalent to themidinpartition
val merge : t -> t -> f:(key:key -> [ `Left of data | `Right of data | `Both of data * data ] -> data option) -> tval split : t -> key -> t * data option * tval join : t -> t -> t Core_kernel.Or_error.tjoin t1 t2directly concatenates the sequences described byt1andt2. This should be used to rejoin trees obtained by a split operation, though it can be used for other things.This operation can fail if the concatenation of the two sequences is invalid; i.e. the keys are not in order. This happens when the maximum key of
t1is at or above the minimum key oft2.Currently the cost of
joinis not fully amortized, so it should be considered worst-case linear time.