src/HOL/Library/Tree.thy
author nipkow
Mon, 06 Apr 2015 15:23:50 +0200
changeset 59928 b9b7f913a19a
parent 59776 f54af3307334
child 60500 903bb1495239
child 60505 9e6584184315
permissions -rw-r--r--
new theory Library/Tree_Multiset.thy
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
58881
b9556a055632 modernized header;
wenzelm
parents: 58467
diff changeset
     3
section {* Binary Tree *}
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
58438
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    17
text{* Can be seen as counting the number of leaves rather than nodes: *}
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
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    27
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
    28
by (cases t) auto
57530
439f881c8744 added lemma
nipkow
parents: 57450
diff changeset
    29
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    30
lemma finite_set_tree[simp]: "finite(set_tree t)"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    31
by(induction t) auto
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    32
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    33
lemma size_map_tree[simp]: "size (map_tree f t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    34
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    35
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    36
lemma size1_map_tree[simp]: "size1 (map_tree f t) = size1 t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    37
by (simp add: size1_def)
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    38
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    39
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    40
subsection "The depth"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    41
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    42
fun depth :: "'a tree => nat" where
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    43
"depth Leaf = 0" |
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    44
"depth (Node t1 a t2) = Suc (max (depth t1) (depth t2))"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    45
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    46
lemma depth_map_tree[simp]: "depth (map_tree f t) = depth t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    47
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    48
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    49
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    50
subsection "The set of subtrees"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    51
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    52
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    53
  "subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    54
  "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
    55
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    56
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
    57
by (induction t)(auto)
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    58
57450
2baecef3207f Library/Tree: bst is preferred to be a function
hoelzl
parents: 57449
diff changeset
    59
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
    60
by (induction t) auto
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    61
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    62
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
    63
by (metis Node_notin_subtrees_if)
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    64
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    65
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    66
subsection "List of entries"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    67
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    68
fun preorder :: "'a tree \<Rightarrow> 'a list" where
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    69
"preorder \<langle>\<rangle> = []" |
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    70
"preorder \<langle>l, x, r\<rangle> = x # preorder l @ preorder r"
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    71
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    72
fun inorder :: "'a tree \<Rightarrow> 'a list" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    73
"inorder \<langle>\<rangle> = []" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    74
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r"
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    75
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    76
lemma set_inorder[simp]: "set (inorder t) = set_tree t"
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    77
by (induction t) auto
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    78
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    79
lemma set_preorder[simp]: "set (preorder t) = set_tree t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    80
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    81
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    82
lemma length_preorder[simp]: "length (preorder t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    83
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    84
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    85
lemma length_inorder[simp]: "length (inorder t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    86
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    87
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    88
lemma preorder_map: "preorder (map_tree f t) = map f (preorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    89
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    90
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    91
lemma inorder_map: "inorder (map_tree f t) = map f (inorder 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
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    94
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    95
subsection {* Binary Search Tree predicate *}
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    96
57450
2baecef3207f Library/Tree: bst is preferred to be a function
hoelzl
parents: 57449
diff changeset
    97
fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    98
"bst \<langle>\<rangle> \<longleftrightarrow> True" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    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)"
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   100
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   101
text{* In case there are duplicates: *}
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   102
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   103
fun (in linorder) bst_eq :: "'a tree \<Rightarrow> bool" where
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   104
"bst_eq \<langle>\<rangle> \<longleftrightarrow> True" |
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   105
"bst_eq \<langle>l,a,r\<rangle> \<longleftrightarrow>
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   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)"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   107
59928
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   108
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
   109
by (induction t) (auto)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   110
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   111
lemma (in linorder) bst_eq_imp_sorted: "bst_eq t \<Longrightarrow> sorted (inorder t)"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   112
apply (induction t)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   113
 apply(simp)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   114
by (fastforce simp: sorted_append sorted_Cons intro: less_imp_le less_trans)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   115
59928
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   116
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
   117
apply (induction t)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   118
 apply simp
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   119
apply(fastforce elim: order.asym)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   120
done
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   121
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   122
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
   123
