src/HOL/Data_Structures/Leftist_Heap.thy
author nipkow
Thu, 24 Mar 2016 15:56:47 +0100
changeset 62706 49c6a54ceab6
child 64969 a7ea55c1be52
permissions -rw-r--r--
added Leftist_Heap
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     2
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     3
section \<open>Leftist Heap\<close>
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     4
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     5
theory Leftist_Heap
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     6
imports Tree2 "~~/src/HOL/Library/Multiset" Complex_Main
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     7
begin
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     8
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     9
type_synonym 'a lheap = "('a,nat)tree"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    10
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    11
fun rank :: "'a lheap \<Rightarrow> nat" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    12
"rank Leaf = 0" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    13
"rank (Node _ _ _ r) = rank r + 1"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    14
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    15
fun rk :: "'a lheap \<Rightarrow> nat" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    16
"rk Leaf = 0" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    17
"rk (Node n _ _ _) = n"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    18
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    19
text{* The invariant: *}
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    20
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    21
fun lheap :: "'a lheap \<Rightarrow> bool" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    22
"lheap Leaf = True" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    23
"lheap (Node n l a r) =
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    24
 (n = rank r + 1 \<and> rank l \<ge> rank r \<and> lheap l & lheap r)"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    25
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    26
definition node :: "'a lheap \<Rightarrow> 'a \<Rightarrow> 'a lheap \<Rightarrow> 'a lheap" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    27
"node l a r =
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    28
 (let rl = rk l; rr = rk r
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    29
  in if rl \<ge> rr then Node (rr+1) l a r else Node (rl+1) r a l)"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    30
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    31
fun get_min :: "'a lheap \<Rightarrow> 'a" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    32
"get_min(Node n l a r) = a"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    33
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    34
function meld :: "'a::ord lheap \<Rightarrow> 'a lheap \<Rightarrow> 'a lheap" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    35
"meld Leaf t2 = t2" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    36
"meld t1 Leaf = t1" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    37
"meld (Node n1 l1 a1 r1) (Node n2 l2 a2 r2) =
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    38
   (if a1 \<le> a2 then node l1 a1 (meld r1 (Node n2 l2 a2 r2))
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    39
    else node l2 a2 (meld r2 (Node n1 l1 a1 r1)))"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    40
by pat_completeness auto
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    41
termination by (relation "measure (%(t1,t2). rank t1 + rank t2)") auto
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    42
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    43
lemma meld_code: "meld t1 t2 = (case (t1,t2) of
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    44
  (Leaf, _) \<Rightarrow> t2 |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    45
  (_, Leaf) \<Rightarrow> t1 |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    46
  (Node n1 l1 a1 r1, Node n2 l2 a2 r2) \<Rightarrow>
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    47
    if a1 \<le> a2 then node l1 a1 (meld r1 t2) else node l2 a2 (meld r2 t1))"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    48
