Created
January 18, 2021 21:33
-
-
Save evgenii-malov/cc1be8e8e80e114dc2b07aa346369f86 to your computer and use it in GitHub Desktop.
haskell binary tree traversal - inorder, postorder, preorder
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://youtu.be/sv9y04GHvmY | |
| 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 | |
| pre_order :: Btree a -> [a] | |
| pre_order (Leaf a) = [a] | |
| pre_order (Node a l r) = [a] ++ pre_order(l) ++ pre_order(r) | |
| in_order (Leaf a) = [a] | |
| in_order (Node a l r) = in_order(l) ++ [a] ++ in_order(r) | |
| post_order (Leaf a) = [a] | |
| post_order (Node a l r) = post_order(l) ++ post_order(r) ++ [a] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
video with explanation
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