src/HOL/Data_Structures/Tree_Map.thy
changeset 63412 def97df48390
parent 63411 e051eea34990
child 67965 aaa31cd0caef
equal deleted inserted replaced
63410:9789ccc2a477 63412:def97df48390
     1 (* Author: Tobias Nipkow *)
     1 (* Author: Tobias Nipkow *)
     2 
     2 
     3 section {* Unbalanced Tree as Map *}
     3 section \<open>Unbalanced Tree Implementation of Map\<close>
     4 
     4 
     5 theory Tree_Map
     5 theory Tree_Map
     6 imports
     6 imports
     7   Tree_Set
     7   Tree_Set
     8   Map_by_Ordered
     8   Map_by_Ordered
     9 begin
     9 begin
    10 
    10 
    11 fun lookup :: "('a::cmp*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
    11 fun lookup :: "('a::linorder*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
    12 "lookup Leaf x = None" |
    12 "lookup Leaf x = None" |
    13 "lookup (Node l (a,b) r) x =
    13 "lookup (Node l (a,b) r) x =
    14   (case cmp x a of LT \<Rightarrow> lookup l x | GT \<Rightarrow> lookup r x | EQ \<Rightarrow> Some b)"
    14   (case cmp x a of LT \<Rightarrow> lookup l x | GT \<Rightarrow> lookup r x | EQ \<Rightarrow> Some b)"
    15 
    15 
    16 fun update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
    16 fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
    17 "update x y Leaf = Node Leaf (x,y) Leaf" |
    17 "update x y Leaf = Node Leaf (x,y) Leaf" |
    18 "update x y (Node l (a,b) r) = (case cmp x a of
    18 "update x y (Node l (a,b) r) = (case cmp x a of
    19    LT \<Rightarrow> Node (update x y l) (a,b) r |
    19    LT \<Rightarrow> Node (update x y l) (a,b) r |
    20    EQ \<Rightarrow> Node l (x,y) r |
    20    EQ \<Rightarrow> Node l (x,y) r |
    21    GT \<Rightarrow> Node l (a,b) (update x y r))"
    21    GT \<Rightarrow> Node l (a,b) (update x y r))"
    22 
    22 
    23 fun delete :: "'a::cmp \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
    23 fun delete :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
    24 "delete x Leaf = Leaf" |
    24 "delete x Leaf = Leaf" |
    25 "delete x (Node l (a,b) r) = (case cmp x a of
    25 "delete x (Node l (a,b) r) = (case cmp x a of
    26   LT \<Rightarrow> Node (delete x l) (a,b) r |
    26   LT \<Rightarrow> Node (delete x l) (a,b) r |
    27   GT \<Rightarrow> Node l (a,b) (delete x r) |
    27   GT \<Rightarrow> Node l (a,b) (delete x r) |
    28   EQ \<Rightarrow> if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"
    28   EQ \<Rightarrow> if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"