RedHotChiliPepper

tree .hs

Nov 28th, 2021 (edited)
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type WEdge w = (Vertex, (w, Vertex))
  2. type WGraph w = Array Vertex [(w, Vertex)]
  3. data WTree w a = WNode { rootLabelW :: a
  4.                        , subForestW :: [(w, WTree w a)]
  5.                        } deriving Show
  6.  
  7. buildWG :: Bounds -> [WEdge w] -> WGraph w
  8. buildWG = accumArray (flip (:)) []
  9.  
  10. dfsWTree :: WGraph w -> Vertex -> WTree w Vertex
  11. dfsWTree g u = go u u where
  12.     go p u = WNode u [(w, go u v) | (w, v) <- g!u, v /= p]
  13.  
  14. instance Functor (WTree w) where
  15.     fmap f = go where
  16.         go (WNode a ts) = WNode (f a) [(w, go t) | (w, t) <- ts]
Add Comment
Please, Sign In to add comment