src/Doc/Tutorial/Misc/Tree.thy
changeset 48985 5386df44a037
parent 27015 f8537d69f514
child 58860 fee7cfa69c50
equal deleted inserted replaced
48984:f51d4a302962 48985:5386df44a037
       
     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 (*>*)