Skip to content

Instantly share code, notes, and snippets.

@hsinewu
Last active April 21, 2018 06:19
Show Gist options
  • Save hsinewu/a141a401ec704eeaaa0f23489fd4ec01 to your computer and use it in GitHub Desktop.
Save hsinewu/a141a401ec704eeaaa0f23489fd4ec01 to your computer and use it in GitHub Desktop.
Naive (unbalanced) binary search tree in haskell
data BinarySearchTree = Null | Node Int (BinarySearchTree) (BinarySearchTree)
deriving Show
insert Null val = Node val Null Null
insert (Node x left right) val
| x > val = Node x left' right
| otherwise = Node x left right'
where
left' = insert left val
right' = insert right val
fromList xs = foldl insert Null xs
toPreorderArray Null = []
toPreorderArray (Node x left right) = toPreorderArray left ++ [x] ++ toPreorderArray right
main = do
print $ tree1
print $ tree2
print $ toPreorderArray tree2
where
a = insert Null 7
b = insert a 3
tree1 = insert b 9
tree2 = fromList [4,1,2,3,45,6,2,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment