src/HOL/Library/Tree.thy
author nipkow
Tue, 13 Sep 2016 11:31:30 +0200
changeset 63861 90360390a916
parent 63829 6a05c8cbf7de
child 64414 f8be2208e99c
permissions -rw-r--r--
reorganization, more funs and lemmas
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 *)
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
     2
(* Todo:
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
     3
 (min_)height of balanced trees via floorlog
63770
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
     4
 minimal path_len of balanced trees
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
     5
*)
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     6
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59928
diff changeset
     7
section \<open>Binary Tree\<close>
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     8
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     9
theory Tree
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    10
imports Main
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    11
begin
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    12
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    13
datatype 'a tree =
62160
ff20b44b2fc8 tuned layout
nipkow
parents: 61585
diff changeset
    14
  is_Leaf: Leaf ("\<langle>\<rangle>") |
ff20b44b2fc8 tuned layout
nipkow
parents: 61585
diff changeset
    15
  Node (left: "'a tree") (val: 'a) (right: "'a tree") ("(1\<langle>_,/ _,/ _\<rangle>)")
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    16
  where
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    17
    "left Leaf = Leaf"
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    18
  | "right Leaf = Leaf"
57569
e20a999f7161 register tree with datatype_compat ot support QuickCheck
hoelzl
parents: 57530
diff changeset
    19
datatype_compat tree
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    20
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59928
diff changeset
    21
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
    22
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    23
definition size1 :: "'a tree \<Rightarrow> nat" where
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    24
"size1 t = size t + 1"
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    25
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    26
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    27
"subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    28
"subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    29
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    30
fun mirror :: "'a tree \<Rightarrow> 'a tree" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    31
"mirror \<langle>\<rangle> = Leaf" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    32
"mirror \<langle>l,x,r\<rangle> = \<langle>mirror r, x, mirror l\<rangle>"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    33
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    34
class height = fixes height :: "'a \<Rightarrow> nat"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    35
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    36
instantiation tree :: (type)height
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    37
begin
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    38
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    39
fun height_tree :: "'a tree => nat" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    40
"height Leaf = 0" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    41
"height (Node t1 a t2) = max (height t1) (height t2) + 1"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    42
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    43
instance ..
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    44
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    45
end
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    46
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    47
fun min_height :: "'a tree \<Rightarrow> nat" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    48
"min_height Leaf = 0" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    49
"min_height (Node l _ r) = min (min_height l) (min_height r) + 1"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    50
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    51
fun complete :: "'a tree \<Rightarrow> bool" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    52
"complete Leaf = True" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    53
"complete (Node l x r) = (complete l \<and> complete r \<and> height l = height r)"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    54
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    55
definition balanced :: "'a tree \<Rightarrow> bool" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    56
"balanced t = (height t - min_height t \<le> 1)"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    57
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    58
text \<open>Weight balanced:\<close>
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    59
fun wbalanced :: "'a tree \<Rightarrow> bool" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    60
"wbalanced Leaf = True" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    61
"wbalanced (Node l x r) = (abs(int(size l) - int(size r)) \<le> 1 \<and> wbalanced l \<and> wbalanced r)"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    62
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    63
text \<open>Internal path length:\<close>
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    64
fun path_len :: "'a tree \<Rightarrow> nat" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    65
"path_len Leaf = 0 " |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    66
"path_len (Node l _ r) = path_len l + size l + path_len r + size r"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    67
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    68
fun preorder :: "'a tree \<Rightarrow> 'a list" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    69
"preorder \<langle>\<rangle> = []" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    70
"preorder \<langle>l, x, r\<rangle> = x # preorder l @ preorder r"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    71
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    72
fun inorder :: "'a tree \<Rightarrow> 'a list" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    73
"inorder \<langle>\<rangle> = []" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    74
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    75
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    76
text\<open>A linear version avoiding append:\<close>
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    77
fun inorder2 :: "'a tree \<Rightarrow> 'a list \<Rightarrow> 'a list" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    78
"inorder2 \<langle>\<rangle> xs = xs" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    79
"inorder2 \<langle>l, x, r\<rangle> xs = inorder2 l (x # inorder2 r xs)"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    80
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    81
text\<open>Binary Search Tree:\<close>
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    82
fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    83
"bst \<langle>\<rangle> \<longleftrightarrow> True" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    84
"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)"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    85
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    86
text\<open>Binary Search Tree with duplicates:\<close>
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    87
fun (in linorder) bst_eq :: "'a tree \<Rightarrow> bool" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    88
"bst_eq \<langle>\<rangle> \<longleftrightarrow> True" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    89
"bst_eq \<langle>l,a,r\<rangle> \<longleftrightarrow>
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    90
 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)"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    91
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    92
fun (in linorder) heap :: "'a tree \<Rightarrow> bool" where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    93
"heap Leaf = True" |
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    94
"heap (Node l m r) =
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    95
  (heap l \<and> heap r \<and> (\<forall>x \<in> set_tree l \<union> set_tree r. m \<le> x))"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    96
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    97
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    98
subsection \<open>@{const size}\<close>
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
    99
