5
我一直在玩Haskell中的二叉树,我试图实现一个dfs变体,它从根节点返回路径(由左和右)组成包含搜索值的节点。我认为最好是返回Maybe Directions
类型。尝试实现Haskell二叉树搜索的路径记录
这是迄今为止已实施的内容。
data Tree a = Empty | Node a (Tree a) (Tree a)
deriving (Show, Eq)
data Direction = L | R
deriving (Show)
type Directions = [Direction]
inTree :: (Eq a) => a -> Tree a -> [Direction]
inTree val (Node x l r)
| val == x = []
| l /= Empty = L:(inTree val l)
| r /= Empty = R:(inTree val r)
| otherwise =
但我不知道如何让它遍历整个树。我感觉好像我可能在思考过于强制。