Sunday, November 22, 2015

I've got 99 problems - and OCaml is all of them (Multiway trees)

Multiway Trees



# type 'a mult_tree = T of 'a * 'a mult_tree list;;
type 'a mult_tree = T of 'a * 'a mult_tree list

Count the nodes of a multiway tree. (easy)


let rec count_nodes tree =
let rec count_list = function
| [] -> 0
| h :: t -> count_nodes h + count_list t
in
match tree with
| T (_, c) -> 1 + count_list c;;

Tree construction from a node string. (medium)


let rec string_of_tree tree =
let rec aux = function
| [] -> ""
| h :: t -> string_of_tree h ^ aux t
in
match tree with
| T (v, c) -> String.make 1 v ^ aux c ^ "^";;

No comments:

Post a Comment