src/HOL/Data_Structures/RBT_Set.thy
author nipkow
Tue Sep 22 08:38:25 2015 +0200 (2015-09-22)
changeset 61224 759b5299a9f2
child 61231 cc6969542f8d
permissions -rw-r--r--
added red black trees
     1 (* Author: Tobias Nipkow *)
     2 
     3 section \<open>Red-Black Tree Implementation of Sets\<close>
     4 
     5 theory RBT_Set
     6 imports
     7   RBT
     8   Isin2
     9 begin
    10 
    11 fun insert :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    12 "insert x Leaf = R Leaf x Leaf" |
    13 "insert x (B l a r) =
    14   (if x < a then bal (insert x l) a r else
    15    if x > a then bal l a (insert x r) else B l a r)" |
    16 "insert x (R l a r) =
    17   (if x < a then R (insert x l) a r
    18    else if x > a then R l a (insert x r) else R l a r)"
    19 
    20 fun delete :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    21 and deleteL :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    22 and deleteR :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    23 where
    24 "delete x Leaf = Leaf" |
    25 "delete x (Node _ l a r) = 
    26   (if x < a then deleteL x l a r 
    27    else if x > a then deleteR x l a r else combine l r)" |
    28 "deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
    29 "deleteL x l a r = R (delete x l) a r" |
    30 "deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
    31 "deleteR x l a r = R l a (delete x r)"
    32 
    33 
    34 subsection "Functional Correctness Proofs"
    35 
    36 lemma inorder_bal:
    37   "inorder(bal l a r) = inorder l @ a # inorder r"
    38 by(induction l a r rule: bal.induct) (auto simp: sorted_lems)
    39 
    40 lemma inorder_insert:
    41   "sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
    42 by(induction a t rule: insert.induct) (auto simp: ins_simps inorder_bal)
    43 
    44 lemma inorder_red: "inorder(red t) = inorder t"
    45 by(induction t) (auto simp: sorted_lems)
    46 
    47 lemma inorder_balL:
    48   "inorder(balL l a r) = inorder l @ a # inorder r"
    49 by(induction l a r rule: balL.induct)
    50   (auto simp: sorted_lems inorder_bal inorder_red)
    51 
    52 lemma inorder_balR:
    53   "inorder(balR l a r) = inorder l @ a # inorder r"
    54 by(induction l a r rule: balR.induct)
    55   (auto simp: sorted_lems inorder_bal inorder_red)
    56 
    57 lemma inorder_combine:
    58   "inorder(combine l r) = inorder l @ inorder r"
    59 by(induction l r rule: combine.induct)
    60   (auto simp: sorted_lems inorder_balL inorder_balR split: tree.split color.split)
    61 
    62 lemma inorder_delete:
    63  "sorted(inorder t) \<Longrightarrow>  inorder(delete x t) = del_list x (inorder t)" and
    64  "sorted(inorder l) \<Longrightarrow>  inorder(deleteL x l a r) =
    65     del_list x (inorder l) @ a # inorder r" and
    66  "sorted(inorder r) \<Longrightarrow>  inorder(deleteR x l a r) =
    67     inorder l @ a # del_list x (inorder r)"
    68 by(induction x t and x l a r and x l a r rule: delete_deleteL_deleteR.induct)
    69   (auto simp: del_simps inorder_combine inorder_balL inorder_balR)
    70 
    71 interpretation Set_by_Ordered
    72 where empty = Leaf and isin = isin and insert = insert and delete = delete
    73 and inorder = inorder and wf = "\<lambda>_. True"
    74 proof (standard, goal_cases)
    75   case 1 show ?case by simp
    76 next
    77   case 2 thus ?case by(simp add: isin_set)
    78 next
    79   case 3 thus ?case by(simp add: inorder_insert)
    80 next
    81   case 4 thus ?case by(simp add: inorder_delete)
    82 next
    83   case 5 thus ?case ..
    84 qed
    85 
    86 end