src/HOL/Library/Tree.thy
author wenzelm
Wed Jun 17 11:03:05 2015 +0200 (2015-06-17)
changeset 60500 903bb1495239
parent 59928 b9b7f913a19a
child 60506 83231b558ce4
permissions -rw-r--r--
isabelle update_cartouches;
     1 (* Author: Tobias Nipkow *)
     2 
     3 section \<open>Binary Tree\<close>
     4 
     5 theory Tree
     6 imports Main
     7 begin
     8 
     9 datatype 'a tree =
    10   Leaf ("\<langle>\<rangle>") |
    11   Node (left: "'a tree") (val: 'a) (right: "'a tree") ("\<langle>_, _, _\<rangle>")
    12   where
    13     "left Leaf = Leaf"
    14   | "right Leaf = Leaf"
    15 datatype_compat tree
    16 
    17 text\<open>Can be seen as counting the number of leaves rather than nodes:\<close>
    18 
    19 definition size1 :: "'a tree \<Rightarrow> nat" where
    20 "size1 t = size t + 1"
    21 
    22 lemma size1_simps[simp]:
    23   "size1 \<langle>\<rangle> = 1"
    24   "size1 \<langle>l, x, r\<rangle> = size1 l + size1 r"
    25 by (simp_all add: size1_def)
    26 
    27 lemma neq_Leaf_iff: "(t \<noteq> \<langle>\<rangle>) = (\<exists>l a r. t = \<langle>l, a, r\<rangle>)"
    28 by (cases t) auto
    29 
    30 lemma finite_set_tree[simp]: "finite(set_tree t)"
    31 by(induction t) auto
    32 
    33 lemma size_map_tree[simp]: "size (map_tree f t) = size t"
    34 by (induction t) auto
    35 
    36 lemma size1_map_tree[simp]: "size1 (map_tree f t) = size1 t"
    37 by (simp add: size1_def)
    38 
    39 
    40 subsection "The depth"
    41 
    42 fun depth :: "'a tree => nat" where
    43 "depth Leaf = 0" |
    44 "depth (Node t1 a t2) = Suc (max (depth t1) (depth t2))"
    45 
    46 lemma depth_map_tree[simp]: "depth (map_tree f t) = depth t"
    47 by (induction t) auto
    48 
    49 
    50 subsection "The set of subtrees"
    51 
    52 fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
    53   "subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
    54   "subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)"
    55 
    56 lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. \<langle>l, a, r\<rangle> \<in> subtrees t"
    57 by (induction t)(auto)
    58 
    59 lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t"
    60 by (induction t) auto
    61 
    62 lemma in_set_tree_if: "\<langle>l, a, r\<rangle> \<in> subtrees t \<Longrightarrow> a \<in> set_tree t"
    63 by (metis Node_notin_subtrees_if)
    64 
    65 
    66 subsection "List of entries"
    67 
    68 fun preorder :: "'a tree \<Rightarrow> 'a list" where
    69 "preorder \<langle>\<rangle> = []" |
    70 "preorder \<langle>l, x, r\<rangle> = x # preorder l @ preorder r"
    71 
    72 fun inorder :: "'a tree \<Rightarrow> 'a list" where
    73 "inorder \<langle>\<rangle> = []" |
    74 "inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r"
    75 
    76 lemma set_inorder[simp]: "set (inorder t) = set_tree t"
    77 by (induction t) auto
    78 
    79 lemma set_preorder[simp]: "set (preorder t) = set_tree t"
    80 by (induction t) auto
    81 
    82 lemma length_preorder[simp]: "length (preorder t) = size t"
    83 by (induction t) auto
    84 
    85 lemma length_inorder[simp]: "length (inorder t) = size t"
    86 by (induction t) auto
    87 
    88 lemma preorder_map: "preorder (map_tree f t) = map f (preorder t)"
    89 by (induction t) auto
    90 
    91 lemma inorder_map: "inorder (map_tree f t) = map f (inorder t)"
    92 by (induction t) auto
    93 
    94 
    95 subsection \<open>Binary Search Tree predicate\<close>
    96 
    97 fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where
    98 "bst \<langle>\<rangle> \<longleftrightarrow> True" |
    99 "bst \<langle>l, a, r\<rangle> \<longleftrightarrow> bst l \<and> bst r \<and> (\<forall>x\<in>set_tree l. x < a) \<and> (\<forall>x\<in>set_tree r. a < x)"
   100 
   101 text\<open>In case there are duplicates:\<close>
   102 
   103 fun (in linorder) bst_eq :: "'a tree \<Rightarrow> bool" where
   104 "bst_eq \<langle>\<rangle> \<longleftrightarrow> True" |
   105 "bst_eq \<langle>l,a,r\<rangle> \<longleftrightarrow>
   106  bst_eq l \<and> bst_eq r \<and> (\<forall>x\<in>set_tree l. x \<le> a) \<and> (\<forall>x\<in>set_tree r. a \<le> x)"
   107 
   108 lemma (in linorder) bst_eq_if_bst: "bst t \<Longrightarrow> bst_eq t"
   109 by (induction t) (auto)
   110 
   111 lemma (in linorder) bst_eq_imp_sorted: "bst_eq t \<Longrightarrow> sorted (inorder t)"
   112 apply (induction t)
   113  apply(simp)
   114 by (fastforce simp: sorted_append sorted_Cons intro: less_imp_le less_trans)
   115 
   116 lemma (in linorder) distinct_preorder_if_bst: "bst t \<Longrightarrow> distinct (preorder t)"
   117 apply (induction t)
   118  apply simp
   119 apply(fastforce elim: order.asym)
   120 done
   121 
   122 lemma (in linorder) distinct_inorder_if_bst: "bst t \<Longrightarrow> distinct (inorder t)"
   123 apply (induction t)
   124  apply simp
   125 apply(fastforce elim: order.asym)
   126 done
   127 
   128 
   129 subsection "Function @{text mirror}"
   130 
   131 fun mirror :: "'a tree \<Rightarrow> 'a tree" where
   132 "mirror \<langle>\<rangle> = Leaf" |
   133 "mirror \<langle>l,x,r\<rangle> = \<langle>mirror r, x, mirror l\<rangle>"
   134 
   135 lemma mirror_Leaf[simp]: "mirror t = \<langle>\<rangle> \<longleftrightarrow> t = \<langle>\<rangle>"
   136 by (induction t) simp_all
   137 
   138 lemma size_mirror[simp]: "size(mirror t) = size t"
   139 by (induction t) simp_all
   140 
   141 lemma size1_mirror[simp]: "size1(mirror t) = size1 t"
   142 by (simp add: size1_def)
   143 
   144 lemma depth_mirror[simp]: "depth(mirror t) = depth t"
   145 by (induction t) simp_all
   146 
   147 lemma inorder_mirror: "inorder(mirror t) = rev(inorder t)"
   148 by (induction t) simp_all
   149 
   150 lemma map_mirror: "map_tree f (mirror t) = mirror (map_tree f t)"
   151 by (induction t) simp_all
   152 
   153 lemma mirror_mirror[simp]: "mirror(mirror t) = t"
   154 by (induction t) simp_all
   155 
   156 
   157 subsection "Deletion of the rightmost entry"
   158 
   159 fun del_rightmost :: "'a tree \<Rightarrow> 'a tree * 'a" where
   160 "del_rightmost \<langle>l, a, \<langle>\<rangle>\<rangle> = (l,a)" |
   161 "del_rightmost \<langle>l, a, r\<rangle> = (let (r',x) = del_rightmost r in (\<langle>l, a, r'\<rangle>, x))"
   162 
   163 lemma del_rightmost_set_tree_if_bst:
   164   "\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> Leaf \<rbrakk>
   165   \<Longrightarrow> x \<in> set_tree t \<and> set_tree t' = set_tree t - {x}"
   166 apply(induction t arbitrary: t' rule: del_rightmost.induct)
   167   apply (fastforce simp: ball_Un split: prod.splits)+
   168 done
   169 
   170 lemma del_rightmost_set_tree:
   171   "\<lbrakk> del_rightmost t = (t',x);  t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> set_tree t = insert x (set_tree t')"
   172 apply(induction t arbitrary: t' rule: del_rightmost.induct)
   173 by (auto split: prod.splits) auto
   174 
   175 lemma del_rightmost_bst:
   176   "\<lbrakk> del_rightmost t = (t',x);  bst t;  t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> bst t'"
   177 proof(induction t arbitrary: t' rule: del_rightmost.induct)
   178   case (2 l a rl b rr)
   179   let ?r = "Node rl b rr"
   180   from "2.prems"(1) obtain r' where 1: "del_rightmost ?r = (r',x)" and [simp]: "t' = Node l a r'"
   181     by(simp split: prod.splits)
   182   from "2.prems"(2) 1 del_rightmost_set_tree[OF 1] show ?case by(auto)(simp add: "2.IH")
   183 qed auto
   184 
   185 
   186 lemma del_rightmost_greater: "\<lbrakk> del_rightmost t = (t',x);  bst t;  t \<noteq> \<langle>\<rangle> \<rbrakk>
   187   \<Longrightarrow> \<forall>a\<in>set_tree t'. a < x"
   188 proof(induction t arbitrary: t' rule: del_rightmost.induct)
   189   case (2 l a rl b rr)
   190   from "2.prems"(1) obtain r'
   191   where dm: "del_rightmost (Node rl b rr) = (r',x)" and [simp]: "t' = Node l a r'"
   192     by(simp split: prod.splits)
   193   show ?case using "2.prems"(2) "2.IH"[OF dm] del_rightmost_set_tree_if_bst[OF dm]
   194     by (fastforce simp add: ball_Un)
   195 qed simp_all
   196 
   197 lemma del_rightmost_Max:
   198   "\<lbrakk> del_rightmost t = (t',x);  bst t;  t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> x = Max(set_tree t)"
   199 by (metis Max_insert2 del_rightmost_greater del_rightmost_set_tree finite_set_tree less_le_not_le)
   200 
   201 end