by(induction t1 t2 rule: meld.induct) (simp_all split: tree.split)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    49
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    50
definition insert :: "'a::ord \<Rightarrow> 'a lheap \<Rightarrow> 'a lheap" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    51
"insert x t = meld (Node 1 Leaf x Leaf) t"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    52
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    53
fun del_min :: "'a::ord lheap \<Rightarrow> 'a lheap" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    54
"del_min Leaf = Leaf" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    55
"del_min (Node n l x r) = meld l r"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    56
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    57
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    58
subsection "Lemmas"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    59
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    60
declare Let_def [simp]
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    61
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    62
lemma rk_eq_rank[simp]: "lheap t \<Longrightarrow> rk t = rank t"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    63
by(cases t) auto
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    64
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    65
lemma lheap_node: "lheap (node l a r) \<longleftrightarrow> lheap l \<and> lheap r"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    66
by(auto simp add: node_def)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    67
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    68
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    69
subsection "Functional Correctness"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    70
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    71
locale Priority_Queue =
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    72
fixes empty :: "'pq"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    73
and insert :: "'a \<Rightarrow> 'pq \<Rightarrow> 'pq"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    74
and get_min :: "'pq \<Rightarrow> 'a"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    75
and del_min :: "'pq \<Rightarrow> 'pq"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    76
and invar :: "'pq \<Rightarrow> bool"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    77
and mset :: "'pq \<Rightarrow> 'a multiset"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    78
assumes mset_empty: "mset empty = {#}"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    79
and mset_insert: "invar pq \<Longrightarrow> mset (insert x pq) = {#x#} + mset pq"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    80
and mset_del_min: "invar pq \<Longrightarrow> mset (del_min pq) = mset pq - {#get_min pq#}"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    81
and invar_insert: "invar pq \<Longrightarrow> invar (insert x pq)"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    82
and invar_del_min: "invar pq \<Longrightarrow> invar (del_min pq)"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    83
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    84
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    85
fun mset_tree :: "('a,'b) tree \<Rightarrow> 'a multiset" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    86
"mset_tree Leaf = {#}" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    87
"mset_tree (Node _ l a r) = {#a#} + mset_tree l + mset_tree r"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    88
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    89
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    90
lemma mset_meld: "mset_tree (meld h1 h2) = mset_tree h1 + mset_tree h2"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    91
by (induction h1 h2 rule: meld.induct) (auto simp add: node_def ac_simps)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    92
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    93
lemma mset_insert: "mset_tree (insert x t) = {#x#} + mset_tree t"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    94
by (auto simp add: insert_def mset_meld)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    95
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    96
lemma mset_del_min: "mset_tree (del_min h) = mset_tree h - {# get_min h #}"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    97
by (cases h) (auto simp: mset_meld ac_simps subset_mset.diff_add_assoc)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    98
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    99
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   100
lemma lheap_meld: "\<lbrakk> lheap l; lheap r \<rbrakk> \<Longrightarrow> lheap (meld l r)"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   101
proof(induction l r rule: meld.induct)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   102
  case (3 n1 l1 a1 r1 n2 l2 a2 r2)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   103
  show ?case (is "lheap(meld ?t1 ?t2)")
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   104
  proof cases
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   105
    assume "a1 \<le> a2"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   106
    hence "lheap (meld ?t1 ?t2) = lheap (node l1 a1 (meld r1 ?t2))" by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   107
    also have "\<dots> = (lheap l1 \<and> lheap(meld r1 ?t2))"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   108
      by(simp add: lheap_node)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   109
    also have "..." using "3.prems" "3.IH"(1)[OF `a1 \<le> a2`] by (simp)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   110
    finally show ?thesis .
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   111
  next (* analogous but automatic *)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   112
    assume "\<not> a1 \<le> a2"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   113
    thus ?thesis using 3 by(simp)(auto simp: lheap_node)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   114
  qed
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   115
qed simp_all
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   116
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   117
lemma lheap_insert: "lheap t \<Longrightarrow> lheap(insert x t)"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   118
by(simp add: insert_def lheap_meld del: meld.simps split: tree.split)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   119
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   120
lemma lheap_del_min: "lheap t \<Longrightarrow> lheap(del_min t)"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   121
by(cases t)(auto simp add: lheap_meld simp del: meld.simps)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   122
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   123
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   124
interpretation lheap: Priority_Queue
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   125
where empty = Leaf and insert = insert and del_min = del_min
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   126
and get_min = get_min and invar = lheap and mset = mset_tree
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   127
proof(standard, goal_cases)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   128
  case 1 show ?case by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   129
next
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   130
  case 2 show ?case by(rule mset_insert)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   131
next
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   132
  case 3 show ?case by(rule mset_del_min)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   133
next
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   134
  case 4 thus ?case by(rule lheap_insert)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   135
next
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   136
  case 5 thus ?case by(rule lheap_del_min)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   137
qed
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   138
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   139
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   140
subsection "Complexity"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   141
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   142
lemma pow2_rank_size1: "lheap t \<Longrightarrow> 2 ^ rank t \<le> size1 t"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   143
proof(induction t)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   144
  case Leaf show ?case by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   145
