Created
December 10, 2021 14:34
-
-
Save evgenii-malov/a1d866ddfed13c97c0c103076dc1ec74 to your computer and use it in GitHub Desktop.
Left and right view of binary tree
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
| -- subscribe my channel https://www.youtube.com/channel/UCBGYk5s1Fnk0PpqSwtH1M9w | |
| -- https://gist.github.com/evgenii-malov/1fc29a652751451dbca0d54d454cc1ef | |
| import PrettyT | |
| -- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show | |
| paths :: Btree a -> [[a]] | |
| paths Empty = [] | |
| paths (Node a Empty Empty) = [[a]] | |
| paths (Node a l r) = (a:) <$> ((paths l)++(paths r)) | |
| maskm x y = x ++ drop (length x) y | |
| lv t = foldl maskm [] $ paths t | |
| rv t = foldr (flip maskm) [] $ paths t | |
| -- -------------------------------------- | |
| zip' :: [[a]] -> [[a]] -> [[a]] | |
| zip' l [] = l | |
| zip' [] r = r | |
| zip' (x:xs) (y:ys) = (x++y): zip' xs ys | |
| lo :: Btree a -> [[a]] | |
| lo Empty = [] | |
| lo (Node a l r) = [[a]] ++ zip' (lo l) (lo r) | |
| lv' t = head <$> lo t | |
| rv' t = last <$> lo t |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
subscribe my channel https://www.youtube.com/channel/UCBGYk5s1Fnk0PpqSwtH1M9w