Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn-exercise-065-into-practice #2265

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added practice/065/:::
Empty file.
23 changes: 23 additions & 0 deletions practice/065/answer/impl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type 'a binary_tree =
| Empty
| Node of 'a * 'a binary_tree * 'a binary_tree

let layout_binary_tree_2 t =
let rec height = function
| Empty -> 0
| Node (_, l, r) -> 1 + max (height l) (height r) in
let tree_height = height t in
let rec find_missing_left depth = function
| Empty -> tree_height - depth
| Node (_, l, _) -> find_missing_left (depth + 1) l in
let translate_dst = 1 lsl (find_missing_left 0 t) - 1 in
(* remember than 1 lsl a = 2ᵃ *)
let rec layout depth x_root = function
| Empty -> Empty
| Node (x, l, r) ->
let spacing = 1 lsl (tree_height - depth - 1) in
let l' = layout (depth + 1) (x_root - spacing) l
and r' = layout (depth + 1) (x_root + spacing) r in
Node((x, x_root, depth), l',r') in
layout 1 ((1 lsl (tree_height - 1)) - translate_dst) t

2 changes: 2 additions & 0 deletions practice/065/answer/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(include_subdirs no)
(test (name run) (libraries ounit2 ex))
3 changes: 3 additions & 0 deletions practice/065/answer/test/run.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Test = Ex.Make(Ex.Answer)

let () = OUnit2.run_test_tt_main Test.v
2 changes: 2 additions & 0 deletions practice/065/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(include_subdirs qualified)
(library (name ex) (libraries ounit2))
1 change: 1 addition & 0 deletions practice/065/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.7)
23 changes: 23 additions & 0 deletions practice/065/ex.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
open OUnit2

module type Testable = sig
type 'a binary_tree = | Empty | Node of 'a * 'a binary_tree * 'a binary_tree
val layout_binary_tree_2 : 'a binary_tree -> ('a * int * int) binary_tree
end

module Make(Tested: Testable) : sig val v : OUnit2.test end = struct
open Tested

let v = "layout_binary_tree_2" >::: [
"test_layout_binary_tree_2_works_correctly" >:: (fun _ ->
let example_layout_tree =
Node ('a', Node ('b', Empty, Empty), Node ('c', Empty, Empty)) in
let expected_layout =
Node (('a', 2, 1), Node (('b', 1, 2), Empty, Empty), Node (('c', 3, 2), Empty, Empty)) in
assert_equal expected_layout (layout_binary_tree_2 example_layout_tree));
]
end

module Work : Testable = Work.Impl

module Answer : Testable = Answer.Impl
5 changes: 5 additions & 0 deletions practice/065/work/impl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type 'a binary_tree =
| Empty
| Node of 'a * 'a binary_tree * 'a binary_tree

let layout_binary_tree_2 _ = failwith "Not yet implemented"
2 changes: 2 additions & 0 deletions practice/065/work/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(include_subdirs no)
(test (name run) (libraries ounit2 ex))
3 changes: 3 additions & 0 deletions practice/065/work/test/run.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Test = Ex.Make(Ex.Work)

let () = OUnit2.run_test_tt_main Test.v
Loading