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 =
但我不知道如何讓它遍歷整個樹。我感覺好像我可能在思考過於強制。