-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproblem56.ml
41 lines (38 loc) · 1.13 KB
/
problem56.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(*# P56 (**) Symmetric binary trees
#
# Let us call a binary tree symmetric if you can draw a vertical line through the
# root node and then the right subtree is the mirror image of the left subtree.
# Write a predicate symmetric/1 to check whether a given binary tree is
# symmetric. Hint: Write a predicate mirror/2 first to check whether one tree is
# the mirror image of another. We are only interested in the structure, not in
# the contents of the nodes.*)
type bin_tree =
Leave of string
| Node of string * bin_tree * bin_tree
;;
let symmetric t =
let rec mirrored t1 t2 =
match t1 with
Leave a ->
begin
match t2 with
Leave b -> a = b
| Node(a,t21,t22) -> false
end
| Node(a,t11,t12) ->
begin
match t2 with
Leave b -> false
| Node(b,t21,t22) ->
( a = b ) &&
( mirrored t11 t22 ) &&
( mirrored t12 t21 )
end
in
match t with
Leave a -> true
| Node(a,t1,t2) -> mirrored t1 t2
;;
if ( symmetric ( Node ( "a", Node ( "c", Leave "b", Leave "a") , Node ( "c", Leave "a", Leave "b" ) ) ) )
then ( Printf.printf "ok\n" )
else ( Printf.printf "failed\n" );;