src/HOL/Library/Tree.thy
author nipkow
Thu, 25 Sep 2014 11:38:56 +0200
changeset 58438 566117a31cc0
parent 58424 cbbba613b6ab
child 58467 6a3da58f7233
permissions -rw-r--r--
added function size1
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
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     3
header {* Binary Tree *}
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
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    33
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    34
subsection "The set of subtrees"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    35
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    36
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    37
  "subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    38
  "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
    39
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    40
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
    41
by (induction t)(auto)
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    42
57450
2baecef3207f Library/Tree: bst is preferred to be a function
hoelzl
parents: 57449
diff changeset
    43
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
    44
by (induction t) auto
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    45
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    46
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
    47
by (metis Node_notin_subtrees_if)
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
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 "Inorder list of entries"
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 inorder :: "'a tree \<Rightarrow> 'a list" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    53
"inorder \<langle>\<rangle> = []" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    54
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r"
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    55
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    56
lemma set_inorder[simp]: "set (inorder t) = set_tree t"
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    57
by (induction t) auto
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    58
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    59
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    60
subsection {* Binary Search Tree predicate *}
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    61
57450
2baecef3207f Library/Tree: bst is preferred to be a function
hoelzl
parents: 57449
diff changeset
    62
fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    63
"bst \<langle>\<rangle> \<longleftrightarrow> True" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    64
"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
    65
57450
2baecef3207f Library/Tree: bst is preferred to be a function
hoelzl
parents: 57449
diff changeset
    66
lemma (in linorder) bst_imp_sorted: "bst t \<Longrightarrow> sorted (inorder t)"
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    67
by (induction t) (auto simp: sorted_append sorted_Cons intro: less_imp_le less_trans)
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    68
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    69
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    70
subsection "Deletion of the rightmost entry"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    71
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    72
fun del_rightmost :: "'a tree \<Rightarrow> 'a tree * 'a" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    73
"del_rightmost \<langle>l, a, \<langle>\<rangle>\<rangle> = (l,a)" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    74
"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
    75
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    76
lemma del_rightmost_set_tree_if_bst:
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    77
  "\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> Leaf \<rbrakk>
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    78
  \<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
    79
apply(induction t arbitrary: t' rule: del_rightmost.induct)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    80
  apply (fastforce simp: ball_Un split: prod.splits)+
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    81
done
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    82
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    83
lemma del_rightmost_set_tree:
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    84
  "\<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
    85
apply(induction t arbitrary: t' rule: del_rightmost.induct)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    86
by (auto split: prod.splits) auto
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    87
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    88
lemma del_rightmost_bst:
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    89
  "\<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
    90
proof(induction t arbitrary: t' rule: del_rightmost.induct)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    91
  case (2 l a rl b rr)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    92
  let ?r = "Node rl b rr"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    93
  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
    94
    by(simp split: prod.splits)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    95
  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
    96
qed auto
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    97
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    98
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    99
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
   100
  \<Longrightarrow> \<forall>a\<in>set_tree t'. a < x"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   101
proof(induction t arbitrary: t' rule: del_rightmost.induct)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   102
  case (2 l a rl b rr)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   103
  from "2.prems"(1) obtain r'
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   104
  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
   105
    by(simp split: prod.splits)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   106
  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
   107
    by (fastforce simp add: ball_Un)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   108
qed simp_all
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   109
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   110
(* should be moved but metis not available in much of Main *)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   111
lemma Max_insert1: "\<lbrakk> finite A;  \<forall>a\<in>A. a \<le> x \<rbrakk> \<Longrightarrow> Max(insert x A) = x"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   112
by (metis Max_in Max_insert Max_singleton antisym max_def)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   113
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   114
lemma del_rightmost_Max:
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   115
  "\<lbrakk> del_rightmost t = (t',x);  bst t;  t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> x = Max(set_tree t)"
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   116
by (metis Max_insert1 del_rightmost_greater del_rightmost_set_tree finite_set_tree less_le_not_le)
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   117
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   118
end