Skip to content

Instantly share code, notes, and snippets.

@evgenii-malov
Last active January 31, 2021 11:32
Show Gist options
  • Save evgenii-malov/7fd9b0179636acc2396d535712bbf502 to your computer and use it in GitHub Desktop.
Save evgenii-malov/7fd9b0179636acc2396d535712bbf502 to your computer and use it in GitHub Desktop.
Haskell
-- 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)))