exception Empty
module type ORDERED = sig
type t
end
module type HEAP = sig
module Elem : ORDERED
type heap
val empty : heap
val is_empty
: heap
-> bool
val insert : Elem.t -> heap -> heap
val merge : heap -> heap -> heap
val find_min : heap -> Elem.t
val delete_min : heap -> heap
end
module SplayHeap (Element : ORDERED) : (HEAP with module Elem = Element) =
struct
module Elem = Element
type heap = E | T of heap * Elem.t * heap
let empty = E
let is_empty h = h = E
let partition pivot E = (E,E)
let partition pivot t as (T a x b) =
if x <= pivot then
case b of
E -> (t,E)
T b' y b'' ->
if y <= pivot then
let (small,big) = partition pivot b''
in (T (T a x b) y small, big)
else
let (small,big) = partition pivot b'
in (T a x small, T big y b'')
else
case a of
E -> (E,t)
T a' y a'' ->
if y <= pivot then
let (small,big) = partition pivot a''
in (T a' y small, T big x b)
else
let (small, big) = partition pivot a'
in (small, T big y (T a'' x b)
end
ZXhjZXB0aW9uIEVtcHR5Cgptb2R1bGUgdHlwZSBPUkRFUkVEID0gc2lnCiAgICB0eXBlIHQKCiAgICB2YWwgZXEgOiB0IC0+IHQgLT4gYm9vbAogICAgdmFsIGx0IDogdCAtPiB0IC0+IGJvb2wKICAgIHZhbCBsZXEgOiB0IC0+IHQgLT4gYm9vbAplbmQKCm1vZHVsZSB0eXBlIEhFQVAgPSBzaWcKICAgIG1vZHVsZSBFbGVtIDogT1JERVJFRAoKICAgIHR5cGUgaGVhcAoKICAgIHZhbCBlbXB0eSA6IGhlYXAKICAgIHZhbCBpc19lbXB0eSA6IGhlYXAgLT4gYm9vbAoKICAgIHZhbCBpbnNlcnQgOiBFbGVtLnQgLT4gaGVhcCAtPiBoZWFwCiAgICB2YWwgbWVyZ2UgOiBoZWFwIC0+IGhlYXAgLT4gaGVhcAoKICAgIHZhbCBmaW5kX21pbiA6IGhlYXAgLT4gRWxlbS50CiAgICB2YWwgZGVsZXRlX21pbiA6IGhlYXAgLT4gaGVhcAplbmQKCm1vZHVsZSBTcGxheUhlYXAgKEVsZW1lbnQgOiBPUkRFUkVEKSA6IChIRUFQIHdpdGggbW9kdWxlIEVsZW0gPSBFbGVtZW50KSA9CnN0cnVjdAogIG1vZHVsZSBFbGVtID0gRWxlbWVudAoKICB0eXBlIGhlYXAgPSBFIHwgVCBvZiBoZWFwICogRWxlbS50ICogaGVhcAoKICBsZXQgZW1wdHkgPSBFCiAgbGV0IGlzX2VtcHR5IGggPSBoID0gRQoKICBsZXQgcGFydGl0aW9uIHBpdm90IEUgPSAoRSxFKQogIGxldCBwYXJ0aXRpb24gcGl2b3QgdCBhcyAoVCBhIHggYikgPQogICAgICBpZiB4IDw9IHBpdm90IHRoZW4KICAgICAgICAgIGNhc2UgYiBvZgogICAgICAgICAgICBFIC0+ICh0LEUpCiAgICAgICAgICAgIFQgYicgeSBiJycgLT4KICAgICAgICAgICAgICAgIGlmIHkgPD0gcGl2b3QgdGhlbgogICAgICAgICAgICAgICAgICAgIGxldCAoc21hbGwsYmlnKSA9IHBhcnRpdGlvbiBwaXZvdCBiJycKICAgICAgICAgICAgICAgICAgICBpbiAoVCAoVCBhIHggYikgeSBzbWFsbCwgYmlnKQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgIGxldCAoc21hbGwsYmlnKSA9IHBhcnRpdGlvbiBwaXZvdCBiJwogICAgICAgICAgICAgICAgICAgIGluIChUIGEgeCBzbWFsbCwgVCBiaWcgeSBiJycpCiAgICAgIGVsc2UKICAgICAgICAgIGNhc2UgYSBvZgogICAgICAgICAgICBFIC0+IChFLHQpCiAgICAgICAgICAgIFQgYScgeSBhJycgLT4KICAgICAgICAgICAgICAgIGlmIHkgPD0gcGl2b3QgdGhlbgogICAgICAgICAgICAgICAgICAgIGxldCAoc21hbGwsYmlnKSA9IHBhcnRpdGlvbiBwaXZvdCBhJycKICAgICAgICAgICAgICAgICAgICBpbiAoVCBhJyB5IHNtYWxsLCBUIGJpZyB4IGIpCiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgbGV0IChzbWFsbCwgYmlnKSA9IHBhcnRpdGlvbiBwaXZvdCBhJwogICAgICAgICAgICAgICAgICAgIGluIChzbWFsbCwgVCBiaWcgeSAoVCBhJycgeCBiKQoKCmVuZA==