mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
added filter to leftistheap
This commit is contained in:
parent
1d39fd1d5b
commit
32a59b7982
3 changed files with 14 additions and 0 deletions
|
|
@ -24,3 +24,4 @@ RAL
|
||||||
MultiSet
|
MultiSet
|
||||||
UnionFind
|
UnionFind
|
||||||
SmallSet
|
SmallSet
|
||||||
|
Leftistheap
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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 *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue