I have a struct that can create a tree structure:
(struct node (value left middle right))
and another struct that defines a leaf node:
(struct emptyNode ())
How do I make a function that folds the tree into one value by summing the values together, starting with the left subtree, then middle, then right?
I was thinking of converting the tree into a list, with the order of the left subtree first, then middle, then right subtrees, and then doing a foldl on the list but am unsure on how to finish it, or if this is the right approach:
(define (treeToList tree)
(cond
[(node? tree) (append (node-left tree)) (treeToList (node-left tree))
(append (node-middle tree)) (treeToList (node-middle tree))
(append (node-right tree)) (treeToList (node-right tree))]
[else ] ;do nothing, leaf node
))