This commit is contained in:
Aaron Manning 2021-12-17 07:23:03 +11:00 committed by aaron-jack-manning
parent bf2cae7c4e
commit d9940d81d0
3 changed files with 12 additions and 4 deletions

View File

@ -1,5 +1,10 @@
open List
open FromStdlib
open Types
let combine (tr1 : 'a tree) (tr2 : 'a tree) (topBranch : 'a) : 'a tree =
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

View File

@ -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). *)
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