在 SML 中向树添加节点

发布于 2024-09-26 19:00:47 字数 952 浏览 2 评论 0原文

有人建议我把这个作为一个单独的问题来问,这样我就会这样做。

我有一棵人树,就像家谱一样。它从一个人开始,然后分支到父母、祖父母等。我希望能够将一个人插入到树上的某个位置(基本上替换那里的任何人)。

这些数据类型很重要:

datatype year    = Year of int | UnkYear | Irrelevant
datatype name    = Name of string | UnkName
datatype sex     = Man | Woman | UnkSex
datatype person  = Person of name * sex * year * year
datatype parents = Dad | Mom
datatype tree    = Unspec | Info of person * tree * tree

分配如下: 声明一个函数插入:树*父母列表*人->树,因此调用 insert(t, pos, p) 会将人 p 插入到树 i 中的位置 pos 中 - 假设该位置存在于树中。如果没有,它应该返回 t。

因此,我需要能够在我的树中选取一个人(比如说妈妈)并将她替换为露西(妈妈和露西都是使用数据类型 person 预先声明的值)。

到目前为止,我有这样的:

fun insert (Info(n,mf,ft) , Mom::xs , p) = Info(p, mf, insert(ft,xs,p))
  | insert (Info(n,mf,ft) , Dad::xs , p) = Info(p, insert(mf,xs,p), ft)
  | insert (Info(n,mf,ft) , [] , p)      = Unspec

似乎要做的就是删除 t 的 pos 中的任何人,并用 p 替换根 - 不完全是我想要的:S 另外,模式匹配尚未完成。

有什么想法可以让我搬到这里吗?

I was advised to ask this as a separate question, so that I will do.

I have a tree of people, like in genealogy. It starts with a person and branches off into parents, grandparents, etc. I want to be able to insert a person into a spot on the tree (basically replacing whoever is there).

These datatypes are important:

datatype year    = Year of int | UnkYear | Irrelevant
datatype name    = Name of string | UnkName
datatype sex     = Man | Woman | UnkSex
datatype person  = Person of name * sex * year * year
datatype parents = Dad | Mom
datatype tree    = Unspec | Info of person * tree * tree

The assignment is as follows:
Declare a function insert : tree * parents list * person -> tree, so that calling insert (t, pos, p) will insert the person p in the postion pos in the tree i - assuming that the position exists in the tree. If it doesn't it should return t.

So I need to be able to take a person in my tree (let's say Mom) and replace her with Lucy (Mom and Lucy are both pre-declared values using the datatype person).

So far I have this:

fun insert (Info(n,mf,ft) , Mom::xs , p) = Info(p, mf, insert(ft,xs,p))
  | insert (Info(n,mf,ft) , Dad::xs , p) = Info(p, insert(mf,xs,p), ft)
  | insert (Info(n,mf,ft) , [] , p)      = Unspec

All is seems to do is delete whoever is in pos of t and replace the root with p - not quite what I want it to do :S Also, the pattern matching isn't finised.

Any ideas to get me moving here?

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

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

发布评论

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

评论(1

橙味迷妹 2024-10-03 19:00:47

好的,当你到达给定的位置时,你想用给定的人替换树中该点的人。您所做的是,您将在每一步中替换该人,除非您到达目的地,在这种情况下,您只需删除那里的节点即可。

所以你需要做的是:

在parents-list还不为空的情况下,不要将person n替换为p - 只需遍历到适当的子树。

如果父母列表为空并且您当前位于信息节点,请将该节点中的人员替换为 p

如果父母列表为空并且您当前位于 Unspec 节点,请将 Unspec 替换为包含 p 和两个空子树(即 Unspec< /代码>s)。

如果您到达 Unspec 节点,尽管parents-list 尚未为空,则只需返回 Unspec,按照分配保持树不变。

Ok, when you reach the given position, you want to replace the person at that point in the tree with the given person. What you're doing is, you're replacing the person each step of the way, except when you reach the destination, in which case you simply delete the node that is there.

So what you need to do is:

In the case where the parents-list is not yet empty, don't replace the person n with p - just traverse into the appropriate subtree.

In the case where the parents-list is empty and you're currently at an Info-node, do replace the person in that node with p.

In the case where the parents-list is empty and you're currently at an Unspec-node, replace the Unspec with a new Info-node containing p and two empty subtrees (i.e. Unspecs).

In the case where you reach an Unspec node although the parents-list is not yet empty, just return Unspec, keeping the tree unchanged as per the assignment.

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