这是树的定义:data Tree = Leaf Char | Node (Char, Tree, Tree)
我想以treeToInfix
如下形式编写一个函数:
treeToInfix :: Tree -> String
这里有些例子:
treeToInfix (Node ('*', (Node ('+', (Leaf 'a'), (Leaf 'b'))), (Leaf 'c')))
-- => "(a+b)*c"
treeToInfix (Node ('-', (Node ('+', (Leaf 'a') ,(Leaf 'b'))), (Leaf 'c')))
-- => "a+b-c"
treeToInfix (Node ('-', (Leaf 'c'), (Node ('+', (Leaf 'a') ,(Leaf 'b')))))
-- => "c-(a+b)"
treeToInfix (Node ('*', (Node ('/', (Leaf 'a'), (Leaf 'b'))), (Node ('/', (Leaf 'c'), (Leaf 'd')))))
-- => "a/b*c/d"
treeToInfix (Node ('+', (Node ('-', (Leaf 'a'), (Node ('*', (Leaf 'b'), (Leaf 'c'))))), (Node ('/', (Leaf 'd'), (Leaf 'e')))))
-- => "a-b*c+d/e"
我需要有关此程序算法的帮助。