Skip to content

Instantly share code, notes, and snippets.

@pscollins
Created April 21, 2015 06:34
Show Gist options
  • Save pscollins/44188f0872ba07e6686d to your computer and use it in GitHub Desktop.
Save pscollins/44188f0872ba07e6686d to your computer and use it in GitHub Desktop.
{-# LANGUAGE ScopedTypeVariables #-}
module BST where
import Test.QuickCheck
import Control.Monad
import Control.Applicative
data BST a = Empty
| Node {value :: a, left :: BST a, right :: BST a}
deriving Show
isBST :: Ord a => BST a -> Bool
isBST Empty = True
isBST (Node v l r) = (checkBranch (<) l) && (checkBranch (>) r)
where checkBranch f (Empty :: BST a) = True
checkBranch f b@(Node v' _ _) = (f v v') && (isBST b)
instance (Arbitrary a, Ord a) => Arbitrary (BST a) where
arbitrary = sized genBST where
genBST n = frequency [
(2, return Empty),
(abs n, Node <$> arbitrary
<*> genBST (n - 2) <*> genBST (n - 2))]
test = quickCheck isBST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment