0

I've create a binary search tree:

data SearchTree a = Empty | Node a (SearchTree a) (SearchTree a) deriving (Show, Eq, Ord)

insertTreeElements :: (Ord a) => a -> SearchTree a -> SearchTree a --Create    binary tree input method
insertTreeElements x Empty = Node x Empty Empty
insertTreeElements x (Node a left right)
   | x == a     = Node x left right
   | x < a  = Node a (insertTreeElements x left) right
   | x > a      = Node a left (insertTreeElements x right)

makeTree :: (Ord a) => (a -> SearchTree a -> SearchTree a) -> SearchTree a -> [a] -> SearchTree a --Create binary tree
makeTree iTE Empty li = foldr iTE Empty li

I'm trying to pass in the following list:

Type Age = Int
mylist = [Age 12, Age 100, Age 2, Age 3, Age 43]

However the output is not correct, it does not order the tree based on the size of the ages? How do I need to edit the insertTreeElements to allow it to work with 'Age x'?

1 Answer 1

1

Probably the biggest problem you have is this one:

   | x > a      = Node a left (insertTreeElements x left)

Notice that you write left twice -- presumably one of them should be right.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.