Skip to content

Instantly share code, notes, and snippets.

@evgenii-malov
Last active January 31, 2021 11:32
Show Gist options
  • Save evgenii-malov/542375ba5ec9fd8658b3d3fec93188d4 to your computer and use it in GitHub Desktop.
Save evgenii-malov/542375ba5ec9fd8658b3d3fec93188d4 to your computer and use it in GitHub Desktop.
haskell full binary tree level order traversal
-- see video https://www.youtube.com/watch?v=q0bFTlmmsJI&t=332s
-- Full Binary Tree
-- A Binary Tree is a full binary tree if every node has 0 or 2 children
data Btree a = Leaf a | Node a (Btree a) (Btree a) deriving Show
tr :: Btree Integer
tr_l = Node 1 (Leaf 2) (Leaf 3)
tr_r = Node 4 (Leaf 5) (Leaf 6)
tr = Node 7 tr_l $ Leaf 8
zipWith' f xs [] = xs
zipWith' f [] xs = xs
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
levels :: Btree a -> [[a]]
levels (Leaf a) = [[a]]
levels (Node a l r) = [[a]] ++ zipWith' (++) (levels l) (levels r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment