我一直在玩Haskell中的二叉树,我正在尝试实现一个dfs变体,该变量返回从根节点到包含搜索值的节点的路径(由left和rights组成).我认为最好返回一个Maybe Directions类型.
以下是目前已实施的内容.
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 =
Run Code Online (Sandbox Code Playgroud)
但我不知道如何让它遍历整棵树.我觉得我可能会过于强迫地思考.