I'm implementing the insert function of a BST, below is my code:
data Tree a = Empty
| Node Integer (Tree a) a (Tree a)
deriving (Show, Eq)
treeInsert :: (Ord a) => a -> Tree a -> Tree a
treeInsert x Empty = Node 0 Empty x Empty
treeInsert x (Node height left a right)
| x == a = Node height left x right
| x < a = Node height (treeInsert x left) a right
| x > a = Node height left a (treeInsert x right)
When I use my code it doesn't do anything, like the recurrence is still working
tree = foldTree [1, 2, 3]
tree
=> Node 1 (Node 0 Empty 2 Empty) 3 (Node 0 Empty 1 Empty)
tree = treeInsert 4 tree
tree
I'm new to Haskell, could anybody help? Thanks a lot.
tree = treeInsert 4 tree, you make a cycle, since you here definetreein terms of itself.