annotate RedBlackTree.agda @ 738:da56e6fb7667

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 22 Apr 2023 14:54:28 +0900
parents 8df36383ced0
children 0b791ae19543
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
1 module RedBlackTree where
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
2
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
3
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
4 open import Level hiding (zero)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
5
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
6 open import Data.Nat hiding (compare)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
7 open import Data.Nat.Properties as NatProp
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
8 open import Data.Maybe
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
9 -- open import Data.Bool
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
10 open import Data.Empty
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
11
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
12 open import Relation.Binary
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
13 open import Relation.Binary.PropositionalEquality
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
14
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
15 open import logic
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
16
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
17 open import stack
564
40ab3d39e49d using strict total order
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 551
diff changeset
18
511
044c25475ed4 fix stack.agda
mir3636
parents: 478
diff changeset
19 record TreeMethods {n m : Level } {a : Set n } {t : Set m } (treeImpl : Set n ) : Set (m Level.⊔ n) where
044c25475ed4 fix stack.agda
mir3636
parents: 478
diff changeset
20 field
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
21 putImpl : treeImpl → a → (treeImpl → t) → t
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
22 getImpl : treeImpl → (treeImpl → Maybe a → t) → t
511
044c25475ed4 fix stack.agda
mir3636
parents: 478
diff changeset
23 open TreeMethods
044c25475ed4 fix stack.agda
mir3636
parents: 478
diff changeset
24
044c25475ed4 fix stack.agda
mir3636
parents: 478
diff changeset
25 record Tree {n m : Level } {a : Set n } {t : Set m } (treeImpl : Set n ) : Set (m Level.⊔ n) where
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
26 field
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
27 tree : treeImpl
513
f2a3acc766b5 fix RedBlackTree.agda
ryokka
parents: 512
diff changeset
28 treeMethods : TreeMethods {n} {m} {a} {t} treeImpl
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
29 putTree : a → (Tree treeImpl → t) → t
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
30 putTree d next = putImpl (treeMethods ) tree d (\t1 → next (record {tree = t1 ; treeMethods = treeMethods} ))
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
31 getTree : (Tree treeImpl → Maybe a → t) → t
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
32 getTree next = getImpl (treeMethods ) tree (\t1 d → next (record {tree = t1 ; treeMethods = treeMethods} ) d )
427
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
33
478
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
34 open Tree
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
35
513
f2a3acc766b5 fix RedBlackTree.agda
ryokka
parents: 512
diff changeset
36 data Color {n : Level } : Set n where
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
37 Red : Color
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
38 Black : Color
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
39
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
40
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
41 record Node {n : Level } (a : Set n) : Set n where
513
f2a3acc766b5 fix RedBlackTree.agda
ryokka
parents: 512
diff changeset
42 inductive
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
43 field
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
44 key : ℕ
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
45 value : a
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
46 right : Maybe (Node a )
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
47 left : Maybe (Node a )
514
f86da73d611e fix RedBlackTree.agda
ryokka
parents: 513
diff changeset
48 color : Color {n}
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
49 open Node
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
50
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
51 record RedBlackTree {n m : Level } {t : Set m} (a : Set n) : Set (m Level.⊔ n) where
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
52 field
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
53 root : Maybe (Node a )
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
54 nodeStack : SingleLinkedStack (Node a )
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
55
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
56 open RedBlackTree
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
57
543
1595dd84fc3e fix use SingleLinkedStack
ryokka
parents: 542
diff changeset
58 open SingleLinkedStack
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
59
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
60 compTri : ( x y : ℕ ) -> Tri ( x < y ) ( x ≡ y ) ( x > y )
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
61 compTri = IsStrictTotalOrder.compare (Relation.Binary.StrictTotalOrder.isStrictTotalOrder <-strictTotalOrder)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
62 where open import Relation.Binary
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
63
518
c9f90f573efe add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 515
diff changeset
64 -- put new node at parent node, and rebuild tree to the top
c9f90f573efe add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 515
diff changeset
65 --
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
66 {-# TERMINATING #-}
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
67 replaceNode : {n m : Level } {t : Set m } {a : Set n} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a ) → Node a → (RedBlackTree {n} {m} {t} a → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
68 replaceNode {n} {m} {t} {a} tree s n0 next = popSingleLinkedStack s (
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
69 \s parent → replaceNode1 s parent)
570
a6aa2ff5fea4 separate clearStack
ryokka
parents: 564
diff changeset
70 module ReplaceNode where
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
71 replaceNode1 : SingleLinkedStack (Node a) → Maybe ( Node a ) → t
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
72 replaceNode1 s nothing = next ( record tree { root = just (record n0 { color = Black}) } )
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
73 replaceNode1 s (just n1) with compTri (key n1) (key n0)
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
74 replaceNode1 s (just n1) | tri< lt ¬eq ¬gt = replaceNode {n} {m} {t} {a} tree s ( record n1 { value = value n0 ; left = left n0 ; right = right n0 } ) next
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
75 replaceNode1 s (just n1) | tri≈ ¬lt eq ¬gt = replaceNode {n} {m} {t} {a} tree s ( record n1 { left = just n0 } ) next
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
76 replaceNode1 s (just n1) | tri> ¬lt ¬eq gt = replaceNode {n} {m} {t} {a} tree s ( record n1 { right = just n0 } ) next
478
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
77
525
0696b9846415 write rotateRithg. but not working now
ryokka
parents: 523
diff changeset
78
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
79 rotateRight : {n m : Level } {t : Set m } {a : Set n} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Maybe (Node a) → Maybe (Node a) →
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
80 (RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a ) → Maybe (Node a) → Maybe (Node a) → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
81 rotateRight {n} {m} {t} {a} tree s n0 parent rotateNext = getSingleLinkedStack s (\ s n0 → rotateRight1 {n} {m} {t} {a} tree s n0 parent rotateNext)
530
63f3df7f5448 add insertCase41, insertCase5
ryokka
parents: 529
diff changeset
82 where
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
83 rotateRight1 : {n m : Level } {t : Set m } {a : Set n} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Maybe (Node a) → Maybe (Node a) →
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
84 (RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Maybe (Node a) → Maybe (Node a) → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
85 rotateRight1 {n} {m} {t} {a} tree s n0 parent rotateNext with n0
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
86 ... | nothing = rotateNext tree s nothing n0
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
87 ... | just n1 with parent
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
88 ... | nothing = rotateNext tree s (just n1 ) n0
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
89 ... | just parent1 with left parent1
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
90 ... | nothing = rotateNext tree s (just n1) nothing
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
91 ... | just leftParent with compTri (key n1) (key leftParent)
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
92 rotateRight1 {n} {m} {t} {a} tree s n0 parent rotateNext | just n1 | just parent1 | just leftParent | tri< a₁ ¬b ¬c = rotateNext tree s (just n1) parent
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
93 rotateRight1 {n} {m} {t} {a} tree s n0 parent rotateNext | just n1 | just parent1 | just leftParent | tri≈ ¬a b ¬c = rotateNext tree s (just n1) parent
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
94 rotateRight1 {n} {m} {t} {a} tree s n0 parent rotateNext | just n1 | just parent1 | just leftParent | tri> ¬a ¬b c = rotateNext tree s (just n1) parent
530
63f3df7f5448 add insertCase41, insertCase5
ryokka
parents: 529
diff changeset
95
519
0a723e418b2a add some more directives in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 518
diff changeset
96
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
97 rotateLeft : {n m : Level } {t : Set m } {a : Set n} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Maybe (Node a) → Maybe (Node a) →
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
98 (RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Maybe (Node a) → Maybe (Node a) → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
99 rotateLeft {n} {m} {t} {a} tree s n0 parent rotateNext = getSingleLinkedStack s (\ s n0 → rotateLeft1 tree s n0 parent rotateNext)
530
63f3df7f5448 add insertCase41, insertCase5
ryokka
parents: 529
diff changeset
100 where
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
101 rotateLeft1 : {n m : Level } {t : Set m } {a : Set n} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Maybe (Node a) → Maybe (Node a) →
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
102 (RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Maybe (Node a) → Maybe (Node a) → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
103 rotateLeft1 {n} {m} {t} {a} tree s n0 parent rotateNext with n0
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
104 ... | nothing = rotateNext tree s nothing n0
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
105 ... | just n1 with parent
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
106 ... | nothing = rotateNext tree s (just n1) nothing
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
107 ... | just parent1 with right parent1
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
108 ... | nothing = rotateNext tree s (just n1) nothing
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
109 ... | just rightParent with compTri (key n1) (key rightParent)
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
110 rotateLeft1 {n} {m} {t} {a} tree s n0 parent rotateNext | just n1 | just parent1 | just rightParent | tri< a₁ ¬b ¬c = rotateNext tree s (just n1) parent
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
111 rotateLeft1 {n} {m} {t} {a} tree s n0 parent rotateNext | just n1 | just parent1 | just rightParent | tri≈ ¬a b ¬c = rotateNext tree s (just n1) parent
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
112 rotateLeft1 {n} {m} {t} {a} tree s n0 parent rotateNext | just n1 | just parent1 | just rightParent | tri> ¬a ¬b c = rotateNext tree s (just n1) parent
519
0a723e418b2a add some more directives in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 518
diff changeset
113
530
63f3df7f5448 add insertCase41, insertCase5
ryokka
parents: 529
diff changeset
114 {-# TERMINATING #-}
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
115 insertCase5 : {n m : Level } {t : Set m } {a : Set n} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Maybe (Node a) → Node a → Node a → (RedBlackTree {n} {m} {t} a → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
116 insertCase5 {n} {m} {t} {a} tree s n0 parent grandParent next = pop2SingleLinkedStack s (\ s parent grandParent → insertCase51 tree s n0 parent grandParent next)
530
63f3df7f5448 add insertCase41, insertCase5
ryokka
parents: 529
diff changeset
117 where
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
118 insertCase51 : {n m : Level } {t : Set m } {a : Set n} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Maybe (Node a) → Maybe (Node a) → Maybe (Node a) → (RedBlackTree {n} {m} {t} a → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
119 insertCase51 {n} {m} {t} {a} tree s n0 parent grandParent next with n0
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
120 ... | nothing = next tree
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
121 ... | just n1 with parent | grandParent
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
122 ... | nothing | _ = next tree
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
123 ... | _ | nothing = next tree
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
124 ... | just parent1 | just grandParent1 with left parent1 | left grandParent1
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
125 ... | nothing | _ = next tree
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
126 ... | _ | nothing = next tree
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
127 ... | just leftParent1 | just leftGrandParent1
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
128 with compTri (key n1) (key leftParent1) | compTri (key leftParent1) (key leftGrandParent1)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
129 ... | tri≈ ¬a b ¬c | tri≈ ¬a1 b1 ¬c1 = rotateRight tree s n0 parent (\ tree s n0 parent → insertCase5 tree s n0 parent1 grandParent1 next)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
130 ... | _ | _ = rotateLeft tree s n0 parent (\ tree s n0 parent → insertCase5 tree s n0 parent1 grandParent1 next)
519
0a723e418b2a add some more directives in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 518
diff changeset
131
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
132 insertCase4 : {n m : Level } {t : Set m } {a : Set n} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Node a → Node a → Node a → (RedBlackTree {n} {m} {t} a → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
133 insertCase4 {n} {m} {t} {a} tree s n0 parent grandParent next
528
7719f40e1367 fix insertCase4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 527
diff changeset
134 with (right parent) | (left grandParent)
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
135 ... | nothing | _ = insertCase5 tree s (just n0) parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
136 ... | _ | nothing = insertCase5 tree s (just n0) parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
137 ... | just rightParent | just leftGrandParent with compTri (key n0) (key rightParent) | compTri (key parent) (key leftGrandParent) -- (key n0) (key rightParent) | (key parent) (key leftGrandParent)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
138 ... | tri≈ ¬a b ¬c | tri≈ ¬a1 b1 ¬c1 = popSingleLinkedStack s (\ s n1 → rotateLeft tree s (left n0) (just grandParent) (\ tree s n0 parent → insertCase5 tree s n0 rightParent grandParent next))
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
139 ... | _ | _ = insertCase41 tree s n0 parent grandParent next
530
63f3df7f5448 add insertCase41, insertCase5
ryokka
parents: 529
diff changeset
140 where
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
141 insertCase41 : {n m : Level } {t : Set m } {a : Set n} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Node a → Node a → Node a → (RedBlackTree {n} {m} {t} a → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
142 insertCase41 {n} {m} {t} {a} tree s n0 parent grandParent next
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
143 with (left parent) | (right grandParent)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
144 ... | nothing | _ = insertCase5 tree s (just n0) parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
145 ... | _ | nothing = insertCase5 tree s (just n0) parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
146 ... | just leftParent | just rightGrandParent with compTri (key n0) (key leftParent) | compTri (key parent) (key rightGrandParent)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
147 ... | tri≈ ¬a b ¬c | tri≈ ¬a1 b1 ¬c1 = popSingleLinkedStack s (\ s n1 → rotateRight tree s (right n0) (just grandParent) (\ tree s n0 parent → insertCase5 tree s n0 leftParent grandParent next))
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
148 ... | _ | _ = insertCase5 tree s (just n0) parent grandParent next
527
a829a367cf7d add insertCase4
ryokka
parents: 526
diff changeset
149
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
150 colorNode : {n : Level } {a : Set n} → Node a → Color → Node a
532
ccf98ed4a4f7 fix red black tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 531
diff changeset
151 colorNode old c = record old { color = c }
527
a829a367cf7d add insertCase4
ryokka
parents: 526
diff changeset
152
519
0a723e418b2a add some more directives in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 518
diff changeset
153 {-# TERMINATING #-}
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
154 insertNode : {n m : Level } {t : Set m } {a : Set n} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Node a → (RedBlackTree {n} {m} {t} a → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
155 insertNode {n} {m} {t} {a} tree s n0 next = get2SingleLinkedStack s (insertCase1 n0)
518
c9f90f573efe add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 515
diff changeset
156 where
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
157 insertCase1 : Node a → SingleLinkedStack (Node a) → Maybe (Node a) → Maybe (Node a) → t -- placed here to allow mutual recursion
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
158 insertCase3 : SingleLinkedStack (Node a) → Node a → Node a → Node a → t
518
c9f90f573efe add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 515
diff changeset
159 insertCase3 s n0 parent grandParent with left grandParent | right grandParent
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
160 ... | nothing | nothing = insertCase4 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
161 ... | nothing | just uncle = insertCase4 tree s n0 parent grandParent next
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
162 ... | just uncle | _ with compTri (key uncle ) (key parent )
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
163 insertCase3 s n0 parent grandParent | just uncle | _ | tri≈ ¬a b ¬c = insertCase4 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
164 insertCase3 s n0 parent grandParent | just uncle | _ | tri< a ¬b ¬c with color uncle
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
165 insertCase3 s n0 parent grandParent | just uncle | _ | tri< a ¬b ¬c | Red = pop2SingleLinkedStack s ( \s p0 p1 → insertCase1 (
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
166 record grandParent { color = Red ; left = just ( record parent { color = Black } ) ; right = just ( record uncle { color = Black } ) }) s p0 p1 )
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
167 insertCase3 s n0 parent grandParent | just uncle | _ | tri< a ¬b ¬c | Black = insertCase4 tree s n0 parent grandParent next
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
168 insertCase3 s n0 parent grandParent | just uncle | _ | tri> ¬a ¬b c with color uncle
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
169 insertCase3 s n0 parent grandParent | just uncle | _ | tri> ¬a ¬b c | Red = pop2SingleLinkedStack s ( \s p0 p1 → insertCase1 ( record grandParent { color = Red ; left = just ( record parent { color = Black } ) ; right = just ( record uncle { color = Black } ) }) s p0 p1 )
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
170 insertCase3 s n0 parent grandParent | just uncle | _ | tri> ¬a ¬b c | Black = insertCase4 tree s n0 parent grandParent next
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
171 insertCase2 : SingleLinkedStack (Node a) → Node a → Node a → Node a → t
518
c9f90f573efe add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 515
diff changeset
172 insertCase2 s n0 parent grandParent with color parent
538
5c001e8ba0d5 add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents: 537
diff changeset
173 ... | Black = replaceNode tree s n0 next
532
ccf98ed4a4f7 fix red black tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 531
diff changeset
174 ... | Red = insertCase3 s n0 parent grandParent
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
175 insertCase1 n0 s nothing nothing = next tree
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
176 insertCase1 n0 s nothing (just grandParent) = next tree
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
177 insertCase1 n0 s (just parent) nothing = replaceNode tree s (colorNode n0 Black) next
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
178 insertCase1 n0 s (just parent) (just grandParent) = insertCase2 s n0 parent grandParent
528
7719f40e1367 fix insertCase4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 527
diff changeset
179
531
f6060e1bf900 add clearStack
ryokka
parents: 530
diff changeset
180 ----
549
bc3208d510cd add some
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 545
diff changeset
181 -- find node potition to insert or to delete, the path will be in the stack
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
182 --
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
183 findNode : {n m : Level } {a : Set n} {t : Set m} → RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → (Node a) → (Node a) → (RedBlackTree {n} {m} {t} a → SingleLinkedStack (Node a) → Node a → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
184 findNode {n} {m} {a} {t} tree s n0 n1 next = pushSingleLinkedStack s n1 (\ s → findNode1 s n1)
570
a6aa2ff5fea4 separate clearStack
ryokka
parents: 564
diff changeset
185 module FindNode where
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
186 findNode2 : SingleLinkedStack (Node a) → (Maybe (Node a)) → t
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
187 findNode2 s nothing = next tree s n0
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
188 findNode2 s (just n) = findNode tree s n0 n next
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
189 findNode1 : SingleLinkedStack (Node a) → (Node a) → t
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
190 findNode1 s n1 with (compTri (key n0) (key n1))
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
191 findNode1 s n1 | tri< a ¬b ¬c = popSingleLinkedStack s ( \s _ → next tree s (record n1 {key = key n1 ; value = value n0 } ) )
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
192 findNode1 s n1 | tri≈ ¬a b ¬c = findNode2 s (right n1)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
193 findNode1 s n1 | tri> ¬a ¬b c = findNode2 s (left n1)
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
194 -- ... | EQ = popSingleLinkedStack s ( \s _ → next tree s (record n1 {ey =ey n1 ; value = value n0 } ) )
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
195 -- ... | GT = findNode2 s (right n1)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
196 -- ... | LT = findNode2 s (left n1)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
197
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
198
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
199
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
200
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
201 leafNode : {n : Level } { a : Set n } → a → ℕ → (Node a)
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
202 leafNode v k1 = record {key = k1 ; value = v ; right = nothing ; left = nothing ; color = Red }
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
203
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
204 putRedBlackTree : {n m : Level} {t : Set m} {a : Set n} → RedBlackTree {n} {m} {t} a → a → (key1 : ℕ) → (RedBlackTree {n} {m} {t} a → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
205 putRedBlackTree {n} {m} {t} {a} tree val1 key1 next with (root tree)
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
206 putRedBlackTree {n} {m} {t} {a} tree val1 key1 next | nothing = next (record tree {root = just (leafNode val1 key1 ) })
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
207 putRedBlackTree {n} {m} {t} {a} tree val1 key1 next | just n2 = clearSingleLinkedStack (nodeStack tree) (λ s → findNode tree s (leafNode val1 key1) n2 (λ tree1 s n1 → insertNode tree1 s n1 next))
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
208
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
209
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
210 -- getRedBlackTree : {n m : Level } {t : Set m} {a : Set n} {k : ℕ} → RedBlackTree {n} {m} {t} {A} a → → (RedBlackTree {n} {m} {t} {A} a → (Maybe (Node a)) → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
211 -- getRedBlackTree {_} {_} {t} {a} {k} tree1 cs = checkNode (root tree)
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
212 -- module GetRedBlackTree where -- http://agda.readthedocs.io/en/v2.5.2/language/let-and-where.html
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
213 -- search : Node a → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
214 -- checkNode : Maybe (Node a) → t
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
215 -- checkNode nothing = cs tree nothing
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
216 -- checkNode (just n) = search n
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
217 -- search n with compTri1 (key n)
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
218 -- search n | tri< a ¬b ¬c = checkNode (left n)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
219 -- search n | tri≈ ¬a b ¬c = cs tree (just n)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
220 -- search n | tri> ¬a ¬b c = checkNode (right n)
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
221
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
222
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
223
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
224 -- putUnblanceTree : {n m : Level } {a : Set n} {k : ℕ} {t : Set m} → RedBlackTree {n} {m} {t} {A} a → → a → (RedBlackTree {n} {m} {t} {A} a → t) → t
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
225 -- putUnblanceTree {n} {m} {A} {a} {k} {t} tree1 value next with (root tree)
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
226 -- ... | nothing = next (record tree {root = just (leafNode1 value) })
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
227 -- ... | just n2 = clearSingleLinkedStack (nodeStack tree) (λ s → findNode tree s (leafNode1 value) n2 (λ tree1 s n1 → replaceNode tree1 s n1 next))
515
54ff7a97aec1 fix findNode in Agda
ryokka
parents: 514
diff changeset
228
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
229 createEmptyRedBlackTreeℕ : {n m : Level} {t : Set m} (a : Set n)
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
230 → RedBlackTree {n} {m} {t} a
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
231 createEmptyRedBlackTreeℕ a = record {
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
232 root = nothing
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
233 ; nodeStack = emptySingleLinkedStack
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
234 }
564
40ab3d39e49d using strict total order
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 551
diff changeset
235
545
b180dc78abcf add someTest
ryokka
parents: 543
diff changeset
236
608
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
237 test : {m : Level} (t : Set) → RedBlackTree {Level.zero} {Level.zero} ℕ
8df36383ced0 fix RedBlackTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 586
diff changeset
238 test t = createEmptyRedBlackTreeℕ {Level.zero} {Level.zero} {t} ℕ
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
239
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents: 575
diff changeset
240 -- ts = createEmptyRedBlackTreeℕ {ℕ} {?} {!!} 0
575
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
241
73fc32092b64 push local rbtree
ryokka
parents: 570
diff changeset
242 -- tes = putRedBlackTree {_} {_} {_} (createEmptyRedBlackTreeℕ {_} {_} {_} 3 3) 2 2 (λ t → t)