Add fold-based solution for isBST.

Thanks random guy in the front for the idea.
This commit is contained in:
Danila Fedorin 2019-01-18 11:16:19 -08:00
parent dbd1e17b2a
commit baa7ab1459
1 changed files with 1 additions and 6 deletions

View File

@ -191,12 +191,7 @@ inorder = treeFoldr (:) []
-- True
--
isBST :: Tree -> Bool
isBST (Leaf _) = True
isBST (Node i l r) = i >= value l && i <= value r && isBST l && isBST r
where
value (Leaf i) = i
value (Node i _ _) = i
isBST tree = snd $ treeFoldl (\v (p, b) -> (v, b && v >= p)) (minInt tree, True) tree
-- | Check whether a number is contained in a binary search tree.
-- (You may assume that the given tree is a binary search tree.)