Cookies disclaimer

I agree Our site saves small pieces of text information (cookies) on your device in order to deliver better content and for statistical purposes. You can disable the usage of cookies by changing the settings of your browser. By browsing our website without changing the browser settings you grant us permission to store that information on your device.

Homework 7

This is the task corresponding to homework 7.


Download Files

Definitions File

theory Defs
  imports "HOL-Library.Tree"

definition lefts::"'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
  where "lefts y xs = takeWhile (\<lambda>x. x \<noteq> y) xs"

definition rights::"'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
  where "rights y xs = tl (dropWhile (\<lambda>x. x \<noteq> y) xs)"

fun reconstruct::"'a list \<Rightarrow> 'a list \<Rightarrow> 'a tree"
  "reconstruct [] [] = \<langle>\<rangle>"
| "reconstruct (y#ys) zs =
      ls = lefts y zs;
      rs = rights y zs
    in \<langle>reconstruct (filter (\<lambda>y. y \<in> set ls) ys) ls, y,
        reconstruct (filter (\<lambda>y. y \<in> set rs) ys) rs\<rangle>)"


Template File

theory Submission
  imports Defs

thm preorder.simps
thm inorder.simps

value "reconstruct [3, 1, 6, 5, 8, 7, 9::int] [1, 3, 5, 6, 7, 8, 9]"
value "reconstruct [5, 3, 4, 1, 2] [1, 2, 3, 4, 5::int]"

theorem reconstruct_correct:
  "distinct (preorder t) \<Longrightarrow> reconstruct (preorder t) (inorder t) = t"


Check File

theory Check
  imports Submission

theorem reconstruct_correct: "distinct (preorder t) \<Longrightarrow> reconstruct (preorder t) (inorder t) = t"
  by (rule Submission.reconstruct_correct)


Terms and Conditions