apply (induction t)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   124
 apply simp
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   125
apply(fastforce elim: order.asym)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   126
done
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   127
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   128
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   129
subsection "Function @{text mirror}"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   130
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   131
fun mirror :: "'a tree \<Rightarrow> 'a tree" where
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   132
"mirror \<langle>\<rangle> = Leaf" |
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   133
"mirror \<langle>l,x,r\<rangle> = \<langle>mirror r, x, mirror l\<rangle>"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   134
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   135
lemma mirror_Leaf[simp]: "mirror t = \<langle>\<rangle> \<longleftrightarrow> t = \<langle>\<rangle>"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   136
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   137
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   138
lemma size_mirror[simp]: "size(mirror t) = size t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   139
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   140
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   141
lemma size1_mirror[simp]: "size1(mirror t) = size1 t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   142
by (simp add: size1_def)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   143
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   144
lemma depth_mirror[simp]: "depth(mirror t) = depth t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   145
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   146
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   147
lemma inorder_mirror: "inorder(mirror t) = rev(inorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   148
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   149
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   150
lemma map_mirror: "map_tree f (mirror t) = mirror (map_tree f t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   151
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   152
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   153
lemma mirror_mirror[simp]: "mirror(mirror t) = t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   154
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   155
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   156
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   157
subsection "Deletion of the rightmost entry"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   158
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   159
fun del_rightmost :: "'a tree \<Rightarrow> 'a tree * 'a" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   160
"del_rightmost \<langle>l, a, \<langle>\<rangle>\<rangle> = (l,a)" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   161
"del_rightmost \<langle>l, a, r\<rangle> = (let (r',x) = del_rightmost r in (\<langle>l, a, r'\<rangle>, x))"
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   162
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   163
lemma del_rightmost_set_tree_if_bst:
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   164
  "\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> Leaf \<rbrakk>
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   165
  \<Longrightarrow> x \<in> set_tree t \<and> set_tree t' = set_tree t - {x}"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   166
apply(induction t arbitrary: t' rule: del_rightmost.induct)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   167
  apply (fastforce simp: ball_Un split: prod.splits)+
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   168
done
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   169
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   170
lemma del_rightmost_set_tree:
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   171
  "\<lbrakk> del_rightmost t = (t',x);  t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> set_tree t = insert x (set_tree t')"
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   172
apply(induction t arbitrary: t' rule: del_rightmost.induct)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   173
by (auto split: prod.splits) auto
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   174
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   175
lemma del_rightmost_bst:
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   176
  "\<lbrakk> del_rightmost t = (t',x);  bst t;  t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> bst t'"
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   177
proof(induction t arbitrary: t' rule: del_rightmost.induct)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   178
  case (2 l a rl b rr)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   179
  let ?r = "Node rl b rr"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   180
  from "2.prems"(1) obtain r' where 1: "del_rightmost ?r = (r',x)" and [simp]: "t' = Node l a r'"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   181
    by(simp split: prod.splits)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   182
  from "2.prems"(2) 1 del_rightmost_set_tree[OF 1] show ?case by(auto)(simp add: "2.IH")
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   183
qed auto
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   184
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   185
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   186
lemma del_rightmost_greater: "\<lbrakk> del_rightmost t = (t',x);  bst t;  t \<noteq> \<langle>\<rangle> \<rbrakk>
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   187
  \<Longrightarrow> \<forall>a\<in>set_tree t'. a < x"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   188
proof(induction t arbitrary: t' rule: del_rightmost.induct)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   189
  case (2 l a rl b rr)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   190
  from "2.prems"(1) obtain r'
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   191
  where dm: "del_rightmost (Node rl b rr) = (r',x)" and [simp]: "t' = Node l a r'"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   192
    by(simp split: prod.splits)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   193
  show ?case using "2.prems"(2) "2.IH"[OF dm] del_rightmost_set_tree_if_bst[OF dm]
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   194
    by (fastforce simp add: ball_Un)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   195
qed simp_all
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   196
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   197
lemma del_rightmost_Max:
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   198
  "\<lbrakk> del_rightmost t = (t',x);  bst t;  t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> x = Max(set_tree t)"
58467
6a3da58f7233 moved to HOL and generalized
haftmann
parents: 58438
diff changeset
   199
by (metis Max_insert2 del_rightmost_greater del_rightmost_set_tree finite_set_tree less_le_not_le)
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   200
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   201
end