Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
MCSP.Data.RadixTree
Description
A compressed trie of string radices.
Synopsis
- type RadixTree a = RadixTreeMap a (String a)
- data RadixTreeMap a v
- empty :: RadixTree a
- construct :: Ord a => [String a] -> RadixTree a
- member :: Ord a => String a -> RadixTree a -> Bool
- findMin :: RadixTree a -> Maybe (String a)
- findMax :: RadixTree a -> Maybe (String a)
- insert :: Ord a => String a -> RadixTree a -> RadixTree a
- union :: Ord a => RadixTree a -> RadixTree a -> RadixTree a
- delete :: Ord a => String a -> RadixTree a -> RadixTree a
Data Types
type RadixTree a = RadixTreeMap a (String a) Source #
A set of String
s represented by a radix tree.
This tree is implemented a RadixTreeMap
where each key is associated with itself, enabling
key construction in constant time (no need to concatenate radices) and some useful instances
(like Foldable
).
data RadixTreeMap a v Source #
A map of String
keys represented by a radix tree.
The tree structure uses the maximal substring possible to label edges so that each internal node
has at least two children. Even though a substrings are used, the trie property is maintained,
that is, a node has at most r
children, where r
is the number of possible values for a
.
Given this property, substring are stored using a sorted Map
which may improve performance.
Instances
Construction
construct :: Ord a => [String a] -> RadixTree a Source #
O(?) Build a radix tree from a list of strings.
>>>
construct ["abc", "def", "abb"]
Tree [ab :~> Tree [b :~> Tree (abb) [],c :~> Tree (abc) []],def :~> Tree (def) []]
Query
member :: Ord a => String a -> RadixTree a -> Bool Source #
O(n log r) Check if string is present in the tree.
>>>
member "ab" (construct ["abc", "def", "abb"])
False
>>>
member "abc" (construct ["abc", "def", "abb"])
True
findMin :: RadixTree a -> Maybe (String a) Source #
O(n log r) Extract the minimal string in the tree.
>>>
findMin (construct ["abc", "def", "abb"])
Just abb
>>>
import Prelude (Char)
>>>
findMin @Char empty
Nothing
findMax :: RadixTree a -> Maybe (String a) Source #
O(n log r) Extract the minimal string in the tree.
>>>
findMax (construct ["abc", "def", "abb"])
Just def
>>>
import Prelude (Char)
>>>
findMax @Char empty
Nothing
Modification
insert :: Ord a => String a -> RadixTree a -> RadixTree a Source #
O(?) Insert a string in a tree.
If the tree already contains a string equal to the given value, it is replaced with the new value.
>>>
insert "xyz" (construct ["abc"])
Tree [abc :~> Tree (abc) [],xyz :~> Tree (xyz) []]
>>>
insert "xyz" (construct ["abc", "xyz"])
Tree [abc :~> Tree (abc) [],xyz :~> Tree (xyz) []]
union :: Ord a => RadixTree a -> RadixTree a -> RadixTree a Source #
O(?) The union of two sets, preferring the first set when equal strings are encountered.
>>>
construct ["abc"] `union` construct ["def"]
Tree [abc :~> Tree (abc) [],def :~> Tree (def) []]
>>>
construct ["abc"] `union` construct []
Tree [abc :~> Tree (abc) []]
>>>
construct [] `union` construct ["abc"]
Tree [abc :~> Tree (abc) []]
>>>
construct ["abc"] `union` construct ["abc"]
Tree [abc :~> Tree (abc) []]