14526
|
1 |
(*<*)
|
|
2 |
theory a2 = Main:
|
|
3 |
(*>*)
|
|
4 |
|
|
5 |
subsection {* Trees *}
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
text{* Define a datatype @{text"'a tree"} for binary trees. Both leaf
|
|
10 |
and internal nodes store information.
|
|
11 |
*};
|
|
12 |
|
|
13 |
datatype 'a tree(*<*)= Tip "'a" | Node "'a" "'a tree" "'a tree"(*>*)
|
|
14 |
|
|
15 |
text{*
|
|
16 |
Define the functions @{term preOrder}, @{term postOrder}, and @{term
|
|
17 |
inOrder} that traverse an @{text"'a tree"} in the respective order.
|
|
18 |
*}
|
|
19 |
|
|
20 |
(*<*)consts(*>*)
|
|
21 |
preOrder :: "'a tree \<Rightarrow> 'a list"
|
|
22 |
postOrder :: "'a tree \<Rightarrow> 'a list"
|
|
23 |
inOrder :: "'a tree \<Rightarrow> 'a list"
|
|
24 |
|
|
25 |
text{*
|
|
26 |
Define a function @{term mirror} that returns the mirror image of an @{text"'a tree"}.
|
|
27 |
*};
|
|
28 |
(*<*)consts(*>*)
|
|
29 |
mirror :: "'a tree \<Rightarrow> 'a tree"
|
|
30 |
|
|
31 |
text{*
|
|
32 |
Suppose that @{term xOrder} and @{term yOrder} are tree traversal
|
|
33 |
functions chosen from @{term preOrder}, @{term postOrder}, and @{term
|
|
34 |
inOrder}. Formulate and prove all valid properties of the form
|
|
35 |
\mbox{@{text "xOrder (mirror xt) = rev (yOrder xt)"}}.
|
|
36 |
*}
|
|
37 |
|
|
38 |
text{*
|
|
39 |
Define the functions @{term root}, @{term leftmost} and @{term
|
|
40 |
rightmost}, that return the root, leftmost, and rightmost element
|
|
41 |
respectively.
|
|
42 |
*}
|
|
43 |
(*<*)consts(*>*)
|
|
44 |
root :: "'a tree \<Rightarrow> 'a"
|
|
45 |
leftmost :: "'a tree \<Rightarrow> 'a"
|
|
46 |
rightmost :: "'a tree \<Rightarrow> 'a"
|
|
47 |
|
|
48 |
text {*
|
|
49 |
Prove or disprove (by counter example) the theorems that follow. You may have to prove some lemmas first.
|
|
50 |
*};
|
|
51 |
|
|
52 |
theorem "last(inOrder xt) = rightmost xt"
|
|
53 |
(*<*)oops(*>*)
|
|
54 |
theorem "hd (inOrder xt) = leftmost xt "
|
|
55 |
(*<*)oops(*>*)
|
|
56 |
theorem "hd(preOrder xt) = last(postOrder xt)"
|
|
57 |
(*<*)oops(*>*)
|
|
58 |
theorem "hd(preOrder xt) = root xt"
|
|
59 |
(*<*)oops(*>*)
|
|
60 |
theorem "hd(inOrder xt) = root xt "
|
|
61 |
(*<*)oops(*>*)
|
|
62 |
theorem "last(postOrder xt) = root xt"
|
|
63 |
(*<*)oops(*>*)
|
|
64 |
|
|
65 |
|
|
66 |
(*<*)end(*>*)
|