Debian Codegraph @

src/Data/Tree/AVL.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- AVL trees
5 ------------------------------------------------------------------------
6
7 -- AVL trees are balanced binary search trees.
8
9 -- The search tree invariant is specified using the technique
10 -- described by Conor McBride in his talk "Pivotal pragmatism".
11
12 -- See README.Data.Tree.AVL for examples of how to use AVL trees.
13
14 {-# OPTIONS --cubical-compatible --safe #-}
15
16 open import Relation.Binary.Bundles using (StrictTotalOrder)
17
18 module Data.Tree.AVL
19 {a ℓ₁ ℓ₂} (strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂)
20 where
21
22 open import Data.Bool.Base using (Bool)
23 import Data.DifferenceList as DiffList
24 open import Data.List.Base as List using (List; []; _∷_)
25 open import Data.Maybe.Base using (Maybe; nothing; just; is-just)
26 open import Data.Nat.Base using (ℕ; suc)
27 open import Data.Product.Base hiding (map)
28 open import Function.Base as F
29 open import Level using (Level; _⊔_)
30 open import Relation.Unary using (IUniversal; _⇒_)
31
32 private
33 variable
34 l : Level
35 A : Set l
36
37 open StrictTotalOrder strictTotalOrder renaming (Carrier to Key)
38 import Data.Tree.AVL.Indexed strictTotalOrder as Indexed
39 open Indexed using (⊥⁺; ⊤⁺; ⊥⁺<⊤⁺; ⊥⁺<[_]<⊤⁺; ⊥⁺<[_]; [_]<⊤⁺)
40
41 ------------------------------------------------------------------------
42 -- Re-export some core definitions publically
43
44 open Indexed using (K&_;_,_; toPair; fromPair; Value; MkValue; const) public
45
46 ------------------------------------------------------------------------
47 -- Types and functions with hidden indices
48
49 data Tree {v} (V : Value v) : Set (a ⊔ v ⊔ ℓ₂) where
50 tree : ∀ {h} → Indexed.Tree V ⊥⁺ ⊤⁺ h → Tree V
51
52 module _ {v} {V : Value v} where
53
54 private
55 Val = Value.family V
56
57 empty : Tree V
58 empty = tree $′ Indexed.empty ⊥⁺<⊤⁺
59
60 singleton : (k : Key) → Val k → Tree V
61 singleton k v = tree (Indexed.singleton k v ⊥⁺<[ k ]<⊤⁺)
62
63 insert : (k : Key) → Val k → Tree V → Tree V
64 insert k v (tree t) = tree $′ proj₂ $ Indexed.insert k v t ⊥⁺<[ k ]<⊤⁺
65
66 insertWith : (k : Key) → (Maybe (Val k) → Val k) →
67 Tree V → Tree V
68 insertWith k f (tree t) = tree $′ proj₂ $ Indexed.insertWith k f t ⊥⁺<[ k ]<⊤⁺
69
70 delete : Key → Tree V → Tree V
71 delete k (tree t) = tree $′ proj₂ $ Indexed.delete k t ⊥⁺<[ k ]<⊤⁺
72
73 lookup : Tree V → (k : Key) → Maybe (Val k)
74 lookup (tree t) k = Indexed.lookup t k ⊥⁺<[ k ]<⊤⁺
75
76 module _ {v w} {V : Value v} {W : Value w} where
77
78 private
79 Val = Value.family V
80 Wal = Value.family W
81
82 map : ∀[ Val ⇒ Wal ] → Tree V → Tree W
83 map f (tree t) = tree $ Indexed.map f t
84
85 module _ {v} {V : Value v} where
86
87 private
88 Val = Value.family V
89
90 member : Key → Tree V → Bool
91 member k t = is-just (lookup t k)
92
93 headTail : Tree V → Maybe (K& V × Tree V)
94 headTail (tree (Indexed.leaf _)) = nothing
95 headTail (tree {h = suc _} t) with (k , _ , _ , t′) ← Indexed.headTail t
96 = just (k , tree (Indexed.castˡ ⊥⁺<[ _ ] t′))
97
98 initLast : Tree V → Maybe (Tree V × K& V)
99 initLast (tree (Indexed.leaf _)) = nothing
100 initLast (tree {h = suc _} t) with (k , _ , _ , t′) ← Indexed.initLast t
101 = just (tree (Indexed.castʳ t′ [ _ ]<⊤⁺) , k)
102
103 foldr : (∀ {k} → Val k → A → A) → A → Tree V → A
104 foldr cons nil (tree t) = Indexed.foldr cons nil t
105
106 -- The input does not need to be ordered.
107
108 fromList : List (K& V) → Tree V
109 fromList = List.foldr (uncurry insert ∘′ toPair) empty
110
111 -- Returns an ordered list.
112
113 toList : Tree V → List (K& V)
114 toList (tree t) = DiffList.toList (Indexed.toDiffList t)
115
116 size : Tree V → ℕ
117 size (tree t) = Indexed.size t
118
119 ------------------------------------------------------------------------
120 -- Naive implementation of union
121
122 module _ {v w} {V : Value v} {W : Value w} where
123 private
124 Val = Value.family V
125 Wal = Value.family W
126
127 unionWith : (∀ {k} → Val k → Maybe (Wal k) → Wal k) →
128 -- left → right → result.
129 Tree V → Tree W → Tree W
130 unionWith f t₁ t₂ = foldr (λ {k} v → insertWith k (f v)) t₂ t₁
131
132 module _ {v} {V : Value v} where
133
134 private Val = Value.family V
135
136 -- Left-biased.
137 union : Tree V → Tree V → Tree V
138 union = unionWith F.const
139
140 unionsWith : (∀ {k} → Val k → Maybe (Val k) → Val k) → List (Tree V) → Tree V
141 unionsWith f ts = List.foldr (unionWith f) empty ts
142
143 -- Left-biased.
144 unions : List (Tree V) → Tree V
145 unions = unionsWith F.const
146
147 ------------------------------------------------------------------------
148 -- Naive implementation of intersection
149
150 module _ {v w x} {V : Value v} {W : Value w} {X : Value x} where
151 private
152 Val = Value.family V
153 Wal = Value.family W
154 Xal = Value.family X
155
156 intersectionWith : (∀ {k} → Val k → Wal k → Xal k) →
157 Tree V → Tree W → Tree X
158 intersectionWith f t₁ t₂ = foldr cons empty t₁ where
159
160 cons : ∀ {k} → Val k → Tree X → Tree X
161 cons {k} v = case lookup t₂ k of λ where
162 nothing → id
163 (just w) → insert k (f v w)
164
165 module _ {v} {V : Value v} where
166 private
167 Val = Value.family V
168
169 -- Left-biased.
170 intersection : Tree V → Tree V → Tree V
171 intersection = intersectionWith F.const
172
173 intersectionsWith : (∀ {k} → Val k → Val k → Val k) →
174 List (Tree V) → Tree V
175 intersectionsWith f [] = empty
176 intersectionsWith f (t ∷ ts) = List.foldl (intersectionWith f) t ts
177 -- We are using foldl so that we are indeed forming t₁ ∩ ⋯ ∩ tₙ for
178 -- the input list [t₁,⋯,tₙ]. If we were to use foldr, we would form
179 -- t₂ ∩ ⋯ ∩ tₙ ∩ t₁ instead!
180
181 -- Left-biased.
182 intersections : List (Tree V) → Tree V
183 intersections = intersectionsWith F.const
184
185
186 ------------------------------------------------------------------------
187 -- DEPRECATED
188 ------------------------------------------------------------------------
189 -- Please use the new names as continuing support for the old names is
190 -- not guaranteed.
191
192 -- Version 2.0
193
194 infixl 4 _∈?_
195 _∈?_ : ∀ {v} {V : Value v} → Key → Tree V → Bool
196 _∈?_ = member
197 {-# WARNING_ON_USAGE _∈?_
198 "Warning: _∈?_ was deprecated in v2.0.
199 Please use member instead."
200 #-}