Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-
- 4. (BONUS, 1.5p) Arbori binari de căutare
- Lucrând pe același tip de date de la exercițiul anterior, definiți
- funcția care află strămoșul comun a două noduri.
- Vom lucra doar pe arbori ce nu conțin valori duplicate!
- -}
- test4 :: TestPP ()
- test4 = tests 4 1.5
- [ testVal (lowestCommonAncestor t 1 3) (Just 2) "lowestCommonAncestor 1, 3" 0.5
- , testVal (lowestCommonAncestor t 4 5) (Just 4) "lowestCommonAncestor 4, 5" 0.5
- , testVal (lowestCommonAncestor t 4 7) Nothing "lowestCommonAncestor 4, 7" 0.5
- ]
- where
- t = makeBST [1, 2, 3, 4, 5, 6]
- lowestCommonAncestor :: (Ord a, Eq a) => BST a -> a -> a -> Maybe a
- lowestCommonAncestor (BSTNod x s d) n1 n2
- | findElem n1 s == Just n1 && findElem n2 d == Just n2 = Just x
- | findElem n1 d == Just n1 && findElem n2 s == Just n2 = Just x
- | findElem n1 s == Just n1 && findElem n2 s == Just n2 = lowestCommonAncestor s n1 n2
- | findElem n1 d == Just n1 && findElem n2 d == Just n2 = lowestCommonAncestor d n1 n2
- | x == n1 && findElem n2 (BSTNod x s d) == Just n2 = Just x
- | x == n2 && findElem n1 (BSTNod x s d) == Just n1 = Just x
- | otherwise = Nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement