src/HOL/Data_Structures/AVL_Bal_Set.thy
changeset 71816 489c907b9e05
parent 71815 a86e37f4ad60
child 71818 986d5abbe77c
equal deleted inserted replaced
71815:a86e37f4ad60 71816:489c907b9e05
    44    Same AB \<Rightarrow> Same (Node AB (c,bc) C) |
    44    Same AB \<Rightarrow> Same (Node AB (c,bc) C) |
    45    Diff AB \<Rightarrow> (case bc of
    45    Diff AB \<Rightarrow> (case bc of
    46      Bal \<Rightarrow> Diff (Node AB (c,Lh) C) |
    46      Bal \<Rightarrow> Diff (Node AB (c,Lh) C) |
    47      Rh \<Rightarrow> Same (Node AB (c,Bal) C) |
    47      Rh \<Rightarrow> Same (Node AB (c,Bal) C) |
    48      Lh \<Rightarrow> Same(case AB of
    48      Lh \<Rightarrow> Same(case AB of
    49        Node A (ab,Lh) B \<Rightarrow> Node A (ab,Bal) (Node B (c,Bal) C) |
    49        Node A (a,Lh) B \<Rightarrow> Node A (a,Bal) (Node B (c,Bal) C) |
    50        Node A (ab,Rh) B \<Rightarrow> (case B of
    50        Node A (a,Rh) B \<Rightarrow> (case B of
    51          Node B\<^sub>1 (b, bb) B\<^sub>2 \<Rightarrow>
    51          Node B\<^sub>1 (b, bb) B\<^sub>2 \<Rightarrow>
    52            Node (Node A (ab,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,rot22 bb) C)))))"
    52            Node (Node A (a,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,rot22 bb) C)))))"
    53 
    53 
    54 fun balR :: "'a tree_bal \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal change \<Rightarrow> 'a tree_bal change" where
    54 fun balR :: "'a tree_bal \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal change \<Rightarrow> 'a tree_bal change" where
    55 "balR A a ba BC' = (case BC' of
    55 "balR A a ba BC' = (case BC' of
    56    Same BC \<Rightarrow> Same (Node A (a,ba) BC) |
    56    Same BC \<Rightarrow> Same (Node A (a,ba) BC) |
    57    Diff BC \<Rightarrow> (case ba of
    57    Diff BC \<Rightarrow> (case ba of
    58      Bal \<Rightarrow> Diff (Node A (a,Rh) BC) |
    58      Bal \<Rightarrow> Diff (Node A (a,Rh) BC) |
    59      Lh \<Rightarrow> Same (Node A (a,Bal) BC) |
    59      Lh \<Rightarrow> Same (Node A (a,Bal) BC) |
    60      Rh \<Rightarrow> Same(case BC of
    60      Rh \<Rightarrow> Same(case BC of
    61        Node B (bc,Rh) C \<Rightarrow> Node (Node A (a,Bal) B) (bc,Bal) C |
    61        Node B (c,Rh) C \<Rightarrow> Node (Node A (a,Bal) B) (c,Bal) C |
    62        Node B (bc,Lh) C \<Rightarrow> (case B of
    62        Node B (c,Lh) C \<Rightarrow> (case B of
    63          Node B\<^sub>1 (b, bb) B\<^sub>2 \<Rightarrow>
    63          Node B\<^sub>1 (b, bb) B\<^sub>2 \<Rightarrow>
    64            Node (Node A (a,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (bc,rot22 bb) C)))))"
    64            Node (Node A (a,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,rot22 bb) C)))))"
    65 
    65 
    66 fun insert :: "'a::linorder \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal change" where
    66 fun insert :: "'a::linorder \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal change" where
    67 "insert x Leaf = Diff(Node Leaf (x, Bal) Leaf)" |
    67 "insert x Leaf = Diff(Node Leaf (x, Bal) Leaf)" |
    68 "insert x (Node l (a, b) r) = (case cmp x a of
    68 "insert x (Node l (a, b) r) = (case cmp x a of
    69    EQ \<Rightarrow> Same(Node l (a, b) r) |
    69    EQ \<Rightarrow> Same(Node l (a, b) r) |
    70    LT \<Rightarrow> balL (insert x l) a b r |
    70    LT \<Rightarrow> balL (insert x l) a b r |
    71    GT \<Rightarrow> balR l a b (insert x r))"
    71    GT \<Rightarrow> balR l a b (insert x r))"
    72 
    72 
    73 fun baldR :: "'a tree_bal \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal change \<Rightarrow> 'a tree_bal change" where
    73 fun baldR :: "'a tree_bal \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal change \<Rightarrow> 'a tree_bal change" where
    74 "baldR AB bc b C' = (case C' of
    74 "baldR AB c bc C' = (case C' of
    75    Same C \<Rightarrow> Same (Node AB (bc,b) C) |
    75    Same C \<Rightarrow> Same (Node AB (c,bc) C) |
    76    Diff C \<Rightarrow> (case b of
    76    Diff C \<Rightarrow> (case bc of
    77      Bal \<Rightarrow> Same (Node AB (bc,Lh) C) |
    77      Bal \<Rightarrow> Same (Node AB (c,Lh) C) |
    78      Rh \<Rightarrow> Diff (Node AB (bc,Bal) C) |
    78      Rh \<Rightarrow> Diff (Node AB (c,Bal) C) |
    79      Lh \<Rightarrow> (case AB of
    79      Lh \<Rightarrow> (case AB of
    80        Node A (ab,Lh) B \<Rightarrow> Diff(Node A (ab,Bal) (Node B (bc,Bal) C)) |
    80        Node A (a,Lh) B \<Rightarrow> Diff(Node A (a,Bal) (Node B (c,Bal) C)) |
    81        Node A (ab,Bal) B \<Rightarrow> Same(Node A (ab,Rh) (Node B (bc,Lh) C)) |
    81        Node A (a,Bal) B \<Rightarrow> Same(Node A (a,Rh) (Node B (c,Lh) C)) |
    82        Node A (ab,Rh) B \<Rightarrow> (case B of
    82        Node A (a,Rh) B \<Rightarrow> (case B of
    83          Node B\<^sub>1 (bb, bB) B\<^sub>2 \<Rightarrow>
    83          Node B\<^sub>1 (b, bb) B\<^sub>2 \<Rightarrow>
    84            Diff(Node (Node A (ab,rot21 bB) B\<^sub>1) (bb,Bal) (Node B\<^sub>2 (bc,rot22 bB) C))))))"
    84            Diff(Node (Node A (a,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,rot22 bb) C))))))"
    85 
    85 
    86 fun baldL :: "'a tree_bal change \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal change" where
    86 fun baldL :: "'a tree_bal change \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal change" where
    87 "baldL A' ab b BC = (case A' of
    87 "baldL A' a ba BC = (case A' of
    88    Same A \<Rightarrow> Same (Node A (ab,b) BC) |
    88    Same A \<Rightarrow> Same (Node A (a,ba) BC) |
    89    Diff A \<Rightarrow> (case b of
    89    Diff A \<Rightarrow> (case ba of
    90      Bal \<Rightarrow> Same (Node A (ab,Rh) BC) |
    90      Bal \<Rightarrow> Same (Node A (a,Rh) BC) |
    91      Lh \<Rightarrow> Diff (Node A (ab,Bal) BC) |
    91      Lh \<Rightarrow> Diff (Node A (a,Bal) BC) |
    92      Rh \<Rightarrow> (case BC of
    92      Rh \<Rightarrow> (case BC of
    93        Node B (bc,Rh) C \<Rightarrow> Diff(Node (Node A (ab,Bal) B) (bc,Bal) C) |
    93        Node B (c,Rh) C \<Rightarrow> Diff(Node (Node A (a,Bal) B) (c,Bal) C) |
    94        Node B (bc,Bal) C \<Rightarrow> Same(Node (Node A (ab,Rh) B) (bc,Lh) C) |
    94        Node B (c,Bal) C \<Rightarrow> Same(Node (Node A (a,Rh) B) (c,Lh) C) |
    95        Node B (bc,Lh) C \<Rightarrow> (case B of
    95        Node B (c,Lh) C \<Rightarrow> (case B of
    96          Node B\<^sub>1 (bb, bB) B\<^sub>2 \<Rightarrow>
    96          Node B\<^sub>1 (b, bb) B\<^sub>2 \<Rightarrow>
    97            Diff(Node (Node A (ab,rot21 bB) B\<^sub>1) (bb,Bal) (Node B\<^sub>2 (bc,rot22 bB) C))))))"
    97            Diff(Node (Node A (a,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,rot22 bb) C))))))"
    98 
    98 
    99 fun split_max :: "'a tree_bal \<Rightarrow> 'a tree_bal change * 'a" where
    99 fun split_max :: "'a tree_bal \<Rightarrow> 'a tree_bal change * 'a" where
   100 "split_max (Node l (a, ba) r) =
   100 "split_max (Node l (a, ba) r) =
   101   (if r = Leaf then (Diff l,a) else let (r',a') = split_max r in (baldR l a ba r', a'))"
   101   (if r = Leaf then (Diff l,a) else let (r',a') = split_max r in (baldR l a ba r', a'))"
   102 
   102