58438
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
   100
lemma size1_simps[simp]:
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
   101
  "size1 \<langle>\<rangle> = 1"
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
   102
  "size1 \<langle>l, x, r\<rangle> = size1 l + size1 r"
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
   103
by (simp_all add: size1_def)
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
   104
62650
7e6bb43e7217 added tree lemmas
nipkow
parents: 62202
diff changeset
   105
lemma size1_ge0[simp]: "0 < size1 t"
7e6bb43e7217 added tree lemmas
nipkow
parents: 62202
diff changeset
   106
by (simp add: size1_def)
7e6bb43e7217 added tree lemmas
nipkow
parents: 62202
diff changeset
   107
60507
nipkow
parents: 60506
diff changeset
   108
lemma size_0_iff_Leaf: "size t = 0 \<longleftrightarrow> t = Leaf"
60505
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   109
by(cases t) auto
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   110
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   111
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
   112
by (cases t) auto
57530
439f881c8744 added lemma
nipkow
parents: 57450
diff changeset
   113
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   114
lemma finite_set_tree[simp]: "finite(set_tree t)"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   115
by(induction t) auto
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   116
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   117
lemma size_map_tree[simp]: "size (map_tree f t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   118
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   119
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   120
lemma size1_map_tree[simp]: "size1 (map_tree f t) = size1 t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   121
by (simp add: size1_def)
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   122
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   123
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   124
subsection \<open>@{const subtrees}\<close>
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
   125
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   126
lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. \<langle>l, a, r\<rangle> \<in> subtrees t"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   127
by (induction t)(auto)
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   128
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   129
lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   130
by (induction t) auto
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   131
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   132
lemma in_set_tree_if: "\<langle>l, a, r\<rangle> \<in> subtrees t \<Longrightarrow> a \<in> set_tree t"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   133
by (metis Node_notin_subtrees_if)
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
   134
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   135
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   136
subsection \<open>@{const height} and @{const min_height}\<close>
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
   137
63665
15f48ce7ec23 added lemma
nipkow
parents: 63598
diff changeset
   138
lemma height_0_iff_Leaf: "height t = 0 \<longleftrightarrow> t = Leaf"
15f48ce7ec23 added lemma
nipkow
parents: 63598
diff changeset
   139
by(cases t) auto
15f48ce7ec23 added lemma
nipkow
parents: 63598
diff changeset
   140
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
   141
