data BinTree a = Empty | Node a (BinTree a) (BinTree a)
deriving (Show)
I'm trying to figure out a way to display a binary tree in a manner such that for each level I go down in the tree, I want to add an additional * next to the name of the node and have them all separated by \n.
For example:
let x = Node "Parent" (Node "childLeft" (Node "grandChildLeftLeft" Emp Emp) Emp) (Node "childRight" Emp Emp)
putStrLn $ displayTree x
should return:
"Parent"
*"childLeft"
**"grandChildLeftLeft"
*"childRight"
My function (only prints up to one *):
displayTree :: Show a => BinTree a -> String
displayTree Emp = ""
displayTree (Node head Emp Emp) = (show head)
displayTree (Node head left Emp) = (show head) ++ "\n*" ++ displayTree left
displayTree (Node head Emp right) = (show head) ++ "\n*" ++ displayTree right
displayTree (Node head left right) = (show head) ++ "\n*" ++ displayTree left ++ "\n*" ++ displayTree right
My displayTree function would print:
"Parent"
*"childLeft"
*"grandChildLeftLeft"
*"childRight"
I want "grandChildLeftLeft" to have ** next to it instead of just *.
Any suggestions?
NOTE: I don't want to change the parameters that are passed into the function, so it should stay as displayTree :: Show a => BinTree a -> String