module Hope.Util.Tree where import Control.Monad (liftM2) import Data.Tree prune :: Int -- ^ Maximum depth of nodes to keep. 0 means to keep only -- the root node. -> Tree a -> Tree a prune 0 (Node x _) = Node x [] prune n (Node x cs) = Node x (map (prune (n-1)) cs) fmapMTree :: Monad m => (a -> m b) -> Tree a -> m (Tree b) fmapMTree f (Node x xs) = liftM2 Node (f x) (mapM (fmapMTree f) xs) -- | Reverses the order of the children of each node in the tree. reverseTree :: Tree a -> Tree a reverseTree t = t { subForest = reverse (map reverseTree (subForest t)) }