Last active
January 31, 2021 11:32
-
-
Save evgenii-malov/7fd9b0179636acc2396d535712bbf502 to your computer and use it in GitHub Desktop.
Haskell
This file contains 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 for description: https://www.youtube.com/watch?v=4mrBrJw_tVc&feature=youtu.be | |
-- (Nodes must be uniq) | |
import Data.Set | |
import Data.List.Split (chunksOf) | |
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 tr_r | |
all_paths :: Btree a -> [[a]] | |
all_paths (Leaf a) = [[a]] | |
all_paths (Node a l r) = (a:) <$> (all_paths l)++(all_paths r) | |
(!!!) :: Int -> [a] -> Maybe a | |
(!!!) i xs | |
| (i> -1) && (length xs > i) = Just (xs!!i) | |
| otherwise = Nothing | |
longest tr = maximum $ length <$> (all_paths tr) | |
cutters tr = ((!!!) <$> [0..(longest tr)-1]) | |
lt tr = fromList <$> (chunksOf (length.all_paths $ tr) ((cutters tr) <*> (all_paths tr))) |
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