annotate RBTree.agda @ 2:7c9b8d601b77 default tip

fix deleteMin
author ryokka
date Thu, 30 May 2019 08:54:03 +0900
parents 7ed7c121d5b8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
1 open import Level renaming (suc to succ ; zero to Zero )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
2 module RBTree where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
3
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
4 -- {
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
5 -- RBTree,
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
6 -- Node,
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
7 -- Color,
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
8 -- hight,
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
9 -- nulln,
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
10 -- empty,
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
11 -- singleton
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
12 -- }
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
13
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
14 open import Data.List
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
15 open import Data.Maybe
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
16 open import Data.Nat
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
17 open import Data.Bool
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
18 open import Data.Empty
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
19 open import Relation.Binary.PropositionalEquality
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
20 open import Relation.Binary
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
21 open import Relation.Binary.Core
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
22 open import Relation.Nullary
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
23 -- open import Level
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
24
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
25 -- Builtin Function
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
26
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
27 _==_ : ℕ → ℕ → Bool
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
28 _==_ x y with compare x y
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
29 (x == .(ℕ.suc (x + k))) | less .x k = false
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
30 (x == .x) | equal .x = true
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
31 (.(ℕ.suc (y + k)) == y) | greater .y k = false
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
32
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
33 _-_ : ℕ → ℕ → ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
34 ℕ.zero - ℕ.zero = ℕ.zero
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
35 ℕ.zero - ℕ.suc y = ℕ.zero
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
36 ℕ.suc x - ℕ.zero = ℕ.suc x
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
37 ℕ.suc x - ℕ.suc y = x - y
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
38
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
39 contraposition : {m : Level } {A B : Set m} → (B → A) → (¬ A → ¬ B)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
40 contraposition f = λ x y → x (f y)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
41
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
42
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
43 compareTri1 : (n : ℕ) → zero <′ suc n
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
44 compareTri1 zero = ≤′-refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
45 compareTri1 (suc n) = ≤′-step ( compareTri1 n )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
46
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
47 compareTri2 : (n m : ℕ) → n ≤′ m → suc n ≤′ suc m
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
48 compareTri2 _ _ ≤′-refl = ≤′-refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
49 compareTri2 n (suc m) ( ≤′-step p ) = ≤′-step { suc n } ( compareTri2 n m p )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
50
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
51 <′dec : Set
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
52 <′dec = ∀ m n → Dec ( m ≤′ n )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
53
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
54 compareTri6 : ∀ m {n} → ¬ m ≤′ n → ¬ suc m ≤′ suc n
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
55
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
56 is≤′ : <′dec
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
57 is≤′ zero zero = yes ≤′-refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
58 is≤′ zero (suc n) = yes (lemma n)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
59 where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
60 lemma : (n : ℕ) → zero ≤′ suc n
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
61 lemma zero = ≤′-step ≤′-refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
62 lemma (suc n) = ≤′-step (lemma n)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
63 is≤′ (suc m) (zero) = no ( λ () )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
64 is≤′ (suc m) (suc n) with is≤′ m n
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
65 ... | yes p = yes ( compareTri2 _ _ p )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
66 ... | no p = no ( compareTri6 _ p )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
67
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
68 compareTri20 : {n : ℕ} → ¬ suc n ≤′ zero
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
69 compareTri20 ()
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
70
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
71
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
72 compareTri21 : (n m : ℕ) → suc n ≤′ suc m → n ≤′ m
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
73 compareTri21 _ _ ≤′-refl = ≤′-refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
74 compareTri21 (suc n) zero ( ≤′-step p ) = compareTri23 (suc n) ( ≤′-step p ) p
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
75 where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
76 compareTri23 : (n : ℕ) → suc n ≤′ suc zero → suc n ≤′ zero → n ≤′ zero
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
77 compareTri23 zero ( ≤′-step p ) _ = ≤′-refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
78 compareTri23 zero ≤′-refl _ = ≤′-refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
79 compareTri23 (suc n1) ( ≤′-step p ) ()
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
80 compareTri21 n (suc m) ( ≤′-step p ) = ≤′-step (compareTri21 _ _ p)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
81 compareTri21 zero zero ( ≤′-step p ) = ≤′-refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
82
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
83
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
84 compareTri3 : ∀ m {n} → ¬ m ≡ suc (m + n)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
85 compareTri3 zero ()
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
86 compareTri3 (suc m) eq = compareTri3 m (cong pred eq)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
87
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
88 compareTri4' : ∀ m {n} → ¬ n ≡ m → ¬ suc n ≡ suc m
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
89 compareTri4' m {n} with n Data.Nat.≟ m
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
90 ... | yes refl = λ x y → x refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
91 ... | no p = λ x y → p ( cong pred y )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
92
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
93 compareTri4 : ∀ m {n} → ¬ n ≡ m → ¬ suc n ≡ suc m
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
94 compareTri4 m = contraposition ( λ x → cong pred x )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
95
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
96 -- compareTri6 : ∀ m {n} → ¬ m ≤′ n → ¬ suc m ≤′ suc n
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
97 compareTri6 m {n} = λ x y → x (compareTri21 _ _ y)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
98
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
99 compareTri5 : ∀ m {n} → ¬ m <′ n → ¬ suc m <′ suc n
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
100 compareTri5 m {n} = compareTri6 (suc m)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
101
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
102
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
103 compareTri : Trichotomous _≡_ _<′_
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
104 compareTri zero zero = tri≈ ( λ ()) refl ( λ ())
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
105 compareTri zero (suc n) = tri< ( compareTri1 n ) ( λ ()) ( λ ())
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
106 compareTri (suc n) zero = tri> ( λ ()) ( λ ()) ( compareTri1 n )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
107 compareTri (suc n) (suc m) with compareTri n m
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
108 ... | tri< p q r = tri< (compareTri2 (suc n) m p ) (compareTri4 _ q) ( compareTri5 _ r )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
109 ... | tri≈ p refl r = tri≈ (compareTri5 _ p) refl ( compareTri5 _ r )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
110 ... | tri> p q r = tri> ( compareTri5 _ p ) (compareTri4 _ q) (compareTri2 (suc m) n r )
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
111
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
112
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
113 data Color : Set where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
114 red : Color
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
115 black : Color
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
116
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
117 record Node {a : Set} : Set where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
118 inductive
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
119 field
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
120 color : Color
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
121 blackHeight : ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
122 right : Node {a}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
123 datum : ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
124 left : Node {a}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
125 open Node
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
126
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
127 -- node は node であり 部分的に分けられた redblacktree そのもの
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
128
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
129 data RBTree {n : Level} (a : Set n) : Set n where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
130 leaf : RBTree a
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
131 node : Color → ℕ → RBTree a → ℕ → RBTree a → RBTree a
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
132
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
133
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
134 record RBTreeBDel {n : Level} (a : Set n) : Set n where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
135 field
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
136 tree : RBTree a
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
137 bool : Bool
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
138 open RBTreeBDel
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
139
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
140
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
141 module _ { n m : Level} {a : Set n} {cg : Set m} where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
142 -- _,_ : (tree : RBTree a) → (b : Bool) → Set
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
143 -- leaf , false = (leaf , false)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
144 -- leaf , true = {!!}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
145 -- (node x x₁ t x₂ t₁) , bb = {!!}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
146
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
147
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
148 hight? : RBTree a → ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
149 hight? leaf = 0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
150 hight? (node c h l x r) = h
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
151
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
152 isLeaf : RBTree a → Bool
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
153 isLeaf leaf = true
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
154 isLeaf (node x x₁ t x₂ t₁) = false
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
155
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
156 empty : RBTree a
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
157 empty = leaf
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
158
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
159 singleton : ℕ → RBTree a
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
160 singleton x = (node black 1 leaf x leaf)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
161
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
162 isMember : ℕ → RBTree ℕ → Bool
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
163 isMember _ leaf = false
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
164 isMember x (node _ _ l y r) with compare x y
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
165 isMember x (node _ _ l y r) | less x k = isMember x l
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
166 isMember x (node _ _ l x r) | equal x = true
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
167 isMember x (node _ _ l y r) | greater y k = isMember x r
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
168
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
169 blacks? : RBTree a → ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
170 blacks? = blacks' 0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
171 where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
172 blacks' : ℕ → RBTree a → ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
173 blacks' n leaf = (n + 1)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
174 blacks' n (node red _ l _ r) = (blacks' n l) + (blacks' n r)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
175 blacks' n (node black _ l _ r) = (blacks' (n + 1) l) + (blacks' (n + 1) r)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
176
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
177 reds : Color → RBTree a → Bool
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
178 reds _ leaf = true
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
179 reds red (node x x₁ t x₂ t₁) = false
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
180 reds black (node c x₁ l x₂ r) = reds c l ∧ reds c r
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
181
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
182 turnR : RBTree a → (RBTree a → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
183 turnR leaf cg = cg leaf
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
184 turnR (node _ h l x r) cg = cg (node red h l x r)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
185
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
186 turnB : RBTree a → (RBTree a → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
187 turnB leaf cg = cg leaf
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
188 turnB (node _ h l x r) cg = cg (node black h l x r)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
189
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
190 minimum : RBTree ℕ → ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
191 minimum (node c h leaf x r) = x
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
192 minimum (node c h l x r) = minimum l
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
193 minimum leaf = 774
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
194
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
195 maximum : RBTree ℕ → ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
196 maximum (node c x₁ t x leaf) = x
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
197 maximum (node x x₁ t x₂ r) = maximum r
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
198 maximum leaf = 0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
199
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
200 isRed : RBTree a → Bool
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
201 isRed (node red x₁ t x₂ t₁) = true
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
202 isRed _ = false
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
203
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
204 isColorSame : Color → Color → (Bool → cg) → cg
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
205 isColorSame red red cg = cg true
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
206 isColorSame red black cg = cg false
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
207 isColorSame black red cg = cg false
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
208 isColorSame black black cg = cg true
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
209
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
210 node→ℕ : Maybe (RBTree a) → Maybe ℕ
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
211 node→ℕ (just leaf) = nothing
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
212 node→ℕ (just (node x x₁ x₂ x₃ x₄)) = just x₃
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
213 node→ℕ nothing = nothing
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
214
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
215
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
216 -- basic operations
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
217 -- Code Gear Style (CPS : Contination Passing Style)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
218
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
219 -- balance
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
220
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
221
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
222
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
223
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
224 module _ { n m : Level} {a : Set n} {cg : Set m} where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
225
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
226 balanceL' : ℕ → RBTree a → ℕ → RBTree a → (RBTree a → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
227 balanceL' h (node red h1 (node red hl a x b) y c) z d cg = cg (node red (h + 1) (node black h a x b) y (node black h c z d))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
228 balanceL' h (node red _ a x (node red _ b y c)) z d cg = cg (node red (h + 1) (node black h a x b) y (node black h c z d))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
229 balanceL' h l x r cg = cg (node black h l x r)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
230
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
231 balanceR' : ℕ -> RBTree a -> ℕ -> RBTree a -> (RBTree a → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
232 balanceR' h a x (node red _ b y (node red _ c z d)) cg = cg (node red (h + 1) (node black h a x b) y (node black h c z d))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
233 balanceR' h a x (node red _ (node red _ b y c) z d) cg = cg (node red (h + 1) (node black h a x b) y (node black h c z d))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
234 balanceR' h l x r cg = cg (node black h l x r)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
235
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
236 balanceL : Color → ℕ → RBTree a → ℕ → RBTree a → (RBTree a → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
237 balanceL black h (node red h1 (node red hl a x b) y c) z d cg = cg (node red (h + 1) (node black h a x b) y (node black h c z d))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
238 balanceL black h (node red _ a x (node red _ b y c)) z d cg = cg (node red (h + 1) (node black h a x b) y (node black h c z d))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
239 balanceL k h l x r cg = cg (node k h l x r)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
240
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
241 balanceR : Color → ℕ -> RBTree a -> ℕ -> RBTree a -> (RBTree a → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
242 balanceR black h a x (node red _ b y (node red _ c z d)) cg = cg (node red (h + 1) (node black h a x b) y (node black h c z d))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
243 balanceR black h a x (node red _ (node red _ b y c) z d) cg = cg (node red (h + 1) (node black h a x b) y (node black h c z d))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
244 balanceR k h l x r cg = cg (node k h l x r)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
245
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
246 -- unbalance
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
247
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
248 -- unbalanceL' : Color → ℕ -> RBTree a -> a -> RBTree a -> (RBTreeBDel a → cg) → cg
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
249 -- unbalanceL' c h l@(node black _ _ _ _) x r cg = isColorSame c black ( λ issame → cg record { tree = {!!} ; bool = issame })
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
250 -- unbalanceL' c h l x r cg = {!!}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
251
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
252 unbalanceL : Color → ℕ → RBTree a → ℕ → RBTree a → (RBTreeBDel a → cg) → cg
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
253 unbalanceL c h l@(node black _ _ _ _) x r cg = turnR l (λ tl → (balanceL black h tl x r (λ t → isColorSame {n} {m} {a} {_} c black (λ issame → cg record {tree = t ; bool = issame}))))
2
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
254 unbalanceL black h l@(node red lh ll lx lr@(node black _ _ _ _)) x r cg = turnR lr (λ tlr → balanceL black h tlr x r (λ t → cg record { tree = (node black lh ll lx t) ; bool = false}))
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
255 unbalanceL c h l x r cg = cg record {tree = leaf ; bool = false}
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
256
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
257 unbalanceR : Color → ℕ → RBTree a → ℕ → RBTree a → (RBTreeBDel a → cg) → cg
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
258 unbalanceR c h l x r@(node black _ _ _ _) cg = turnR r (λ tr → balanceR black h l x r (λ t → isColorSame {n} {m} {a} {_} c black (λ issame → cg record {tree = t ; bool = issame})))
2
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
259 unbalanceR black h l x (node red rh rl@(node black _ _ _ _) rx rr) cg = turnR rl (λ trl → balanceR black h l x trl (λ t → cg record { tree = (node black rh t rx rr); bool = false}))
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
260 unbalanceR c h l x r cg = cg record {tree = leaf ; bool = false}
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
261
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
262
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
263
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
264 {--
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
265 Insert Function
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
266
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
267 All Nodes → ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
268 search Insert Location → O(log ℕ)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
269 fix Tree (balanceR,L) → O(log ℕ)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
270 Root Child Color Fix → O(1)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
271 Total → O(2 log ℕ + 1)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
272 --}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
273
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
274 -- turnRtree : RBTree a → (RBTree a → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
275 -- turnRtree leaf cg = cg leaf
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
276 -- -- turnRtree (node black h l@(node black _ _ _ _) x r@(node black _ _ _ _)) cg = turnRtree (node red h l x r) cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
277 -- turnRtree (node c h l x r) cg = turnRtree r cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
278
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
279 rootChildFix : RBTree a → (RBTree a → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
280 rootChildFix (node black h (node black lh ll@(node black llh lll llx llr) lx lr@(node black lrh lrl lrx lrr)) x (node black rh rl@(node black rlh rll rlx rlr) rx rr@(node black rrh rrl rrx rrr))) cg = cg (node black (h - 1) (node red (lh - 1) ll lx lr) x (node red (rh - 1) rl rx rr))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
281 rootChildFix n cg = cg n
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
282
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
283 insert' : ℕ → RBTree a → (RBTree a → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
284 insert' kx leaf cg = cg (node red 1 leaf kx leaf)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
285
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
286 insert' kx (node black h l x r) cg with compare kx x
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
287 insert' kx (node black h l x r) cg | less kx k = insert' kx l (λ t → balanceL' h t x r (λ tt → cg tt))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
288 insert' kx s@(node black h l x r) cg | equal kx = cg s
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
289 insert' kx (node black h l x r) cg | greater x k = insert' kx r (λ t → balanceR' h l x t (λ tt → cg tt))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
290
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
291 insert' kx (node red h l x r) cg with compare kx x
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
292 insert' kx (node red h l x r) cg | less kx k = insert' kx l (λ t → cg (node red h t x r))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
293 insert' kx s@(node red h l x r) cg | equal kx = cg s
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
294 insert' kx (node red h l x r) cg | greater x k = insert' kx r (λ t → cg (node red h l x t))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
295
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
296 insert : ℕ → RBTree a → (RBTree a → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
297 insert kx t cg = insert' kx t (λ t1 → turnB t1 (λ t2 → rootChildFix t2 cg))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
298
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
299
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
300 get : ℕ → RBTree a → (Maybe (RBTree a) → cg) → cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
301 get x leaf cg = cg nothing
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
302 get x (node c h l x1 r) cg with compareTri x x1
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
303 get x (node c h l x1 r) cg | tri< a ¬b ¬c = get x l cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
304 get x t@(node c h l x1 r) cg | tri≈ ¬a b ¬c = cg (just t)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
305 get x (node c h l x1 r) cg | tri> ¬a ¬b c₁ = get x r cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
306
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
307
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
308
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
309 {- Check Balance, Color rule and hight calc -}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
310
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
311 -- create RBTree from Nat List
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
312 fromList : List ℕ → RBTree ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
313 fromList [] = empty {_} {_} {ℕ} {Set}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
314 fromList (x ∷ l) = (maketree x (empty {_} {_} {ℕ} {Set}) (λ t → fromList' l t (λ tt → tt)))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
315 where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
316 module _ {l : Level} {cg1 : Set l} {a : Set} where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
317 maketree : ℕ → RBTree a → (RBTree a → cg1) → cg1
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
318 maketree x t1 cg = insert x t1 cg
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
319 fromList' : List ℕ → RBTree a → (RBTree a → cg1) → cg1
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
320 fromList' [] t cg = cg t
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
321 fromList' (x1 ∷ nl) t cg = maketree x1 t (λ tt → fromList' nl tt (λ ttt → cg ttt))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
322
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
323
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
324 c1 = fromList (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ [])
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
325 c2 = fromList (5 ∷ 1 ∷ 2 ∷ 3 ∷ 4 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ [])
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
326 c3 = fromList (8 ∷ 7 ∷ 6 ∷ 1 ∷ 2 ∷ 5 ∷ 4 ∷ 3 ∷ 9 ∷ [])
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
327
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
328 cget = get 6 c1 (λ a → a)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
329
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
330
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
331 proof-leaf : {l : Level} {D : Set l} (n : ℕ) → insert {_} {_} {ℕ} {Set} n leaf (λ t1 → get n t1 (λ n1 → (just n ≡ (node→ℕ {_} {_} {ℕ} {ℕ} n1))))
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
332 proof-leaf n with n Data.Nat.≟ n | compareTri n n
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
333 proof-leaf n | yes p | tri< a ¬b ¬c = ⊥-elim (¬b p)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
334 proof-leaf n | yes p | tri≈ ¬a b ¬c = refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
335 proof-leaf n | yes p | tri> ¬a ¬b c = ⊥-elim (¬b p)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
336 proof-leaf n | no ¬p | tri< a ¬b ¬c = ⊥-elim (¬c a)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
337 proof-leaf n | no ¬p | tri≈ ¬a b ¬c = refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
338 proof-leaf n | no ¬p | tri> ¬a ¬b c = ⊥-elim (¬a c)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
339
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
340
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
341
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
342
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
343 -- 任意の木だと insert でノードをどこに入れるか決められず計算が進まない
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
344 -- 任意の木の root が Nothing のケースはもちろんできる
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
345 -- proof1 : {l : Level} {D : Set l} (n : ℕ) → (t : RBTree D) → insert n t (λ t1 → get n t1 (λ n1 → (just n ≡ (root→ℕ n1))))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
346 -- proof1 n t with n Data.Nat.≟ n | compareTri n n
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
347 -- proof1 n t | yes p | tri< a ¬b ¬c = ⊥-elim (¬b p)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
348 -- proof1 n t | yes p | tri> ¬a ¬b c = ⊥-elim (¬b p)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
349 -- proof1 n t | no ¬p | tri< a ¬b ¬c = ⊥-elim (¬c a)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
350 -- proof1 n t | no ¬p | tri> ¬a ¬b c = ⊥-elim (¬a c)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
351 -- proof1 n leaf | yes p | tri≈ ¬a b ¬c with compareTri n n
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
352 -- proof1 n leaf | yes p | tri≈ ¬a b ¬c | tri< a ¬b ¬c₁ = ⊥-elim (¬b p)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
353 -- proof1 n leaf | yes p | tri≈ ¬a b ¬c | tri≈ ¬a₁ b₁ ¬c₁ = refl
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
354 -- proof1 n leaf | yes p | tri≈ ¬a b ¬c | tri> ¬a₁ ¬b c = ⊥-elim (¬b p)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
355 -- proof1 n (node x x₁ t x₂ t₁) | yes p | tri≈ ¬a b ¬c = {!!}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
356 -- proof1 n t | no ¬p | tri≈ ¬a b ¬c = {!!}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
357
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
358
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
359 {--
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
360 書くなら全ての探索が木の高さ分で終わることを証明しとく必要がありそう
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
361 あと探索で木の高さが消化されていくことも示す
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
362 stack の push は存在してるデータ構造に関係なく上に乗るから任意のstack で証明できる
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
363 Tree でデータの保存を確かめるには…?
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
364
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
365 tree で証明するくらいだったら、親子関係、
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
366 rb 含めると、色関係、枝の長さ、になりそう?
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
367 --}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
368
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
369 module _ { n m : Level} {a : Set n} {cg : Set m} where
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
370
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
371 {--
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
372 deleteMin' は後ろに bool(Color の変更情報?)を持ちながら再帰する
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
373 再帰に一つ関数が挟まってるけど互いに呼んでるからどう書くべきかわからん
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
374
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
375 ```Haskell Source
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
376 deleteMin' (Node c h l x r) = if d then (tD, m) else (tD', m)
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
377
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
378 ((l', d), m) = deleteMin' l
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
379 tD = unbalancedR c (h-1) l' x r
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
380 tD' = (Node c h l' x r, False)
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
381 ```
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
382
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
383 たぶん ((l', d), m) の形になると deleteMin' がしたい
2
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
384 ~~RBTreeBDel の bool 情報はleaf に着いたかどうかだけ~~ → ではなく unbalance する必要があるのかの確認
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
385
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
386 ん、じゃあ node の左に leaf がついてる最小パターン探せばいいのでは?
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
387
2
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
388 あるパターンは (leaf x leaf), (leaf x r) の2パターンと r が red のケースを含めて 3パターン
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
389 その上のノードからそれを確認すれば良いので
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
390 (node c h (node lc lh leaf lx leaf) x r) と lr が存在するケースを確認すると良い
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
391 終わった tree に対して unbalance するかしないかを確定させたら終わり
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
392 --}
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
393
2
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
394
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
395
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
396 {-# TERMINATING #-}
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
397 deleteMin' : RBTree ℕ → (RBTreeBDel ℕ → cg) → cg
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
398
2
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
399 deleteMin' leaf cg = cg record {tree = leaf ; bool = false} -- unreachable case
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
400 deleteMin' (node black _ leaf x leaf) cg = cg record {tree = leaf ; bool = true}
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
401 deleteMin' (node black _ leaf x r@(node red _ _ _ _)) cg = turnB r (λ tr → cg record {tree = tr ; bool = false})
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
402 deleteMin' (node red _ leaf x r) cg = cg record {tree = r ; bool = false}
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
403 deleteMin' (node c h l x r) cg = deleteMin' l (λ dt → delCheck {n} {m} {a} dt (λ dct → cg dct))
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
404 where
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
405 delCheck : { n m : Level} {a : Set n} {cg : Set m} → RBTreeBDel ℕ → (RBTreeBDel ℕ → cg) → cg
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
406 delCheck bt cg with (bool bt)
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
407 delCheck record { tree = leaf ; bool = b } cg | false = cg record {tree = (node red 100 leaf 100 leaf) ; bool = b } -- unreachable case
2
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
408 delCheck record { tree = leaf ; bool = b } cg | true = unbalanceR c (h - 1) leaf x r (λ result → cg result)
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
409 -- delCheck record { tree = (node x₁ x₂ t₁ x₃ t₂) ; bool = b } cg | false = cg record {tree = (node x₁ x₂ t₁ x₃ t₂) ; bool = false}
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
410 delCheck record { tree = (node x₁ x₂ t₁ x₃ t₂) ; bool = b } cg | _ = unbalanceR c (h - 1) (node x₁ x₂ t₁ x₃ t₂) x r (λ result → cg result)
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
411
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
412 {--
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
413 deleteMin では最後に帰ってきたやつを turnB
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
414 型は rbtree → (rbtree → cg) → cg
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
415
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
416 rbtree が tree のとき deleteMin' tree
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
417 ((s, _), _) は RBTreeBDel から RBTree への変換なので outPutTree
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
418 --}
2
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
419
1
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
420 deleteMin : RBTree ℕ → (RBTree ℕ → cg) → cg
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
421 deleteMin leaf cg = cg leaf
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
422 deleteMin tree cg = deleteMin' tree (λ dt → outPutTree {n} {m} {a} dt (λ dct → turnB dct (λ dctb → cg dctb)))
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
423 where
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
424 outPutTree : { n m : Level} {a : Set n} {cg : Set m} → RBTreeBDel ℕ → (RBTree ℕ → cg) → cg
7ed7c121d5b8 fix DeleteFunction but not all
ryokka
parents: 0
diff changeset
425 outPutTree record { tree = s ; bool = b } cg = cg s
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
426
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
427
2
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
428 -- > delCheck の node があって false のケースも unbalanceしなきゃいけなかった
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
429 d1 = deleteMin (fromList (3 ∷ 1 ∷ 2 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ []))
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
430 l1 = fromList (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ [])
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
431 l2 = fromList ( 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ [])
7c9b8d601b77 fix deleteMin
ryokka
parents: 1
diff changeset
432 d2 = deleteMin (fromList ( 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ []))
0
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
433
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
434
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
435 -- delete' : {a : Set} → ℕ → RBTree ℕ → RBTreeBDel ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
436 -- delete' _ leaf = record { t = leaf ; b = false}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
437 -- delete' x (node c h l y r) with compare x y
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
438 -- delete' x (node c h l y r) | less .x k = delCheck (delete' {ℕ} x l)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
439 -- where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
440 -- delCheck : RBTreeBDel ℕ → RBTreeBDel ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
441 -- delCheck bt with (b bt)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
442 -- delCheck record { t = leaf ; b = b } | false = record {t = (node red 100 leaf 100 leaf) ; b = b } -- unreachable case
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
443 -- delCheck record { t = leaf ; b = b } | true = unbalanceR {ℕ} c (h - 1) leaf x r
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
444 -- delCheck record { t = (node x₁ x₂ t₁ x₃ t₂) ; b = b } | false = deleteMin' (node x₁ x₂ t₁ x₃ t₂)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
445 -- delCheck record { t = (node x₁ (x₂) t₁ x₃ t₂) ; b = b } | true = unbalanceR {ℕ} c (h - 1) (node x₁ x₂ t₁ x₃ t₂) x r
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
446 -- delete' x (node c h l y r) | greater .y k = delCheck (delete' {ℕ} x r)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
447 -- where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
448 -- delCheck : RBTreeBDel ℕ → RBTreeBDel ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
449 -- delCheck bt with (b bt)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
450 -- delCheck record { t = leaf ; b = b } | false = record {t = (node red 100 leaf 100 leaf) ; b = b } -- unreachable case
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
451 -- delCheck record { t = leaf ; b = b } | true = unbalanceR {ℕ} c (h - 1) leaf x r
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
452 -- delCheck record { t = (node x₁ x₂ t₁ x₃ t₂) ; b = b } | false = deleteMin' (node x₁ x₂ t₁ x₃ t₂)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
453 -- delCheck record { t = (node x₁ (x₂) t₁ x₃ t₂) ; b = b } | true = unbalanceR {ℕ} c (h - 1) (node x₁ x₂ t₁ x₃ t₂) x r
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
454 -- delete' x (node c h l .x leaf) | equal .x with checkColor {ℕ} c black
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
455 -- delete' x (node c h l .x leaf) | equal .x | false = record {t = l ; b = false}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
456 -- delete' x (node c h l .x leaf) | equal .x | true = blackify l
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
457 -- where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
458 -- blackify : RBTree ℕ → RBTreeBDel ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
459 -- blackify (node red x₂ a x₃ a₁) = record {t = turnB {ℕ} (node red x₂ a x₃ a₁) ; b = false}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
460 -- blackify s = record {t = s; b = true}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
461 -- delete' x (node c h l .x (node x₁ x₂ r x₃ r₁)) | equal .x with r
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
462 -- delete' x (node c h l .x (node x₁ x₂ r x₃ r₁)) | equal .x | leaf with checkColor {ℕ} c black
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
463 -- delete' x (node c h l .x (node x₁ x₂ r x₃ r₁)) | equal .x | leaf | false = blackify l
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
464 -- where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
465 -- blackify : RBTree ℕ → RBTreeBDel ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
466 -- blackify (node red x₂ a x₃ a₁) = record {t = turnB {ℕ} (node red x₂ a x₃ a₁) ; b = false}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
467 -- blackify s = record {t = s; b = true}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
468 -- delete' x (node c h l .x (node x₁ x₂ r x₃ r₁)) | equal .x | leaf | true = record {t = l ; b = false}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
469 -- delete' x (node c h l .x (node x₁ x₂ r x₃ r₁)) | equal .x | node x₄ x₅ rnod x₆ rnod₁ = {!!}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
470
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
471 -- -- where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
472 -- -- delCheck : RBTreeBDel ℕ → RBTreeBDel ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
473 -- -- delCheck bt with (b bt)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
474 -- -- delCheck record { t = leaf ; b = b } | false = record {t = (node red 100 leaf 100 leaf) ; b = b } -- unreachable case
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
475 -- -- delCheck record { t = leaf ; b = b } | true = unbalanceR {ℕ} c (h - 1) leaf x r
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
476 -- -- delCheck record { t = (node x₁ x₂ t₁ x₃ t₂) ; b = b } | false = deleteMin' (node x₁ x₂ t₁ x₃ t₂)
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
477 -- -- delCheck record { t = (node x₁ (x₂) t₁ x₃ t₂) ; b = b } | true = unbalanceR {ℕ} c (h - 1) (node x₁ x₂ t₁ x₃ t₂) x r
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
478 -- -- 謎フラグがtrue なら バランスしろとのお達しなのでどっかで持ちましょうはい。
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
479 -- -- 多分 record で適当に持つのがよいっぽい
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
480
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
481 -- delete : {a : Set} → ℕ → RBTree ℕ → RBTree ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
482 -- delete kx t = turnB {ℕ} (outPutTree (delete' {ℕ} kx t))
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
483 -- where
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
484 -- outPutTree : RBTreeBDel ℕ → RBTree ℕ
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
485 -- outPutTree record { t = s ; b = b } = s
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
486
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
487 -- proofs
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
488 -- isOrdered : RBTree a → Bool
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
489 -- isOrdered t = {!!}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
490
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
491 -- isBlackSame : RBTree a → Bool
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
492 -- isBlackSame t = {!!}
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
493
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
494 -- isBalanced : RBTree a → Bool
6bd95a31ffef add Insert Function
ryokka
parents:
diff changeset
495 -- isBalanced t = {!!}