Skip to content

Instantly share code, notes, and snippets.

@booo
Created May 6, 2013 20:37
Show Gist options
  • Select an option

  • Save booo/5527996 to your computer and use it in GitHub Desktop.

Select an option

Save booo/5527996 to your computer and use it in GitHub Desktop.
import Data.List
data STree a = Empty | Node (STree a) a (STree a)
deriving (Eq, Ord, Show)
bft :: [STree a] -> [[a]]
bft = unfoldr f
where f x = case x of
[] -> Nothing
(Empty):xs -> Just ([], xs)
(Node l v r):xs -> Just ([v], xs ++ [l] ++ [r])
t = Node (Node (Node Empty 1 Empty) 1 (Node (Node Empty 2 Empty) 2 Empty))
3
(Node (Node Empty 5 Empty) 8 Empty)
t2 = Node Empty 2 Empty
-- bft [t] == [[3],[1,8],[1,2,5],[2],[]]
-- tl = (Node (Node Empty 1 Empty) 1 (Node (Node Empty 2 Empty) 2 Empty))
-- tr = (Node (Node Empty 5 Empty) 8 Empty)
-- bft [tl, tr] == [[1,8],[1,2,5],[2],[]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment