Sha256: 8c39f0f084698ac4053f6cd277bde032bfd10183f405e0f681f46695b5c268b0

Contents?: true

Size: 1.14 KB

Versions: 396

Compression:

Stored size: 1.14 KB

Contents

module BST ( BST, bstLeft, bstRight, bstValue,
             empty, singleton, insert, fromList, toList
           ) where

import Data.Foldable (foldl', toList)

data BST a = Tip
           | Node (BST a) a (BST a)
           deriving (Show, Eq)

-- This allows us to use the toList from Foldable.
-- This may be seen as gratuitous for just one toList function,
-- but keep in mind now this BST could use other Foldable functions too,
-- not just toList.
instance Foldable BST where
  foldMap _f Tip = mempty
  foldMap f (Node l v r) = foldMap f l `mappend` f v `mappend` foldMap f r

bstValue :: BST a -> Maybe a
bstValue Tip = Nothing
bstValue (Node _ v _) = Just v

bstLeft :: BST a -> Maybe (BST a)
bstLeft Tip = Nothing
bstLeft (Node l _ _) = Just l

bstRight :: BST a -> Maybe (BST a)
bstRight Tip = Nothing
bstRight (Node _ _ r) = Just r

empty :: BST a
empty = Tip

singleton :: a -> BST a
singleton x = Node empty x empty

insert :: Ord a => a -> BST a -> BST a
insert x Tip = singleton x
insert x (Node l v r) =
  if v >= x
  then Node (insert x l) v r
  else Node l v (insert x r)

fromList :: Ord a => [a] -> BST a
fromList = foldl' (flip insert) empty

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.118 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.117 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.116 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.115 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.114 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.113 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.111 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.110 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.109 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.108 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.107 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.106 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.105 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.104 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.103 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.102 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.101 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.100 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs
trackler-2.2.1.99 tracks/haskell/exercises/binary-search-tree/examples/success-standard/src/BST.hs