------------------------------------------------------------------------ -- The Agda standard library -- -- AVL trees where the stored values may depend on their key ------------------------------------------------------------------------ {-# OPTIONS --cubical-compatible --safe #-} open import Relation.Binary.Bundles using (StrictTotalOrder) module Data.Tree.AVL.Indexed {a ℓ₁ ℓ₂} (strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂) where open import Level using (Level; _⊔_) open import Data.Nat.Base using (ℕ; zero; suc; _+_) open import Data.Product.Base using (Σ; ∃; _×_; _,_; proj₁) open import Data.Maybe.Base using (Maybe; just; nothing) open import Data.List.Base as List using (List) open import Data.DifferenceList using (DiffList; []; _∷_; _++_) open import Function.Base as F hiding (const) open import Relation.Unary open import Relation.Binary.Definitions using (_Respects_; Tri; tri<; tri≈; tri>) open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) private variable l v : Level A : Set l open StrictTotalOrder strictTotalOrder renaming (Carrier to Key) ------------------------------------------------------------------------ -- Re-export core definitions publicly open import Data.Tree.AVL.Key strictTotalOrder public open import Data.Tree.AVL.Value Eq.setoid public open import Data.Tree.AVL.Height public ------------------------------------------------------------------------ -- Definitions of the tree -- The trees have three parameters/indices: a lower bound on the -- keys, an upper bound, and a height. -- -- (The bal argument is the balance factor.) data Tree {v} (V : Value v) (l u : Key⁺) : ℕ → Set (a ⊔ v ⊔ ℓ₂) where leaf : (l _ _ k′ _ _ k′>k = joinˡ⁻ _ p (delete k lp (lk ]ᴿ)) pu bal ... | tri≈ _ k′≡k _ = join lp pu bal -- Looks up a key. Logarithmic in the size of the tree (assuming -- constant-time comparisons). lookup : ∀ {l u h} → Tree V l u h → (k : Key) → l < k < u → Maybe (Val k) lookup (leaf _) k l _ _ k′>k = lookup lk′ k (lk ]ᴿ) ... | tri≈ _ k′≡k _ = just (V≈ k′≡k v) -- Converts the tree to an ordered list. Linear in the size of the -- tree. foldr : ∀ {l u h} → (∀ {k} → Val k → A → A) → A → Tree V l u h → A foldr cons nil (leaf l