Commit 522ff6e9 authored by Andreas Pfurtscheller's avatar Andreas Pfurtscheller

Add HW08 馃搫

parent d94ed9a4
......@@ -8,34 +8,57 @@ type 'a ltree = LNode of 'a * (unit -> 'a ltree) * (unit -> 'a ltree)
(**************************** HOMEWORK STARTS HERE ***************************)
(*****************************************************************************)
(* Assignment 8.5 [3 Points] *)
let interleave3 = todo
let interleave3 l1 l2 l3 = let rec aux l1 l2 l3 acc = match l1, l2, l3 with
| [], [], [] -> acc
| [], l2, l3 -> (aux l2 l3 [] acc)
| h::tl, l2, l3 -> (aux l2 l3 tl (h::acc)) in
List.rev (aux l1 l2 l3 [])
(*****************************************************************************)
(* Assignment 8.6 [4 Points] *)
let lagrange = todo
(*****************************************************************************)
(* Assignment 8.7 [6 Points] *)
let insert = todo
let calc_supp_point x xj xk = (x -. xk) /. (xj -. xk)
let string_of_tree = todo
let calc_lagrange_pol x xj p = (List.fold_left (fun c (xk,_) -> if xj=xk then c else c *. (calc_supp_point x xj xk)) 1.0 p)
let inorder_list = todo
let lagrange p = fun x -> (List.fold_left (fun c (xj,yj) -> c +. (yj *. (calc_lagrange_pol x xj p))) 0.0 p)
(*****************************************************************************)
(* Assignment 8.8 [7 Points] *)
let layer_tree = todo
(* Assignment 8.7 [6 Points] *)
let rec insert v c t = match t with
| Empty -> Node (v, Empty, Empty)
| Node (x, l, r) -> let cmp = (c v x) in
if cmp < 0 then Node (x, (insert v c l), r)
else if cmp > 0 then Node (x, l, (insert v c r))
else Node (x, l, r)
let rec string_of_tree s t = match t with
| Empty -> "Empty"
| Node (x, l, r) -> "Node (" ^ (s x) ^ ", " ^ (string_of_tree s l) ^ ", " ^ (string_of_tree s r) ^ ")"
let inorder_list t = let rec aux t acc = match t with
| Empty -> acc
| Node (x, l, r) -> aux l (x::(aux r acc)) in
aux t []
let interval_tree = todo
(*****************************************************************************)
(* Assignment 8.8 [7 Points] *)
let layer_tree r = let rec aux c = fun _ -> (LNode (c, (aux (c+1)), (aux (c+1)))) in (aux r) ()
let rational_tree = todo
let interval_tree i = let rec aux (l,h) = fun _ -> (LNode ((l,h), (aux (l,(l+.h)/.2.)), (aux ((l+.h)/.2.,h)))) in (aux i) ()
let top = todo
let rational_tree _ = let rec aux (n,d) = fun _ -> (LNode ((n,d), (aux (n,d+1)), (aux (n+1,d)))) in (aux (0,0)) ()
let map = todo
let rec top n t = match n, t with
| 0, _ -> Empty
| _, LNode (v, l, r) -> Node(v, (top (n-1) (l ())), (top (n-1) (r ())))
let find = todo
let rec map m t = let rec aux m (LNode (v, l, r)) = fun _ -> LNode ((m v), (aux m (l ())), (aux m (r ()))) in (aux m t) ()
let find f n = let rec aux f ((LNode (v, l, r))::tl) = if (f v)
then LNode (v, l, r)
else aux f (tl@[l();r()]) in
aux f [n]
(*****************************************************************************)
(**************************** END OF HOMEWORK ********************************)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment