src/HOL/Library/Tree.thy
author nipkow
Tue, 28 Jul 2015 13:00:54 +0200
changeset 60808 fd26519b1a6a
parent 60507 16993a3f4967
child 61585 a9599d3d7610
permissions -rw-r--r--
depth -> height; removed del_rightmost (too specifi)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     2
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59928
diff changeset
     3
section \<open>Binary Tree\<close>
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     4
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     5
theory Tree
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     6
imports Main
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     7
begin
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     8
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
     9
datatype 'a tree =
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    10
  Leaf ("\<langle>\<rangle>") |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    11
  Node (left: "'a tree") (val: 'a) (right: "'a tree") ("\<langle>_, _, _\<rangle>")
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    12
  where
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    13
    "left Leaf = Leaf"
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    14
  | "right Leaf = Leaf"
57569
e20a999f7161 register tree with datatype_compat ot support QuickCheck
hoelzl
parents: 57530
diff changeset
    15
datatype_compat tree
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    16
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59928
diff changeset
    17
text\<open>Can be seen as counting the number of leaves rather than nodes:\<close>
58438
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    18
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    19
definition size1 :: "'a tree \<Rightarrow> nat" where
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    20
"size1 t = size t + 1"
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    21
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    22
lemma size1_simps[simp]:
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    23
  "size1 \<langle>\<rangle> = 1"
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    24
  "size1 \<langle>l, x, r\<rangle> = size1 l + size1 r"
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    25
by (simp_all add: size1_def)
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    26
60507
nipkow
parents: 60506
diff changeset
    27
lemma size_0_iff_Leaf: "size t = 0 \<longleftrightarrow> t = Leaf"
60505
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
    28
by(cases t) auto
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
    29
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    30
lemma neq_Leaf_iff: "(t \<noteq> \<langle>\<rangle>) = (\<exists>l a r. t = \<langle>l, a, r\<rangle>)"
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    31
by (cases t) auto
57530
439f881c8744 added lemma
nipkow
parents: 57450
diff changeset
    32
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    33
lemma finite_set_tree[simp]: "finite(set_tree t)"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    34
by(induction t) auto
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    35
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    36
lemma size_map_tree[simp]: "size (map_tree f t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    37
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    38
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    39
lemma size1_map_tree[simp]: "size1 (map_tree f t) = size1 t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    40
by (simp add: size1_def)
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    41
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    42
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    43
subsection "The Height"
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    44
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    45
class height = fixes height :: "'a \<Rightarrow> nat"
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    46
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    47
instantiation tree :: (type)height
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    48
begin
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    49
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    50
fun height_tree :: "'a tree => nat" where
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    51
"height Leaf = 0" |
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    52
"height (Node t1 a t2) = max (height t1) (height t2) + 1"
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    53
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    54
instance ..
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    55
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    56
end
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    57
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    58
lemma height_map_tree[simp]: "height (map_tree f t) = height t"
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    59
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    60
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    61
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    62
subsection "The set of subtrees"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    63
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    64
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    65
"subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    66
"subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)"
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    67
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    68
lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. \<langle>l, a, r\<rangle> \<in> subtrees t"
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    69
by (induction t)(auto)
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    70
57450
2baecef3207f Library/Tree: bst is preferred to be a function
hoelzl
parents: 57449
diff changeset
    71
lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t"
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    72
by (induction t) auto
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    73
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    74
lemma in_set_tree_if: "\<langle>l, a, r\<rangle> \<in> subtrees t \<Longrightarrow> a \<in> set_tree t"
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    75
by (metis Node_notin_subtrees_if)
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    76
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    77
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    78
subsection "List of entries"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    79
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    80
fun preorder :: "'a tree \<Rightarrow> 'a list" where
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    81
"preorder \<langle>\<rangle> = []" |
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    82
"preorder \<langle>l, x, r\<rangle> = x # preorder l @ preorder r"
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    83
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    84
fun inorder :: "'a tree \<Rightarrow> 'a list" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    85
"inorder \<langle>\<rangle> = []" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    86
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r"
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    87
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    88
lemma set_inorder[simp]: "set (inorder t) = set_tree t"
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    89
by (induction t) auto
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    90
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    91
lemma set_preorder[simp]: "set (preorder t) = set_tree t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    92
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    93
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    94
lemma length_preorder[simp]: "length (preorder t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    95
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    96
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    97
lemma length_inorder[simp]: "length (inorder t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    98
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    99
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   100
lemma preorder_map: "preorder (map_tree f t) = map f (preorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   101
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   102
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   103
lemma inorder_map: "inorder (map_tree f t) = map f (inorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   104
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   105
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   106
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59928
diff changeset
   107
subsection \<open>Binary Search Tree predicate\<close>
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   108
57450
2baecef3207f Library/Tree: bst is preferred to be a function
hoelzl
parents: 57449
diff changeset
   109
fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   110
"bst \<langle>\<rangle> \<longleftrightarrow> True" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   111
"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)"
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   112
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59928
diff changeset
   113
text\<open>In case there are duplicates:\<close>
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   114
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   115
fun (in linorder) bst_eq :: "'a tree \<Rightarrow> bool" where
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   116
"bst_eq \<langle>\<rangle> \<longleftrightarrow> True" |
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   117
"bst_eq \<langle>l,a,r\<rangle> \<longleftrightarrow>
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   118
 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)"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   119
