minor update of Leftistheap

This commit is contained in:
Simon Cruanes 2013-04-24 00:31:47 +02:00
parent b6212bea7c
commit d0bb88e365
3 changed files with 17 additions and 7 deletions

View file

@ -23,7 +23,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*) *)
(** {1 Leftist Heaps *) (** {1 Leftist Heaps} *)
(** Polymorphic implementation, following Okasaki *) (** Polymorphic implementation, following Okasaki *)
@ -36,11 +36,16 @@ and 'a tree =
| Empty | Empty
| Node of int * 'a * 'a tree * 'a tree | Node of int * 'a * 'a tree * 'a tree
let empty ~leq = let empty_with ~leq =
{ tree = Empty; { tree = Empty;
leq; leq;
} }
let empty =
{ tree = Empty;
leq = (fun x y -> x <= y);
}
let is_empty heap = let is_empty heap =
match heap.tree with match heap.tree with
| Empty -> true | Empty -> true

View file

@ -23,16 +23,19 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*) *)
(** {1 Leftist Heaps *) (** {1 Leftist Heaps} *)
(** Polymorphic implementation, following Okasaki *) (** Polymorphic implementation, following Okasaki *)
type 'a t type 'a t
(** Heap containing values of type 'a *) (** Heap containing values of type 'a *)
val empty : leq:('a -> 'a -> bool) -> 'a t val empty_with : leq:('a -> 'a -> bool) -> 'a t
(** Empty heap. The function is used to check whether (** Empty heap. The function is used to check whether the first element is
the first element is smaller than the second. *) smaller than the second. *)
val empty : 'a t
(** Empty heap using Pervasives.compare *)
val is_empty : _ t -> bool val is_empty : _ t -> bool
(** Is the heap empty? *) (** Is the heap empty? *)

View file

@ -3,7 +3,7 @@
open OUnit open OUnit
let empty = Leftistheap.empty ~leq:(fun i j -> i <= j) let empty = Leftistheap.empty
let test1 () = let test1 () =
let h = Leftistheap.of_seq empty (Sequence.of_list [5;3;4;1;42;0]) in let h = Leftistheap.of_seq empty (Sequence.of_list [5;3;4;1;42;0]) in
@ -27,6 +27,7 @@ let rec is_sorted l = match l with
| [] -> true | [] -> true
| x::((y::_) as l') -> x <= y && is_sorted l' | x::((y::_) as l') -> x <= y && is_sorted l'
(* extract the content of the heap into a list *)
let extract_list heap = let extract_list heap =
let rec recurse acc h = let rec recurse acc h =
if Leftistheap.is_empty h if Leftistheap.is_empty h
@ -43,6 +44,7 @@ let test_sort () =
let l = Sequence.to_rev_list (Sequence.take n (Sequence.random_int n)) in let l = Sequence.to_rev_list (Sequence.take n (Sequence.random_int n)) in
(* put elements into a heap *) (* put elements into a heap *)
let h = Leftistheap.of_seq empty (Sequence.of_list l) in let h = Leftistheap.of_seq empty (Sequence.of_list l) in
OUnit.assert_equal n (Leftistheap.size h);
let l' = extract_list h in let l' = extract_list h in
OUnit.assert_bool "sorted" (is_sorted l'); OUnit.assert_bool "sorted" (is_sorted l');
() ()