所以我有一棵树定义为
data Tree a = Leaf | Node a (Tree a) (Tree a) deriving Show
我知道我可以将 Leaf 定义为 Leaf a。但我真的只是希望我的节点具有价值。我的问题是,当我进行搜索时,我有一个类型的返回值函数
Tree a -> a
由于叶子没有价值,我很困惑如果遇到叶子什么也不做。我试过了nil
,,,,似乎没有任何效果。" "
' '
[]
编辑代码
data Tree a = Leaf | Node a (Tree a) (Tree a) deriving Show
breadthFirst :: Tree a -> [a]
breadthFirst x = _breadthFirst [x]
_breadthFirst :: [Tree a] -> [a]
_breadthFirst [] = []
_breadthFirst xs = map treeValue xs ++
_breadthFirst (concat (map immediateChildren xs))
immediateChildren :: Tree a -> [Tree a]
immediateChildren (Leaf) = []
immediateChildren (Node n left right) = [left, right]
treeValue :: Tree a -> a
treeValue (Leaf) = //this is where i need nil
treeValue (Node n left right) = n
test = breadthFirst (Node 1 (Node 2 (Node 4 Leaf Leaf) Leaf) (Node 3 Leaf (Node 5 Leaf Leaf)))
main =
do putStrLn $ show $ test