Compare commits

..

No commits in common. "d378cc45259379f3f51d8ccd5a1df8a5699a70ce" and "ba781dad2da836994e224ffeecf5ccb8b2a7d6d2" have entirely different histories.

2 changed files with 8 additions and 8 deletions

View File

@ -10,7 +10,7 @@ data Tree
-- | An example binary tree, which will be used in tests. -- | An example binary tree, which will be used in tests.
t1 :: Tree t1 :: Tree
t1 = Node 1 (Node 2 (Node 3 (Leaf 4) (Lea 5)) t1 = Node 1 (Node 2 (Node 3 (Leaf 4) (Leaf 5))
(Leaf 6)) (Leaf 6))
(Node 7 (Leaf 8) (Leaf 9)) (Node 7 (Leaf 8) (Leaf 9))
@ -62,7 +62,7 @@ treeFold f a (Node i l r) = f i $ treeFold f (treeFold f a r) l
-- 1 -- 1
-- --
leftmost :: Tree -> Int leftmost :: Tree -> Int
leftmost = treeFoldr1 (\a _ -> a) leftmost = treeFoldr1 const
-- | The integer at the right-most node of a binary tree. -- | The integer at the right-most node of a binary tree.
-- --
@ -79,7 +79,7 @@ leftmost = treeFoldr1 (\a _ -> a)
-- 9 -- 9
-- --
rightmost :: Tree -> Int rightmost :: Tree -> Int
rightmost = treeFoldl1 (\a _ -> a) rightmost = treeFoldl1 const
-- | Get the maximum integer from a binary tree. -- | Get the maximum integer from a binary tree.
-- --

View File

@ -92,12 +92,12 @@ valueAt (x:xs) (Node a l r) = valueAt xs $ if x == L then l else r
-- >>> pathTo 10 ex -- >>> pathTo 10 ex
-- Nothing -- Nothing
-- --
pathTo :: Eq a => a -> Tree a -> Maybe Path pathTo :: Eq a => a -> Tree a -> Maybe Path
pathTo v End = Nothing pathTo v End = Nothing
pathTo v (Node a l r) = orElse currentNode $ orElse (pathHelper v l L) $ pathHelper v r R pathTo v (Node a l r) = orElse currentNode $ orElse (pathHelper v l L) $ pathHelper v r R
where where
currentNode = if a == v then Just [] else Nothing currentNode = if a == v then Just [] else Nothing
pathHelper v tree dir = fmap (dir:) (pathTo v tree) pathHelper v tree dir = (pathTo v tree) >>= (Just . (dir:))
orElse m1 m2 = if isJust m1 then m1 else m2 orElse m1 m2 = case m1 of
isJust mx = mx /= Nothing Just _ -> m1
Nothing -> m2