Skip to content

Instantly share code, notes, and snippets.

@passy
Created February 3, 2018 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passy/cf74893716af6125ccb2112bed7608a6 to your computer and use it in GitHub Desktop.
Save passy/cf74893716af6125ccb2112bed7608a6 to your computer and use it in GitHub Desktop.
data Tree a = Empty | Node a (Tree a) (Tree a)
deriving Show
type IntTree = Tree Int
collapse :: IntTree -> [Int]
collapse t =
case t of
Empty -> [-1]
Node a Empty r -> a : -1 : collapse r
Node a l Empty -> a : (collapse l ++ [-1])
Node a l r -> a : collapse l ++ collapse r
main =
-- 1
-- \
-- 2
-- / \
-- 3 4
-- \
-- 5
--
let tree :: Tree Int = Node 1 (Empty) (Node 2 (Node 3 Empty Empty) (Node 4 Empty (Node 5 Empty Empty)))
in
print $ collapse tree
-- Prints: [1,-1,2,3,-1,-1,4,-1,5,-1,-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment