Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-
- 5. (BONUS, 1.5p) Arbori binari de căutare
- Definiți funcția nodesDistance, care returnează distanța
- între două noduri (numărul minim de arce ce trebuie parcurse
- pentru a ajunge dintr-un nod în celălalt).
- Vom lucra doar pe arbori ce nu conțin valori duplicate!
- -}
- test5 :: TestPP ()
- test5 = tests 5 1.5
- [ testVal (nodesDistance t 1 5) (Just 4) "nodesDistance 1, 5" 0.5
- , testVal (nodesDistance t 4 5) (Just 2) "nodesDistance 4, 5" 0.5
- , testVal (nodesDistance t 2 7) Nothing "nodesDistance 2, 7" 0.5
- ]
- where
- t = makeBST [1, 2, 3, 4, 5, 6]
- nodesDistance :: (Ord a, Eq a) => BST a -> a -> a-> Maybe Int
- nodesDistance t n1 n2
- | lowestCommonAncestor t n1 n2 /= Nothing = Just $ (findDistance n1 (retTree (lowestCommonAncestor t n1 n2) t)) + (findDistance n2 (retTree (lowestCommonAncestor t n1 n2) t))
- | otherwise = Nothing
- retTree :: (Ord a, Eq a) => a -> BST a -> BST a
- retTree y (BSTNod x s d)
- | x == y = (BSTNod x s d)
- | x < y = retTree y s
- | otherwise = retTree y d
- findDistance :: (Ord a, Eq a) => a -> BST a -> Int
- findDistance y (BSTNod x s d)
- | x == y = 0
- | x < y = 1 + findDistance y s
- | otherwise = 1 + findDistance y d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement