tree map
This commit is contained in:
parent
bf2cae7c4e
commit
d9940d81d0
@ -1,5 +1,10 @@
|
|||||||
open List
|
open FromStdlib
|
||||||
open Types
|
open Types
|
||||||
|
|
||||||
let combine (tr1 : 'a tree) (tr2 : 'a tree) (topBranch : 'a) : 'a tree =
|
let combine (tr1 : 'a tree) (tr2 : 'a tree) (topBranch : 'a) : 'a tree =
|
||||||
Branch (topBranch, tr1 :: tr2 :: [])
|
Branch (topBranch, tr1 :: tr2 :: [])
|
||||||
|
|
||||||
|
let rec map (f : 'a -> 'b) (tr : 'a tree) : 'b tree =
|
||||||
|
match tr with
|
||||||
|
| Branch (value, branches) -> Branch (f value, branches |> List.map (map f))
|
||||||
|
| Leaf -> Leaf
|
@ -2,3 +2,6 @@ open Types
|
|||||||
|
|
||||||
(* Combines two trees of the same type, with the specified value at the new top node. Runs in O(1). *)
|
(* Combines two trees of the same type, with the specified value at the new top node. Runs in O(1). *)
|
||||||
val combine : 'a tree -> 'a tree -> 'a -> 'a tree
|
val combine : 'a tree -> 'a tree -> 'a -> 'a tree
|
||||||
|
|
||||||
|
(** Applies the provided function to each element of the tree. Does not use constant stack space. Runs in O(n). *)
|
||||||
|
val map : ('a -> 'b) -> 'a tree -> 'b tree
|
Loading…
Reference in New Issue
Block a user