标准ML二叉树数据类型

时间:2011-11-02 20:08:55

标签: binary-tree sml ml

我之前已经问过这个问题,但之前问题的答案都没有对我有用,所以我会尝试不同的方法。

我做到了这一点:

> datatype which = STRING of string | INT of int;
datatype which = INT of int | STRING of string
> datatype whichTree = Empty | Leaf of which | Node of whichTree*whichTree;
datatype whichTree = Empty | Leaf of which | Node of whichTree * whichTree

但是当我尝试建树时

> val mytree = Node(Leaf(which 2), Leaf(which 6));

我收到错误。

Error-Value or constructor (which) has not been declared   Found near
Node( Leaf(which(2)), Leaf(which(6)))
Error-Value or constructor (which) has not been declared   Found near
Node( Leaf(which(2)), Leaf(which(6)))
Static errors (pass2)

1 个答案:

答案 0 :(得分:1)

which是数据类型的名称;它不是构造函数。相反,您必须按如下方式创建树:

> val mytree = Node(Leaf(INT 2), Leaf(STRING "6"));