我有这个问题要做:
“定义一个函数findpath:: BTree a -> a -> Path
(其中Btree
a 在前面的问题中定义),给定一棵二叉树t
和一个 value ,如果有一个值,则x
返回从根t
到叶的路径,否则返回值。运行时间你的程序应该与树中的节点数成线性关系。”x
Nothing
到目前为止,我有:
data Maybe a = Nothing | Just a
data BTree a = Leaf a | Fork (BTree a) (BTree a)
type Path = Maybe [Dir]
data Dir = Left | Right
findpath :: Eq a => BTree a -> a -> Path
findpath (Leaf y) x = if y==x then ??? else Nothing
findpath (Fork l r) x = nodepath (findpath l x) (findpath r x) where
nodepath :: Path -> Path -> Path
nodepath Nothing Nothing = Nothing
nodepath Nothing pathr = Just [R]
nodepath pathl Nothing = Just [L]
我仍然无法在(Leaf y)
案例中构建最终答案