Advertisement
Guest User

Untitled

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