fork download
  1. data Tree x = Leaf x
  2. | Node x [Tree x]
  3.  
  4. dfs tree limit
  5. | limit == 0 = []
  6. case tree of
  7. Leaf x -> [x]
  8. Node x children -> x : traverse children (limit - 1)
  9.  
  10. traverse children limit
  11. | limit == 0 = []
  12. case children of
  13. [] -> []
  14. x : rest ->
  15. result ++ traverse rest (limit - length result)
  16. where result = dfs x limit
  17.  
  18. example_tree =
  19. Node 0 [
  20. Node 1 [Leaf 2, Leaf 3],
  21. Node 4 [Node 5 [Leaf 6]],
  22. Leaf 7
  23. ]
  24.  
  25. main = do
  26. print $ map (\limit -> dfs example_tree limit) [1..8]
Success #stdin #stdout 0.02s 3592KB
stdin
Standard input is empty
stdout
[[0],[0,1],[0,1,2],[0,1,2,3],[0,1,2,3,4],[0,1,2,3,4,5],[0,1,2,3,4,5,6],[0,1,2,3,4,5,6,7]]