Copyright | (c) Highlander Paiva 2022 |
---|---|
License | BSD-style (see the LICENSE file) |
Maintainer | hvpaiva@icloud.com |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
The BTree type, and associated operations.
This module allows you to use a BTree to store a set of values, and to efficiently lookup values in the set. The BTree is a balanced tree, which means that the tree is more efficient than a conventional binary tree.
Most of BTree operations are performed in O(log n) time.
Synopsis
- data BTree a
- empty :: BTree a
- singleton :: Ord a => a -> BTree a
- fromList :: Ord a => [a] -> BTree a
- insert :: forall a. Ord a => a -> BTree a -> BTree a
- delete :: forall a. Ord a => a -> BTree a -> BTree a
- search :: forall a. Ord a => a -> BTree a -> Maybe a
- height :: forall a. BTree a -> Int
- levels :: forall a. Ord a => BTree a -> [[a]]
- preorder :: forall a. BTree a -> [a]
- inorder :: forall a. BTree a -> [a]
- postorder :: forall a. BTree a -> [a]
- draw :: forall a. Show a => BTree a -> String
BTree type
The BTree
type represents a tree using the b-tree algorithm.
A
is a self-balancing tree as its nodes are traversed in-order. The
node is a set of elements pointing to its children, and a leaf has no children and nothing in itself.BTree
a
This implementation uses a order 3 BTree
, this means:
- {one, two} elements per node and {two, three} subtrees.
- A leaf contains nothing.
- Every leaf is equidistant from the root.
- Subtrees must have the same height.
- Data are ordered left to right.
4 <-- (3) root / \ 2 6 <-- (2) internal nodes / \ / \ 1 3 5 7 <-- (1) internal nodes / \ / \ / \ / \ . .. .. .. . <-- (0) leafs
The complexity of the operations:
Algorithm | Medium Case | Worst Case |
---|---|---|
Search | O(log n) | O(log n) |
Insert | O(log n) | O(log n) |
Delete | O(log n) | O(log n) |
Examples
A
may be represented as:BTree
Int
>>>
let t = fromList [1,2,3,4,5,6,7]
>>>
t
"(((. 1 .) 2 (. 3 .)) 4 ((. 5 .) 6 (. 7 .)))"
>>>
let n = insert 8 t
>>>
n
"(((. 1 .) 2 (. 3 .)) 4 ((. 5 .) 6 (. 7 . 8 .)))"
Since: 1.0.0
Instances
Foldable BTree Source # | |
Defined in Data.BTree fold :: Monoid m => BTree m -> m # foldMap :: Monoid m => (a -> m) -> BTree a -> m # foldMap' :: Monoid m => (a -> m) -> BTree a -> m # foldr :: (a -> b -> b) -> b -> BTree a -> b # foldr' :: (a -> b -> b) -> b -> BTree a -> b # foldl :: (b -> a -> b) -> b -> BTree a -> b # foldl' :: (b -> a -> b) -> b -> BTree a -> b # foldr1 :: (a -> a -> a) -> BTree a -> a # foldl1 :: (a -> a -> a) -> BTree a -> a # elem :: Eq a => a -> BTree a -> Bool # maximum :: Ord a => BTree a -> a # minimum :: Ord a => BTree a -> a # | |
Traversable BTree Source # | The default
|
Functor BTree Source # | |
Show a => Show (BTree a) Source # | |
Constructors
singleton :: Ord a => a -> BTree a Source #
Creates a singleton BTree
from a single element.
>>>
let t = singleton 1
>>>
t
"(. 1 .)"
Since: 1.0.0
fromList :: Ord a => [a] -> BTree a Source #
Creates a BTree
from a list.
>>>
let t = fromList [1,2,3,4,5,6,7]
>>>
t
"(((. 1 .) 2 (. 3 .)) 4 ((. 5 .) 6 (. 7 .)))"
Since: 1.0.0
Operations
delete :: forall a. Ord a => a -> BTree a -> BTree a Source #
Delete an element from a BTree
.
Algorithm | Medium Case | Worst Case |
---|---|---|
Delete | O(log n) | O(log n) |
>>>
let t = fromList [1,2,3,4,5,6,7]
>>>
let n = delete 3 t
>>>
n
"((. 1 . 2 .) 4 (. 5 .) 6 (. 7 .))"
Since: 1.0.0
levels :: forall a. Ord a => BTree a -> [[a]] Source #
Return the BTree
as a list of lists, grouped by hights.
It will start by the root level and then go down to the branches.
a / \ => [[a], [b,c]] b c
>>>
levels (fromList [1,2,3,4,5,6,7])
[[4],[2,6],[1,3,5,7]]
Since: 1.0.0
Traversals to list
preorder :: forall a. BTree a -> [a] Source #
Return the BTree
as a list in a pre-order traversal.
The pre-order traversal runs through the elements of the BTree
in the following order:
- The root element.
- The left subtree.
- The right subtree.
A BTree
of [1, 2, 3, 4, 5, 6, 7]
is represented as:
4 / \ 2 6 / \ / \ 1 3 5 7 / \ / \ / \ / \ . .. .. .. .
>>>
preorder (fromList [1,2,3,4,5,6,7])
[4,2,1,3,6,5,7]
Since: 1.0.0
inorder :: forall a. BTree a -> [a] Source #
Return the BTree
as a list in a in-order traversal.
The in-order traversal runs through the elements of the BTree
in the following order:
- The left subtree.
- The root element.
- The right subtree.
Note:
The in-order traversal is the default traversal for the BTree
, and is used to return as list.
A BTree
of [1, 2, 3, 4, 5, 6, 7]
is represented as:
4 / \ 2 6 / \ / \ 1 3 5 7 / \ / \ / \ / \ . .. .. .. .
>>>
inorder (fromList [1,2,3,4,5,6,7])
[1,2,3,4,5,6,7]
Since: 1.0.0
postorder :: forall a. BTree a -> [a] Source #
Return the BTree
as a list in a post-order traversal.
The post-order traversal runs through the elements of the BTree
in the following order:
- The left subtree.
- The right subtree.
- The root element.
A BTree
of [1, 2, 3, 4, 5, 6, 7]
is represented as:
4 / \ 2 6 / \ / \ 1 3 5 7 / \ / \ / \ / \ . .. .. .. .
>>>
postorder (fromList [1,2,3,4,5,6,7])
[1,3,2,5,7,6,4]
Since: 1.0.0
Drawing
draw :: forall a. Show a => BTree a -> String Source #
Draws the BTree
.
The output is a string of the form:
- Each Leaf is represented by a ' . '
- Each Node is represented as (left, value, right) where left and right are the left and right subtrees.
So, the current list [1, 2, 3, 4, 5, 6, 7]
will be represented as:
"(((. 1 .) 2 (. 3 .)) 4 ((. 5 .) 6 (. 7 .)))"
Which is an inline representation of the BTree
.
Examples
>>>
draw (fromList [1,2,3,4,5,6,7])
"(((. 1 .) 2 (. 3 .)) 4 ((. 5 .) 6 (. 7 .)))"
Since: 1.0.0