Add fold-based solution for isBST.
Thanks random guy in the front for the idea.
This commit is contained in:
parent
dbd1e17b2a
commit
baa7ab1459
|
@ -191,12 +191,7 @@ inorder = treeFoldr (:) []
|
||||||
-- True
|
-- True
|
||||||
--
|
--
|
||||||
isBST :: Tree -> Bool
|
isBST :: Tree -> Bool
|
||||||
isBST (Leaf _) = True
|
isBST tree = snd $ treeFoldl (\v (p, b) -> (v, b && v >= p)) (minInt tree, True) tree
|
||||||
isBST (Node i l r) = i >= value l && i <= value r && isBST l && isBST r
|
|
||||||
where
|
|
||||||
value (Leaf i) = i
|
|
||||||
value (Node i _ _) = i
|
|
||||||
|
|
||||||
|
|
||||||
-- | Check whether a number is contained in a binary search tree.
|
-- | Check whether a number is contained in a binary search tree.
|
||||||
-- (You may assume that the given tree is a binary search tree.)
|
-- (You may assume that the given tree is a binary search tree.)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user