next
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   146
  case (Node n l a r)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   147
  hence "rank r \<le> rank l" by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   148
  hence *: "(2::nat) ^ rank r \<le> 2 ^ rank l" by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   149
  have "(2::nat) ^ rank \<langle>n, l, a, r\<rangle> = 2 ^ rank r + 2 ^ rank r"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   150
    by(simp add: mult_2)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   151
  also have "\<dots> \<le> size1 l + size1 r"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   152
    using Node * by (simp del: power_increasing_iff)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   153
  also have "\<dots> = size1 \<langle>n, l, a, r\<rangle>" by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   154
  finally show ?case .
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   155
qed
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   156
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   157
function t_meld :: "'a::ord lheap \<Rightarrow> 'a lheap \<Rightarrow> nat" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   158
"t_meld Leaf t2 = 1" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   159
"t_meld t2 Leaf = 1" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   160
"t_meld (Node n1 l1 a1 r1) (Node n2 l2 a2 r2) =
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   161
  (if a1 \<le> a2 then 1 + t_meld r1 (Node n2 l2 a2 r2)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   162
   else 1 + t_meld r2 (Node n1 l1 a1 r1))"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   163
by pat_completeness auto
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   164
termination by (relation "measure (%(t1,t2). rank t1 + rank t2)") auto
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   165
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   166
definition t_insert :: "'a::ord \<Rightarrow> 'a lheap \<Rightarrow> nat" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   167
"t_insert x t = t_meld (Node 1 Leaf x Leaf) t"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   168
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   169
fun t_del_min :: "'a::ord lheap \<Rightarrow> nat" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   170
"t_del_min Leaf = 1" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   171
"t_del_min (Node n l a r) = t_meld l r"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   172
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   173
lemma t_meld_rank: "t_meld l r \<le> rank l + rank r + 1"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   174
proof(induction l r rule: meld.induct)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   175
  case 3 thus ?case
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   176
    by(simp)(fastforce split: tree.splits simp del: t_meld.simps)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   177
qed simp_all
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   178
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   179
corollary t_meld_log: assumes "lheap l" "lheap r"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   180
  shows "t_meld l r \<le> log 2 (size1 l) + log 2 (size1 r) + 1"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   181
using le_log2_of_power[OF pow2_rank_size1[OF assms(1)]]
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   182
  le_log2_of_power[OF pow2_rank_size1[OF assms(2)]] t_meld_rank[of l r]
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   183
by linarith
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   184
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   185
corollary t_insert_log: "lheap t \<Longrightarrow> t_insert x t \<le> log 2 (size1 t) + 2"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   186
using t_meld_log[of "Node 1 Leaf x Leaf" t]
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   187
by(simp add: t_insert_def split: tree.split)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   188
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   189
lemma ld_ld_1_less:
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   190
  assumes "x > 0" "y > 0" shows "1 + log 2 x + log 2 y < 2 * log 2 (x+y)"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   191
proof -
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   192
  have 1: "2*x*y < (x+y)^2" using assms
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   193
    by(simp add: numeral_eq_Suc algebra_simps add_pos_pos)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   194
  show ?thesis
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   195
    apply(rule powr_less_cancel_iff[of 2, THEN iffD1])
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   196
     apply simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   197
    using assms 1 by(simp add: powr_add log_powr[symmetric] powr_numeral)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   198
qed
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   199
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   200
corollary t_del_min_log: assumes "lheap t"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   201
  shows "t_del_min t \<le> 2 * log 2 (size1 t) + 1"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   202
proof(cases t)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   203
  case Leaf thus ?thesis using assms by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   204
next
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   205
  case [simp]: (Node _ t1 _ t2)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   206
  have "t_del_min t = t_meld t1 t2" by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   207
  also have "\<dots> \<le> log 2 (size1 t1) + log 2 (size1 t2) + 1"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   208
    using \<open>lheap t\<close> by (auto simp: t_meld_log simp del: t_meld.simps)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   209
  also have "\<dots> \<le> 2 * log 2 (size1 t) + 1"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   210
    using ld_ld_1_less[of "size1 t1" "size1 t2"] by (simp)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   211
  finally show ?thesis .
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   212
qed
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   213
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   214
end