lemma height_map_tree[simp]: "height (map_tree f t) = height t"
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   142
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   143
62202
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   144
lemma size1_height: "size t + 1 \<le> 2 ^ height (t::'a tree)"
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   145
proof(induction t)
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   146
  case (Node l a r)
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   147
  show ?case
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   148
  proof (cases "height l \<le> height r")
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   149
    case True
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   150
    have "size(Node l a r) + 1 = (size l + 1) + (size r + 1)" by simp
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   151
    also have "size l + 1 \<le> 2 ^ height l" by(rule Node.IH(1))
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   152
    also have "size r + 1 \<le> 2 ^ height r" by(rule Node.IH(2))
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   153
    also have "(2::nat) ^ height l \<le> 2 ^ height r" using True by simp
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   154
    finally show ?thesis using True by (auto simp: max_def mult_2)
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   155
  next
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   156
    case False
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   157
    have "size(Node l a r) + 1 = (size l + 1) + (size r + 1)" by simp
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   158
    also have "size l + 1 \<le> 2 ^ height l" by(rule Node.IH(1))
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   159
    also have "size r + 1 \<le> 2 ^ height r" by(rule Node.IH(2))
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   160
    also have "(2::nat) ^ height r \<le> 2 ^ height l" using False by simp
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   161
    finally show ?thesis using False by (auto simp: max_def mult_2)
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   162
  qed
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   163
qed simp
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
   164
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   165
corollary size_height: "size t \<le> 2 ^ height (t::'a tree) - 1"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   166
using size1_height[of t] by(arith)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   167
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   168
lemma height_subtrees: "s \<in> subtrees t \<Longrightarrow> height s \<le> height t"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   169
by (induction t) auto
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   170
63598
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   171
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   172
lemma min_hight_le_height: "min_height t \<le> height t"
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   173
by(induction t) auto
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   174
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   175
lemma min_height_map_tree[simp]: "min_height (map_tree f t) = min_height t"
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   176
by (induction t) auto
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   177
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   178
lemma min_height_le_size1: "2 ^ min_height t \<le> size t + 1"
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   179
proof(induction t)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   180
  case (Node l a r)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   181
  have "(2::nat) ^ min_height (Node l a r) \<le> 2 ^ min_height l + 2 ^ min_height r"
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   182
    by (simp add: min_def)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   183
  also have "\<dots> \<le> size(Node l a r) + 1" using Node.IH by simp
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   184
  finally show ?case .
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   185
qed simp
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   186
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   187
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   188
subsection \<open>@{const complete}\<close>
63036
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   189
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   190
lemma complete_iff_height: "complete t \<longleftrightarrow> (min_height t = height t)"
63598
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   191
apply(induction t)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   192
 apply simp
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   193
apply (simp add: min_def max_def)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   194
by (metis le_antisym le_trans min_hight_le_height)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   195
63770
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   196
lemma size1_if_complete: "complete t \<Longrightarrow> size1 t = 2 ^ height t"
63036
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   197
by (induction t) auto
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   198
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   199
lemma size_if_complete: "complete t \<Longrightarrow> size t = 2 ^ height t - 1"
63770
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   200
using size1_if_complete[simplified size1_def] by fastforce
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   201
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   202
lemma complete_if_size: "size t = 2 ^ height t - 1 \<Longrightarrow> complete t"
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   203
proof (induct "height t" arbitrary: t)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   204
  case 0 thus ?case by (simp add: size_0_iff_Leaf)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   205
next
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   206
  case (Suc h)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   207
  hence "t \<noteq> Leaf" by auto
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   208
  then obtain l a r where [simp]: "t = Node l a r"
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   209
    by (auto simp: neq_Leaf_iff)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   210
  have 1: "height l \<le> h" and 2: "height r \<le> h" using Suc(2) by(auto)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   211
  have 3: "~ height l < h"
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   212
  proof
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   213
    assume 0: "height l < h"
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   214
    have "size t = size l + (size r + 1)" by simp
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   215
    also note size_height[of l]
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   216
    also note size1_height[of r]
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   217
    also have "(2::nat) ^ height l - 1 < 2 ^ h - 1"
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   218
        using 0 by (simp add: diff_less_mono)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   219
    also have "(2::nat) ^ height r \<le> 2 ^ h" using 2 by simp
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   220
    also have "(2::nat) ^ h - 1 + 2 ^ h = 2 ^ (Suc h) - 1" by (simp)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   221
    also have "\<dots> = size t" using Suc(2,3) by simp
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   222
    finally show False by (simp add: diff_le_mono)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   223
  qed
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   224
  have 4: "~ height r < h"
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   225
  proof
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   226
    assume 0: "height r < h"
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   227
    have "size t = (size l + 1) + size r" by simp
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   228
    also note size_height[of r]
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   229
    also note size1_height[of l]
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   230
    also have "(2::nat) ^ height r - 1 < 2 ^ h - 1"
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   231
        using 0 by (simp add: diff_less_mono)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   232
    also have "(2::nat) ^ height l \<le> 2 ^ h" using 1 by simp
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   233
    also have "(2::nat) ^ h + (2 ^ h - 1) = 2 ^ (Suc h) - 1" by (simp)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   234
    also have "\<dots> = size t" using Suc(2,3) by simp
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   235
    finally show False by (simp add: diff_le_mono)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   236
  qed
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   237
  from 1 2 3 4 have *: "height l = h" "height r = h" by linarith+
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   238
  hence "size l = 2 ^ height l - 1" "size r = 2 ^ height r - 1"
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   239
    using Suc(3) size_height[of l] size_height[of r] by (auto)
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   240
  with * Suc(1) show ?case by simp
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   241
qed
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   242
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   243
lemma complete_iff_size: "complete t \<longleftrightarrow> size t = 2 ^ height t - 1"
a67397b13eb5 added lemmas
nipkow
parents: 63765
diff changeset
   244
