Skip to content

Instantly share code, notes, and snippets.

@evgenii-malov
Last active January 31, 2021 11:29
Show Gist options
  • Save evgenii-malov/8dd9fe486883da7f936056819cfde289 to your computer and use it in GitHub Desktop.
Save evgenii-malov/8dd9fe486883da7f936056819cfde289 to your computer and use it in GitHub Desktop.
level order traversal with bfs
-- see video https://www.youtube.com/watch?v=OibwXW1ssV8
-- pretty print tree https://www.youtube.com/watch?v=Ud-1Z0hBlB8
data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
tr_l = Node "b" (Node "d" Empty Empty) (Node "e" Empty Empty)
tr_r = Node "c" (Node "f" Empty Empty) (Node "g" Empty Empty)
--tr = Node "a" tr_l tr_r :: Btree String
tr_ll = Node "x" tr_l Empty
tr = Node "a" tr_ll tr_r :: Btree String
lev_ord t = lo [t]
where lo :: [Btree a] -> [a]
lo [] = []
lo (Empty:xs) = lo xs
lo (Node v l r:xs) = v : lo (xs ++ [l,r])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment