added filter to leftistheap

This commit is contained in:
Simon Cruanes 2013-08-20 12:15:35 +02:00
parent 1d39fd1d5b
commit 32a59b7982
3 changed files with 14 additions and 0 deletions

View file

@ -24,3 +24,4 @@ RAL
MultiSet MultiSet
UnionFind UnionFind
SmallSet SmallSet
Leftistheap

View file

@ -79,6 +79,16 @@ let insert heap x =
let tree = merge_tree heap.leq (Node (1, x, Empty, Empty)) heap.tree in let tree = merge_tree heap.leq (Node (1, x, Empty, Empty)) heap.tree in
{ heap with tree; } { heap with tree; }
let filter heap p =
let rec filter tree p = match tree with
| Empty -> Empty
| Node (_, x, l, r) when p x ->
merge_tree heap.leq (Node (1, x, Empty, Empty))
(merge_tree heap.leq (filter l p) (filter r p))
| Node (_, _, l, r) -> merge_tree heap.leq (filter l p) (filter r p)
in
{ heap with tree = filter heap.tree p; }
let find_min heap = let find_min heap =
match heap.tree with match heap.tree with
| Empty -> raise Not_found | Empty -> raise Not_found

View file

@ -46,6 +46,9 @@ val merge : 'a t -> 'a t -> 'a t
val insert : 'a t -> 'a -> 'a t val insert : 'a t -> 'a -> 'a t
(** Insert a value in the heap *) (** Insert a value in the heap *)
val filter : 'a t -> ('a -> bool) -> 'a t
(** Filter values, only retaining the ones that satisfy the predicate *)
val find_min : 'a t -> 'a val find_min : 'a t -> 'a
(** Find minimal element, or raise Not_found *) (** Find minimal element, or raise Not_found *)