59928
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   120
lemma (in linorder) bst_eq_if_bst: "bst t \<Longrightarrow> bst_eq t"
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   121
by (induction t) (auto)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   122
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   123
lemma (in linorder) bst_eq_imp_sorted: "bst_eq t \<Longrightarrow> sorted (inorder t)"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   124
apply (induction t)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   125
 apply(simp)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   126
by (fastforce simp: sorted_append sorted_Cons intro: less_imp_le less_trans)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   127
59928
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   128
lemma (in linorder) distinct_preorder_if_bst: "bst t \<Longrightarrow> distinct (preorder t)"
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   129
apply (induction t)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   130
 apply simp
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   131
apply(fastforce elim: order.asym)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   132
done
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   133
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   134
lemma (in linorder) distinct_inorder_if_bst: "bst t \<Longrightarrow> distinct (inorder t)"
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   135
apply (induction t)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   136
 apply simp
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   137
apply(fastforce elim: order.asym)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   138
done
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   139
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   140
60505
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   141
subsection "The heap predicate"
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   142
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   143
fun heap :: "'a::linorder tree \<Rightarrow> bool" where
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   144
"heap Leaf = True" |
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   145
"heap (Node l m r) =
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   146
  (heap l \<and> heap r \<and> (\<forall>x \<in> set_tree l \<union> set_tree r. m \<le> x))"
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   147
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   148
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   149
subsection "Function @{text mirror}"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   150
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   151
fun mirror :: "'a tree \<Rightarrow> 'a tree" where
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   152
"mirror \<langle>\<rangle> = Leaf" |
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   153
"mirror \<langle>l,x,r\<rangle> = \<langle>mirror r, x, mirror l\<rangle>"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   154
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   155
lemma mirror_Leaf[simp]: "mirror t = \<langle>\<rangle> \<longleftrightarrow> t = \<langle>\<rangle>"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   156
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   157
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   158
lemma size_mirror[simp]: "size(mirror t) = size t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   159
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   160
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   161
lemma size1_mirror[simp]: "size1(mirror t) = size1 t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   162
by (simp add: size1_def)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   163
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
   164
lemma height_mirror[simp]: "height(mirror t) = height t"
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   165
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   166
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   167
lemma inorder_mirror: "inorder(mirror t) = rev(inorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   168
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   169
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   170
lemma map_mirror: "map_tree f (mirror t) = mirror (map_tree f t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   171
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   172
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   173
lemma mirror_mirror[simp]: "mirror(mirror t) = t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   174
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   175
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   176
end