doc-src/TutorialI/Misc/Tree.thy
author nipkow
Thu, 29 May 2008 22:45:33 +0200
changeset 27015 f8537d69f514
parent 16417 9bc16273c2d4
permissions -rw-r--r--
*** empty log message ***
     1 (*<*)
     2 theory Tree imports Main begin
     3 (*>*)
     4 
     5 text{*\noindent
     6 Define the datatype of \rmindex{binary trees}:
     7 *}
     8 
     9 datatype 'a tree = Tip | Node "'a tree" 'a "'a tree";(*<*)
    10 
    11 primrec mirror :: "'a tree \<Rightarrow> 'a tree" where
    12 "mirror Tip = Tip" |
    13 "mirror (Node l x r) = Node (mirror r) x (mirror l)";(*>*)
    14 
    15 text{*\noindent
    16 Define a function @{term"mirror"} that mirrors a binary tree
    17 by swapping subtrees recursively. Prove
    18 *}
    19 
    20 lemma mirror_mirror: "mirror(mirror t) = t";
    21 (*<*)
    22 apply(induct_tac t);
    23 by(auto);
    24 
    25 primrec flatten :: "'a tree => 'a list" where
    26 "flatten Tip = []" |
    27 "flatten (Node l x r) = flatten l @ [x] @ flatten r";
    28 (*>*)
    29 
    30 text{*\noindent
    31 Define a function @{term"flatten"} that flattens a tree into a list
    32 by traversing it in infix order. Prove
    33 *}
    34 
    35 lemma "flatten(mirror t) = rev(flatten t)";
    36 (*<*)
    37 apply(induct_tac t);
    38 by(auto);
    39 
    40 end
    41 (*>*)