Copyright | (c) 2020 Arnau Abella Dmitrii Kovanikov |
---|---|
License | MIT (see the file LICENSE) |
Maintainer | Arnau Abella arnauabell@gmail.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
General description
Package rbst
implements a self-balancing-tree-like data structure called Randomized Binary Search Tree. This data structure behave exactly like a random_binary_search_tree, irrespectively of the input data distribution, with fast (logarithmic time complexity)
/ insert
/ delete
/ lookup
/ etc. operations.union
Package structure
This package contains the following modules:
- RBST.Pretty: pretty-printer for the tree.
Module RBST reexports ony RBST.Pretty and exports all RBST
functionalities.
Usage example
A balanced Tree
can be created from a list of keys/values:
>>>
import GHC.Exts (IsList (..))
>>>
let empty = empty :: RBST Int String
>>>
let single = one 1 'v'
>>>
let tree = fromList [("x", 2),("y", 3), ("z", 1)] :: RBST String Int
>>>
prettyPrint tree
("y",3) [3] ╱╲ ╱ ╲ ╱ ╲ ╱ ╲ ╱ ╲ ╱ ╲ ("x",2) [1] ("z",1) [1]
Each node shows:
- The key.
- The associated value.
- The size of the tree.
You can try it yourself:
> insert "w" 5 tree > delete "u" tree
>>>
lookup "y" tree
Just 3
>>>
lookup "w" tree
Nothing
>>>
compactPrint tree
("y",3) [3] | |-- ("z",1) [1] | \__ ("x",2) [1]
Implementation
The implementation of Randomized Binary Search Trees is based on:
- Conrado Martinez and Salvador Roura, "Randomized Binary Search Trees", January 1997, http://akira.ruc.dk/~keld/teaching/algoritmedesign_f08/Artikler/03/Martinez97.pdf.
Synopsis
- newtype Size = Size {}
- data Tree k a
- data RBST k a = RBST {}
- empty :: RBST k a
- one :: k -> a -> RBST k a
- size :: RBST k a -> Int
- height :: RBST k a -> Int
- lookup :: Ord k => k -> RBST k a -> Maybe a
- at :: Int -> RBST k a -> Maybe (k, a)
- insert :: Ord k => k -> a -> RBST k a -> RBST k a
- delete :: Ord k => k -> RBST k a -> RBST k a
- remove :: Int -> RBST k a -> RBST k a
- take :: Int -> RBST k a -> RBST k a
- drop :: Int -> RBST k a -> RBST k a
- union :: Ord k => RBST k a -> RBST k a -> RBST k a
- intersection :: Ord k => RBST k a -> RBST k a -> RBST k a
- subtraction :: Ord k => RBST k a -> RBST k a -> RBST k a
- difference :: Ord k => RBST k a -> RBST k a -> RBST k a
- module RBST.Pretty
Data structure & Instances
Size of the Tree
data structure.
Tree
data structure. The node contains the rank of the tree.
Instances
RBST
data structure.
Instances
Foldable (RBST k) Source # | |
Defined in RBST.Internal fold :: Monoid m => RBST k m -> m # foldMap :: Monoid m => (a -> m) -> RBST k a -> m # foldr :: (a -> b -> b) -> b -> RBST k a -> b # foldr' :: (a -> b -> b) -> b -> RBST k a -> b # foldl :: (b -> a -> b) -> b -> RBST k a -> b # foldl' :: (b -> a -> b) -> b -> RBST k a -> b # foldr1 :: (a -> a -> a) -> RBST k a -> a # foldl1 :: (a -> a -> a) -> RBST k a -> a # elem :: Eq a => a -> RBST k a -> Bool # maximum :: Ord a => RBST k a -> a # minimum :: Ord a => RBST k a -> a # | |
Ord k => IsList (RBST k a) Source # | Create a tree from a list of key/value pairs, and viceversa. NOTE: This requires -XOverloadedLists enabled. Functions have the following time complexity:
|
(Eq k, Eq a) => Eq (RBST k a) Source # | (==) is implemented via (==) of the underlying |
(Show k, Show a) => Show (RBST k a) Source # | |
Generic (RBST k a) Source # | |
Ord k => Semigroup (RBST k a) Source # | (<>) is implemented via Note: Unlawful instance. TODO: require Semigroup a and use |
Ord k => Monoid (RBST k a) Source # | mempty is implemented via |
(NFData k, NFData a) => NFData (RBST k a) Source # | |
Defined in RBST.Internal | |
type Rep (RBST k a) Source # | |
Defined in RBST.Internal type Rep (RBST k a) = D1 (MetaData "RBST" "RBST.Internal" "rbst-0.0.0.1-65lZlm8Xqg8CU5M7iQrOXY" False) (C1 (MetaCons "RBST" PrefixI True) (S1 (MetaSel (Just "rbstGen") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 PureMT) :*: S1 (MetaSel (Just "rbstTree") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 (Tree k a)))) | |
type Item (RBST k a) Source # | |
Defined in RBST.Internal |
Construction functions
Query functions
height :: RBST k a -> Int Source #
\( O(n) \). Height of the tree.
>>>
height (empty :: RBST Char Int)
-1
>>>
height (one 'x' 1)
0
>>>
height (one 'x' 1 <> one 'y' 2)
1
lookup :: Ord k => k -> RBST k a -> Maybe a Source #
\( O(\log \ n) \). Lookup the value at the key in the tree.
>>>
lookup 'A' (empty :: RBST Char Int)
Nothing
>>>
lookup 'A' (one 'A' 7)
Just 7
at :: Int -> RBST k a -> Maybe (k, a) Source #
\( O(\log \ n) \). Get the i-th element of the tree.
NOTE: \(0 \leq i \leq n\), where n is the size of the tree.
>>>
let tree = fromList [('a',1), ('b', 2), ('c',3)] :: RBST Char Int
>>>
at 0 tree
Just ('a',1)>>>
at 2 tree
Just ('c',3)
Modification functions
insert :: Ord k => k -> a -> RBST k a -> RBST k a Source #
\( O(\log \ n) \). Insert a new key/value pair in the tree.
If the key is already present in the map, the associated value is replaced with the supplied value.
> insertx
1 empty == onex
1
delete :: Ord k => k -> RBST k a -> RBST k a Source #
\( O(\log \ n) \). Delete a key and its value from the map. When the key is not a member of the map, the original map is returned.
> delete 1 (one (1, A)) == empty
remove :: Int -> RBST k a -> RBST k a Source #
\( O(\log \ n) \). Delete the i-th element of the tree.
NOTE: \(0 \leq i \leq n\), where n is the size of the tree.
>>>
let tree = fromList [('a',1), ('b', 2), ('c',3)] :: RBST Char Int
>>>
toList $ remove 0 tree
[('b',2),('c',3)]
take :: Int -> RBST k a -> RBST k a Source #
\( O(\log n) \). Returns the first i
-th elements of the given tree t
of size n
.
Note:
- If \( i \leq 0 \), then the result is
empty
. - If \( i \geq n \), then the result is
t
.
drop :: Int -> RBST k a -> RBST k a Source #
\( O(\log n) \). Returns the tree t
without the first i
-th elements.
Note:
- If \( i \leq 0 \), then the result is
t
. - If \( i \geq n \), then the result is
empty
.
Set operations
union :: Ord k => RBST k a -> RBST k a -> RBST k a Source #
\( \theta(m + n) \). Union of two RBST
.
In case of duplication, only one key remains by a random choice.
intersection :: Ord k => RBST k a -> RBST k a -> RBST k a Source #
\( \theta(m + n) \). Intersection of two RBST
.
subtraction :: Ord k => RBST k a -> RBST k a -> RBST k a Source #
\( \theta(m + n) \). Difference (subtraction) of two RBST
.
difference :: Ord k => RBST k a -> RBST k a -> RBST k a Source #
\( \theta(m + n) \). Difference (disjunctive union) of two RBST
.
Reexports
module RBST.Pretty