Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Created December 23, 2016 21:24
Show Gist options
  • Save deque-blog/0dc2571cd010270c1ff181c5f7654b7f to your computer and use it in GitHub Desktop.
Save deque-blog/0dc2571cd010270c1ff181c5f7654b7f to your computer and use it in GitHub Desktop.
treeWalkH :: Tree a -> [a]
treeWalkH EmptyTree = []
treeWalkH (Tree root) = treeWalkH' root
treeWalkH' :: Node a -> [a]
treeWalkH' = reverse . loop [] []
where
loop out [] (Node v []) = v:out -- End of traversal
loop out stack (Node v (c:cs)) = loop (v:out) (toStack (cs:stack)) c
loop out (h:t) (Node v []) = loop (v:out) (toStack (tail h:t)) (head h)
toStack t = dropWhile null t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment