Created
December 23, 2016 21:24
-
-
Save deque-blog/0dc2571cd010270c1ff181c5f7654b7f to your computer and use it in GitHub Desktop.
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
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