Copyright | (C) Frank Staals |
---|---|
License | see the LICENSE file |
Maintainer | Frank Staals |
Safe Haskell | None |
Language | GHC2021 |
Several types of Binary trees.
Synopsis
- data BinLeafTree v a
- = Leaf !a
- | Node (BinLeafTree v a) !v (BinLeafTree v a)
- node :: (Measured f a, Semigroup (f a)) => BinLeafTree (f a) a -> BinLeafTree (f a) a -> BinLeafTree (f a) a
- asBalancedBinLeafTree :: Foldable1 f => f a -> BinLeafTree (Count a) a
- foldUp :: (b -> v -> b -> b) -> (a -> b) -> BinLeafTree v a -> b
- foldUpData :: (w -> v -> w -> w) -> (a -> w) -> BinLeafTree v a -> BinLeafTree w a
- zipExactWith :: (u -> v -> w) -> (a -> b -> c) -> BinLeafTree u a -> BinLeafTree v b -> BinLeafTree w c
- toRoseTree :: BinLeafTree v a -> Tree (TreeNode v a)
- drawTree :: (Show v, Show a) => BinLeafTree v a -> String
- data BinaryTree a
- = Nil
- | Internal (BinaryTree a) !a (BinaryTree a)
- asBalancedBinTree :: Foldable f => f a -> BinaryTree a
- access :: BinaryTree a -> Maybe a
- foldBinaryUp :: b -> (a -> b -> b -> b) -> BinaryTree a -> BinaryTree (a, b)
- toRoseTree' :: BinaryTree a -> Maybe (Tree a)
- drawTree' :: Show a => BinaryTree a -> String
Documentation
data BinLeafTree v a Source #
Binary tree that stores its values (of type a) in the leaves. Internal nodes store something of type v.
Leaf !a | |
Node (BinLeafTree v a) !v (BinLeafTree v a) |
Instances
node :: (Measured f a, Semigroup (f a)) => BinLeafTree (f a) a -> BinLeafTree (f a) a -> BinLeafTree (f a) a Source #
smart constructor
asBalancedBinLeafTree :: Foldable1 f => f a -> BinLeafTree (Count a) a Source #
Create a balanced tree, i.e. a tree of height \(O(\log n)\) with the elements in the leaves.
\(O(n)\) time.
foldUp :: (b -> v -> b -> b) -> (a -> b) -> BinLeafTree v a -> b Source #
Given a function to combine internal nodes into b's and leafs into b's, traverse the tree bottom up, and combine everything into one b.
foldUpData :: (w -> v -> w -> w) -> (a -> w) -> BinLeafTree v a -> BinLeafTree w a Source #
Traverses the tree bottom up, recomputing the assocated values.
zipExactWith :: (u -> v -> w) -> (a -> b -> c) -> BinLeafTree u a -> BinLeafTree v b -> BinLeafTree w c Source #
Takes two trees, that have the same structure, and uses the provided functions to "zip" them together
toRoseTree :: BinLeafTree v a -> Tree (TreeNode v a) Source #
\( O(n) \) Convert binary tree to a rose tree, aka Tree
.
drawTree :: (Show v, Show a) => BinLeafTree v a -> String Source #
2-dimensional ASCII drawing of a tree.
data BinaryTree a Source #
Binary tree in which we store the values of type a in internal nodes.
Nil | |
Internal (BinaryTree a) !a (BinaryTree a) |
Instances
Functor BinaryTree Source # | |||||
Defined in HGeometry.Tree.Binary.Static fmap :: (a -> b) -> BinaryTree a -> BinaryTree b # (<$) :: a -> BinaryTree b -> BinaryTree a # | |||||
Foldable BinaryTree Source # | |||||
Defined in HGeometry.Tree.Binary.Static fold :: Monoid m => BinaryTree m -> m # foldMap :: Monoid m => (a -> m) -> BinaryTree a -> m # foldMap' :: Monoid m => (a -> m) -> BinaryTree a -> m # foldr :: (a -> b -> b) -> b -> BinaryTree a -> b # foldr' :: (a -> b -> b) -> b -> BinaryTree a -> b # foldl :: (b -> a -> b) -> b -> BinaryTree a -> b # foldl' :: (b -> a -> b) -> b -> BinaryTree a -> b # foldr1 :: (a -> a -> a) -> BinaryTree a -> a # foldl1 :: (a -> a -> a) -> BinaryTree a -> a # toList :: BinaryTree a -> [a] # null :: BinaryTree a -> Bool # length :: BinaryTree a -> Int # elem :: Eq a => a -> BinaryTree a -> Bool # maximum :: Ord a => BinaryTree a -> a # minimum :: Ord a => BinaryTree a -> a # sum :: Num a => BinaryTree a -> a # product :: Num a => BinaryTree a -> a # | |||||
Traversable BinaryTree Source # | |||||
Defined in HGeometry.Tree.Binary.Static traverse :: Applicative f => (a -> f b) -> BinaryTree a -> f (BinaryTree b) # sequenceA :: Applicative f => BinaryTree (f a) -> f (BinaryTree a) # mapM :: Monad m => (a -> m b) -> BinaryTree a -> m (BinaryTree b) # sequence :: Monad m => BinaryTree (m a) -> m (BinaryTree a) # | |||||
NFData a => NFData (BinaryTree a) Source # | |||||
Defined in HGeometry.Tree.Binary.Static rnf :: BinaryTree a -> () # | |||||
Generic (BinaryTree a) Source # | |||||
Defined in HGeometry.Tree.Binary.Static
from :: BinaryTree a -> Rep (BinaryTree a) x # to :: Rep (BinaryTree a) x -> BinaryTree a # | |||||
Read a => Read (BinaryTree a) Source # | |||||
Defined in HGeometry.Tree.Binary.Static readsPrec :: Int -> ReadS (BinaryTree a) # readList :: ReadS [BinaryTree a] # readPrec :: ReadPrec (BinaryTree a) # readListPrec :: ReadPrec [BinaryTree a] # | |||||
Show a => Show (BinaryTree a) Source # | |||||
Defined in HGeometry.Tree.Binary.Static showsPrec :: Int -> BinaryTree a -> ShowS # show :: BinaryTree a -> String # showList :: [BinaryTree a] -> ShowS # | |||||
Eq a => Eq (BinaryTree a) Source # | |||||
Defined in HGeometry.Tree.Binary.Static (==) :: BinaryTree a -> BinaryTree a -> Bool # (/=) :: BinaryTree a -> BinaryTree a -> Bool # | |||||
Ord a => Ord (BinaryTree a) Source # | |||||
Defined in HGeometry.Tree.Binary.Static compare :: BinaryTree a -> BinaryTree a -> Ordering # (<) :: BinaryTree a -> BinaryTree a -> Bool # (<=) :: BinaryTree a -> BinaryTree a -> Bool # (>) :: BinaryTree a -> BinaryTree a -> Bool # (>=) :: BinaryTree a -> BinaryTree a -> Bool # max :: BinaryTree a -> BinaryTree a -> BinaryTree a # min :: BinaryTree a -> BinaryTree a -> BinaryTree a # | |||||
type Rep (BinaryTree a) Source # | |||||
Defined in HGeometry.Tree.Binary.Static type Rep (BinaryTree a) = D1 ('MetaData "BinaryTree" "HGeometry.Tree.Binary.Static" "hgeometry-combinatorial-1.0.0.0-inplace" 'False) (C1 ('MetaCons "Nil" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Internal" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (BinaryTree a)) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (BinaryTree a))))) |
asBalancedBinTree :: Foldable f => f a -> BinaryTree a Source #
Create a balanced binary tree.
running time: \(O(n)\)
access :: BinaryTree a -> Maybe a Source #
Get the element stored at the root, if it exists
foldBinaryUp :: b -> (a -> b -> b -> b) -> BinaryTree a -> BinaryTree (a, b) Source #
Fold function for folding over a binary tree.
toRoseTree' :: BinaryTree a -> Maybe (Tree a) Source #
Convert a BinaryTree
into a RoseTree