Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.Maybe
- data Tree a = Empty | Node a [Tree a]
- depthFirstSearch _ Empty = Nothing
- depthFirstSearch needle (Node val children) =
- if needle == val then Just needle
- else let results = dropWhile isNothing $
- map (depthFirstSearch needle) children
- in if null results then Nothing else head results
- main = do
- needle <- getLine
- let tree = Node "a" [Node "b" [Node "d" [Empty], Node "e" [Empty], Node "f" [Empty]], Node "c" [Node "g" [Node "h" [Empty]]]]
- putStrLn $ show $ depthFirstSearch needle tree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement