Last active
January 31, 2021 11:32
-
-
Save evgenii-malov/542375ba5ec9fd8658b3d3fec93188d4 to your computer and use it in GitHub Desktop.
haskell full binary tree level order traversal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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
see video
Videos:
pretty print tree
Level order traversal with BFS
Level order traversall with all paths
Level order traversall with zipwith
Level order traversall with label tree
Level order traversal of binary tree with fmap