user494948
user494948

Reputation: 51

Standard ML Binary Tree

I am still having problems with this so I can going to ask for more help.

We are given:

datatype which = STRING of string | INT of int

Part 1. We are told we need to created another datatype named whichTree for a binary tree containing the values of type "which" where data is only at the leaves of the tree.

I assumed this would be the correct answer:

datatype whichTree = Empty | Leaf of which | Node of whichTree*whichTree;

It turns out it is not as when I run:

val inttree = Node(Leaf(1), Leaf(2));

I get an error.

I was able to figure out that I can do something like this:

datatype 'a whichTree = Empty | Leaf of 'a | Node of 'a whichTree * 'a whichTree;

or

datatype whichTree = Empty | Leaf of int | Node of whichTree*whichTree;

But neither of these seem correct to me as in my first case my datatype name has a 'a in it and in my other one I am saying Leaf of int when I should be able to specify string or int by using which.

Can anyone tell me what the correct answer it or provide me with some help?

Upvotes: 2

Views: 1497

Answers (1)

ZoogieZork
ZoogieZork

Reputation: 11289

Your definition of whichTree is correct, you're just missing the constructor for which in your inttree:

val inttree = Node(Leaf(INT 1), Leaf(INT 2))

Upvotes: 4

Related Questions