I am struggling with how to recursively traverse a binary tree in Haskell
It is rather clear how it's done when the return type is a list. But I don't get how is it done if the return type is a tree itself?
for example, if we have the tree and wish to exclude zeros:
5
/ \
0 2
/ \
0 1
and we would like to return a tree with the other numbers:
5
/ \
1 2
Then I figured you would do something like this:
modTree :: Tree -> Tree
modTree Void = Void
modTree (Node l x r)
| x == 0 = modTree l
| otherwise = Node (modTree l) x (modTree r)
My issue is that I know that this will only traverse the left side. But I fail to grasp how recursively call the function in Haskell for the entire tree.
Any suggestions would be highly appreciated.
5were0, and the current0s were5. What tree would you want to see come out ofmodTree?0subtrees are not leaves?5nodes:0at the root, left tree is (4at the root,5to the left,1to the right) and right tree is5.