using complete_if_size size_if_complete by blast
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   245
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   246
text\<open>A better lower bound for incomplete trees:\<close>
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   247
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   248
lemma min_height_le_size_if_incomplete:
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   249
  "\<not> complete t \<Longrightarrow> 2 ^ min_height t \<le> size t"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   250
proof(induction t)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   251
  case Leaf thus ?case by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   252
next
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   253
  case (Node l a r)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   254
  show ?case (is "?l \<le> ?r")
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   255
  proof (cases "complete l")
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   256
    case l: True thus ?thesis
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   257
    proof (cases "complete r")
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   258
      case r: True
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   259
      have "height l \<noteq> height r" using Node.prems l r by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   260
      hence "?l < 2 ^ min_height l + 2 ^ min_height r"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   261
        using l r by (simp add: min_def complete_iff_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   262
      also have "\<dots> = (size l + 1) + (size r + 1)"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   263
        using l r size_if_complete[where ?'a = 'a]
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   264
        by (simp add: complete_iff_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   265
      also have "\<dots> \<le> ?r + 1" by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   266
      finally show ?thesis by arith
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   267
    next
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   268
      case r: False
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   269
      have "?l \<le> 2 ^ min_height l + 2 ^ min_height r" by (simp add: min_def)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   270
      also have "\<dots> \<le> size l + 1 + size r"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   271
        using Node.IH(2)[OF r] l size_if_complete[where ?'a = 'a]
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   272
        by (simp add: complete_iff_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   273
      also have "\<dots> = ?r" by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   274
      finally show ?thesis .
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   275
    qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   276
  next
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   277
    case l: False thus ?thesis
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   278
    proof (cases "complete r")
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   279
      case r: True
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   280
      have "?l \<le> 2 ^ min_height l + 2 ^ min_height r" by (simp add: min_def)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   281
      also have "\<dots> \<le> size l + (size r + 1)"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   282
        using Node.IH(1)[OF l] r size_if_complete[where ?'a = 'a]
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   283
        by (simp add: complete_iff_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   284
      also have "\<dots> = ?r" by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   285
      finally show ?thesis .
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   286
    next
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   287
      case r: False
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   288
      have "?l \<le> 2 ^ min_height l + 2 ^ min_height r"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   289
        by (simp add: min_def)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   290
      also have "\<dots> \<le> size l + size r"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   291
        using Node.IH(1)[OF l] Node.IH(2)[OF r] by (simp)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   292
      also have "\<dots> \<le> ?r" by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   293
      finally show ?thesis .
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   294
    qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   295
  qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   296
qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   297
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   298
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   299
subsection \<open>@{const balanced}\<close>
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   300
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   301
lemma balanced_subtreeL: "balanced (Node l x r) \<Longrightarrow> balanced l"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   302
by(simp add: balanced_def)
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   303
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   304
lemma balanced_subtreeR: "balanced (Node l x r) \<Longrightarrow> balanced r"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   305
by(simp add: balanced_def)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   306
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   307
lemma balanced_subtrees: "\<lbrakk> balanced t; s \<in> subtrees t \<rbrakk> \<Longrightarrow> balanced s"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   308
using [[simp_depth_limit=1]]
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   309
by(induction t arbitrary: s)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   310
  (auto simp add: balanced_subtreeL balanced_subtreeR)
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   311
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   312
text\<open>Balanced trees have optimal height:\<close>
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   313
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   314
lemma balanced_optimal:
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   315
fixes t :: "'a tree" and t' :: "'b tree"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   316
assumes "balanced t" "size t \<le> size t'" shows "height t \<le> height t'"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   317
proof (cases "complete t")
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   318
  case True
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   319
  have "(2::nat) ^ height t - 1 \<le> 2 ^ height t' - 1"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   320
  proof -
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   321
    have "(2::nat) ^ height t - 1 = size t"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   322
      using True by (simp add: complete_iff_height size_if_complete)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   323
    also note assms(2)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   324
    also have "size t' \<le> 2 ^ height t' - 1" by (rule size_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   325
    finally show ?thesis .
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   326
  qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   327
  thus ?thesis by (simp add: le_diff_iff)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   328
next
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   329
  case False
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   330
  have "(2::nat) ^ min_height t < 2 ^ height t'"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   331
  proof -
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   332
    have "(2::nat) ^ min_height t \<le> size t"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   333
      by(rule min_height_le_size_if_incomplete[OF False])
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   334
    also note assms(2)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   335
    also have "size t' \<le> 2 ^ height t' - 1"  by(rule size_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   336
    finally show ?thesis
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   337
      using power_eq_0_iff[of "2::nat" "height t'"] by linarith
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   338
  qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   339
  hence *: "min_height t < height t'" by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   340
  have "min_height t + 1 = height t"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   341
    using min_hight_le_height[of t] assms(1) False
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63770
diff changeset
   342
    by (simp add: complete_iff_height balanced_def)
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   343
  with * show ?thesis by arith
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   344
qed
63036
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   345
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   346
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   347
subsection \<open>@{const wbalanced}\<close>
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   348
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   349
lemma wbalanced_subtrees: "\<lbrakk> wbalanced t; s \<in> subtrees t \<rbrakk> \<Longrightarrow> wbalanced s"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   350
using [[simp_depth_limit=1]] by(induction t arbitrary: s) auto
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   351
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   352
(* show wbalanced \<Longrightarrow> balanced and use that in Balanced.thy *)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   353
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   354
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   355
subsection \<open>@{const path_len}\<close>
63413
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   356
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   357
text \<open>The internal path length of a tree:\<close>
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   358
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   359
lemma path_len_if_bal: "complete t
63413
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   360
  \<Longrightarrow> path_len t = (let n = height t in 2 + n*2^n - 2^(n+1))"
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   361
proof(induction t)
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   362
  case (Node l x r)
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   363
  have *: "2^(n+1) \<le> 2 + n*2^n" for n :: nat
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   364
    by(induction n) auto
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   365
  have **: "(0::nat) < 2^n" for n :: nat by simp
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   366
  let ?h = "height r"
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   367
  show ?case using Node *[of ?h] **[of ?h] by (simp add: size_if_complete Let_def)
63413
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   368
qed simp
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   369
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   370
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   371
subsection "List of entries"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   372
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
   373
lemma set_inorder[simp]: "set (inorder t) = set_tree t"
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   374
by (induction t) auto
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   375
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   376
lemma set_preorder[simp]: "set (preorder t) = set_tree t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   377
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   378
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   379
lemma length_preorder[simp]: "length (preorder t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   380
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   381
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   382
lemma length_inorder[simp]: "length (inorder t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   383
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   384
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   385
lemma preorder_map: "preorder (map_tree f t) = map f (preorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   386
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   387
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   388
lemma inorder_map: "inorder (map_tree f t) = map f (inorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   389
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   390
63765
e60020520b15 added inorder2
nipkow
parents: 63755
diff changeset
   391
lemma inorder2_inorder: "inorder2 t xs = inorder t @ xs"
e60020520b15 added inorder2
nipkow
parents: 63755
diff changeset
   392
by (induction t arbitrary: xs) auto
e60020520b15 added inorder2
nipkow
parents: 63755
diff changeset
   393
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   394
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   395
subsection \<open>Binary Search Tree\<close>
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   396
59928
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   397
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
   398
by (induction t) (auto)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   399
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   400
lemma (in linorder) bst_eq_imp_sorted: "bst_eq t \<Longrightarrow> sorted (inorder t)"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   401
apply (induction t)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   402
 apply(simp)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   403
by (fastforce simp: sorted_append sorted_Cons intro: less_imp_le less_trans)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   404
59928
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   405
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
   406
apply (induction t)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   407
 apply simp
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   408
apply(fastforce elim: order.asym)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   409
done
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   410
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   411
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
   412
apply (induction t)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   413
 apply simp
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   414
apply(fastforce elim: order.asym)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   415
done
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   416
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   417
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   418
subsection \<open>@{const heap}\<close>
60505
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   419
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   420
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63829
diff changeset
   421
subsection \<open>@{const mirror}\<close>
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   422
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   423
lemma mirror_Leaf[simp]: "mirror t = \<langle>\<rangle> \<longleftrightarrow> t = \<langle>\<rangle>"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   424
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   425
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   426
lemma size_mirror[simp]: "size(mirror t) = size t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   427
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   428
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   429
lemma size1_mirror[simp]: "size1(mirror t) = size1 t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   430
by (simp add: size1_def)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   431
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
   432
lemma height_mirror[simp]: "height(mirror t) = height t"
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   433
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   434
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   435
lemma inorder_mirror: "inorder(mirror t) = rev(inorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   436
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   437
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   438
lemma map_mirror: "map_tree f (mirror t) = mirror (map_tree f t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   439
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   440
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   441
lemma mirror_mirror[simp]: "mirror(mirror t) = t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   442
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   443
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   444
end