|
|
|
@@ -8,6 +8,13 @@ For example, in the tree
|
|
|
|
|
the nodes in the trunk are [a,b] (note that d is not the first child of b).
|
|
|
|
|
-}
|
|
|
|
|
module Dodge.Layout.Tree.Polymorphic
|
|
|
|
|
( applyToRoot
|
|
|
|
|
, treeFromPost
|
|
|
|
|
, treeFromTrunk
|
|
|
|
|
, splitTrunk
|
|
|
|
|
, applyToRandomNode
|
|
|
|
|
, addToTrunk
|
|
|
|
|
)
|
|
|
|
|
where
|
|
|
|
|
import Dodge.RandomHelp
|
|
|
|
|
|
|
|
|
@@ -40,9 +47,10 @@ Applies a function to the root of a tree.
|
|
|
|
|
applyToRoot :: (a -> a) -> Tree a -> Tree a
|
|
|
|
|
applyToRoot f (Node t ts) = Node (f t) ts
|
|
|
|
|
|
|
|
|
|
-- | Consider defining this using generalised recursion patterns
|
|
|
|
|
treeSize :: Tree a -> Int
|
|
|
|
|
treeSize = length . flatten
|
|
|
|
|
-- find use for?
|
|
|
|
|
---- | Consider defining this using generalised recursion patterns
|
|
|
|
|
--treeSize :: Tree a -> Int
|
|
|
|
|
--treeSize = length . flatten
|
|
|
|
|
|
|
|
|
|
{- |
|
|
|
|
|
Applies a function to a specific node determined by a list of indices.
|
|
|
|
@@ -54,17 +62,19 @@ applyToNode (i:is) f (Node x xs) = Node x (ys ++ [applyToNode is f z] ++ zs)
|
|
|
|
|
where
|
|
|
|
|
(ys, z:zs) = splitAt i xs
|
|
|
|
|
|
|
|
|
|
{- |
|
|
|
|
|
Applies a function to the first node along a trunk that satisfies a given property.
|
|
|
|
|
-}
|
|
|
|
|
applyToSubTrunkBy :: (a -> Bool) -> (Tree a -> Tree a) -> Tree a -> Tree a
|
|
|
|
|
applyToSubTrunkBy cond f (Node x (t:ts))
|
|
|
|
|
| cond x = f (Node x (t:ts))
|
|
|
|
|
| otherwise = Node x (applyToSubTrunkBy cond f t : ts)
|
|
|
|
|
applyToSubTrunkBy _ _ t = t
|
|
|
|
|
-- do not delete: find use for
|
|
|
|
|
--{- |
|
|
|
|
|
--Applies a function to the first node along a trunk that satisfies a given property.
|
|
|
|
|
---}
|
|
|
|
|
--applyToSubTrunkBy :: (a -> Bool) -> (Tree a -> Tree a) -> Tree a -> Tree a
|
|
|
|
|
--applyToSubTrunkBy cond f (Node x (t:ts))
|
|
|
|
|
-- | cond x = f (Node x (t:ts))
|
|
|
|
|
-- | otherwise = Node x (applyToSubTrunkBy cond f t : ts)
|
|
|
|
|
--applyToSubTrunkBy _ _ t = t
|
|
|
|
|
|
|
|
|
|
zipTree :: Tree a -> Tree b -> Tree (a,b)
|
|
|
|
|
zipTree (Node x xs) (Node y ys) = Node (x,y) $ zipWith zipTree xs ys
|
|
|
|
|
-- find use for?
|
|
|
|
|
--zipTree :: Tree a -> Tree b -> Tree (a,b)
|
|
|
|
|
--zipTree (Node x xs) (Node y ys) = Node (x,y) $ zipWith zipTree xs ys
|
|
|
|
|
|
|
|
|
|
{- |
|
|
|
|
|
Makes each node into its child number, i.e. the index it has
|
|
|
|
|