标准 ML 二叉树

发布于 2024-10-01 05:46:08 字数 801 浏览 5 评论 0原文

我仍然遇到这个问题,所以我可以寻求更多帮助。

我们得到:

datatype which = STRING of string | INT of int

第 1 部分。我们被告知需要为包含“which”类型值的二叉树创建另一个名为 whichTree 的数据类型,其中数据仅位于树的叶子处。

我认为这将是正确的答案:

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

事实证明,这与我运行时不同:

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

我收到错误。

我能够弄清楚我可以做这样的事情:

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

或者

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

但是这些对我来说似乎都不正确,因为在我的第一种情况下,我的数据类型名称中有一个 'a ,而在我的另一个情况下,当我说 Leaf of int 时应该能够使用 which 来指定 string 或 int。

谁能告诉我正确的答案是什么或为我提供一些帮助?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留蓝 2024-10-08 05:46:08

您对 whichTree 的定义是正确的,您只是缺少 inttreewhich 的构造函数:

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

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文