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