Skip to content

Instantly share code, notes, and snippets.

@evgenii-malov
Created January 18, 2021 21:33
Show Gist options
  • Select an option

  • Save evgenii-malov/cc1be8e8e80e114dc2b07aa346369f86 to your computer and use it in GitHub Desktop.

Select an option

Save evgenii-malov/cc1be8e8e80e114dc2b07aa346369f86 to your computer and use it in GitHub Desktop.
haskell binary tree traversal - inorder, postorder, preorder
-- 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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment