Ćwiczenia na drzewa z elementami procedur wyższych rzędów:
tree i procedura fold_tree:
type 'a tree = Node of 'a * 'a tree list;;
let rec fold_tree f (Node (x, l)) =
f x (map (fold_tree f) l);;
Użyj procedury fold_tree do zaimplementowania:
preorder/postorder : 'a tree -> 'a list, która przekształca dane drzewo w listę jego elementów w porządku pre-order/post-order (zwróć uwagę na efektywność).bin_tree i procedura fold_bin_tree:
type 'a bin_tree = Node of 'a bin_tree * 'a * 'a bin_tree | Null;;
let rec fold_bin_tree f a t =
match t with
Null -> a |
Node (l, x, r) -> f x (fold_bin_tree f a l) (fold_bin_tree f a r);;
Użyj procedury fold_bin_tree do zaimplementowania:
float tree),map_bin_tree -- odpowiednika procedury map_tree dla drzew binarnych,