Advertisement
Coriic

Zigzag

Oct 24th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Prelude hiding (Left, Right)
  2.  
  3. data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show)
  4. data Direction = Left | Right
  5.  
  6. computeZigzag::Tree a->Int->Direction->[Int]
  7. computeZigzag Empty _ _ = [0]
  8. computeZigzag (Node _ Empty Empty) current _ = [current]
  9. computeZigzag (Node _ left right) current Left = computeZigzag left current Left ++ computeZigzag right (current + 1) Right
  10. computeZigzag (Node _ left right) current Right = computeZigzag left (current + 1) Left ++ computeZigzag right current Right
  11.  
  12. zigzag::Tree a->Int
  13. zigzag (Node  _ left right) = maximum(computeZigzag left 0 Left ++ computeZigzag right 0 Right)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement