Debian Codegraph @

src/Data/Tree/AVL/NonEmpty.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Non-empty 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 {-# OPTIONS --cubical-compatible --safe #-}
13
14 open import Relation.Binary.Bundles using (StrictTotalOrder)
15
16 module Data.Tree.AVL.NonEmpty
17 {a ℓ₁ ℓ₂} (strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂) where
18
19 open import Data.Bool.Base using (Bool)
20 open import Data.List.NonEmpty as List⁺ using (List⁺; _∷_; _++⁺_)
21 open import Data.Maybe.Base hiding (map)
22 open import Data.Nat.Base hiding (_<_; _⊔_; compare)
23 open import Data.Product.Base hiding (map)
24 open import Function.Base using (_$_; _∘′_)
25 open import Level using (_⊔_)
26 open import Relation.Unary using (IUniversal; _⇒_)
27
28 open StrictTotalOrder strictTotalOrder renaming (Carrier to Key)
29 open import Data.Tree.AVL.Value Eq.setoid
30 import Data.Tree.AVL.Indexed strictTotalOrder as Indexed
31 open Indexed using (⊥⁺; ⊤⁺; ⊥⁺<⊤⁺; ⊥⁺<[_]<⊤⁺; ⊥⁺<[_]; [_]<⊤⁺; node; toList)
32
33 ------------------------------------------------------------------------
34 -- Types and functions with hidden indices
35
36 -- NB: the height is non-zero thus guaranteeing that the AVL tree
37 -- contains at least one value.
38
39 data Tree⁺ {v} (V : Value v) : Set (a ⊔ v ⊔ ℓ₂) where
40 tree : ∀ {h} → Indexed.Tree V ⊥⁺ ⊤⁺ (suc h) → Tree⁺ V
41
42 module _ {v} {V : Value v} where
43
44 private
45 Val = Value.family V
46
47 singleton : (k : Key) → Val k → Tree⁺ V
48 singleton k v = tree (Indexed.singleton k v ⊥⁺<[ k ]<⊤⁺)
49
50 insert : (k : Key) → Val k → Tree⁺ V → Tree⁺ V
51 insert k v (tree t) with Indexed.insert k v t ⊥⁺<[ k ]<⊤⁺
52 ... | Indexed.0# , t′ = tree t′
53 ... | Indexed.1# , t′ = tree t′
54
55 insertWith : (k : Key) → (Maybe (Val k) → Val k) → Tree⁺ V → Tree⁺ V
56 insertWith k f (tree t) with Indexed.insertWith k f t ⊥⁺<[ k ]<⊤⁺
57 ... | Indexed.0# , t′ = tree t′
58 ... | Indexed.1# , t′ = tree t′
59
60 delete : Key → Tree⁺ V → Maybe (Tree⁺ V)
61 delete k (tree {h} t) with Indexed.delete k t ⊥⁺<[ k ]<⊤⁺
62 delete k (tree {h} t) | Indexed.1# , t′ = just (tree t′)
63 delete k (tree {0} t) | Indexed.0# , t′ = nothing
64 delete k (tree {suc h} t) | Indexed.0# , t′ = just (tree t′)
65
66 lookup : Tree⁺ V → (k : Key) → Maybe (Val k)
67 lookup (tree t) k = Indexed.lookup t k ⊥⁺<[ k ]<⊤⁺
68
69 module _ {v w} {V : Value v} {W : Value w} where
70
71 private
72 Val = Value.family V
73 Wal = Value.family W
74
75 map : ∀[ Val ⇒ Wal ] → Tree⁺ V → Tree⁺ W
76 map f (tree t) = tree $ Indexed.map f t
77
78 module _ {v} {V : Value v} where
79
80 -- The input does not need to be ordered.
81
82 fromList⁺ : List⁺ (K& V) → Tree⁺ V
83 fromList⁺ = List⁺.foldr (uncurry insert ∘′ toPair) (uncurry singleton ∘′ toPair)
84
85 -- The output is ordered
86
87 toList⁺ : Tree⁺ V → List⁺ (K& V)
88 toList⁺ (tree (node k&v l r bal)) = toList l ++⁺ k&v ∷ toList r