向树中删除和添加节点

发布于 2024-09-26 13:52:50 字数 1482 浏览 0 评论 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

首先,我需要能够从这个位置删除某人以及它“下面”的所有内容 - 因此删除“妈妈”将删除妈妈和她的父母、祖父母等。如果调用的位置中没有人,则该函数应该归还树。它应该是这样的: 删除:树*父母列表->树和调用是remove(t, pos)

这就是我所拥有的,但它不太正确。有人告诉我可以用 4 行来完成。

fun replace (Info(n,mf,ft) , Mom::[]) = Info(n,replace(mf,[]),Unspec)
  | replace (Info(n,mf,ft) , Dad::[]) = Info(n,Unspec,replace(ft,[]))
  | replace (Info(n,mf,ft) , [])      = Info(n,mf,ft)
  | replace (Info(n,mf,ft) , Mom::xs) = Info(n,replace(mf,[]),replace(ft,xs))
  | replace (Info(n,mf,ft) , Dad::xs) = Info(n,replace(mf,xs),replace(ft,[]))
  | replace (Unspec , x::xs)          = Unspec
  | replace (Unspec , [])             = Unspec;

我有一个想法:

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

但这不正确。我该怎么办?

我还应该能够将一个人 p, 插入树 t 的位置 pos - 如果该位置不存在,它应该只返回树。 插入:树 * 父母列表 * 人 ->树

我只是无法理解这个问题,我希望有人能够帮助我。我希望我已经说得足够清楚了(我知道它很长)。

I have an assignment, and I can't figure out what to do about it. I have a tree of people with their name, birth and death year. Think genealogy here. I have a bunch of datatypes to take care of ages, names, the tree itself, etc. and then I have a bunch of people and a tree.

The datatypes are:

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

First I need to be able to remove someone from this position and everything "under" it - so removing "Mom" will remove mom and her parents, grandparents, etc. If there's no person in the position called, the function should return the tree. It's supposed to be like this:
remove : Tree * parents list -> Tree and the call is remove(t, pos)

This is what I have, but it's not quite right. I've been told that I can do it in 4 lines.

fun replace (Info(n,mf,ft) , Mom::[]) = Info(n,replace(mf,[]),Unspec)
  | replace (Info(n,mf,ft) , Dad::[]) = Info(n,Unspec,replace(ft,[]))
  | replace (Info(n,mf,ft) , [])      = Info(n,mf,ft)
  | replace (Info(n,mf,ft) , Mom::xs) = Info(n,replace(mf,[]),replace(ft,xs))
  | replace (Info(n,mf,ft) , Dad::xs) = Info(n,replace(mf,xs),replace(ft,[]))
  | replace (Unspec , x::xs)          = Unspec
  | replace (Unspec , [])             = Unspec;

An idea I have:

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

But it's not correct. What do I do?

I'm also supposed to be able to insert a person p, into a tree t in the position pos - if the position doesn't exist it should just return the tree.
insert : tree * parents list * person -> tree

I just can't get my head around this, and I'm hoping that someone will be able to help me. I hope I've been clear enough in this (I know it's long).

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

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

发布评论

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

评论(1

故事和酒 2024-10-03 13:52:50

(重新发布显然我之前的答案没有在数据库崩溃中幸存下来)。

您将使用列表的头部来决定是分支到母亲子树还是父亲子树。这是正确的。然后,您可以使用列表的尾部作为路径的其余部分。这也是正确的。然而,当列表为空时(即您已到达目的地),您将执行以下操作:

| remove (Info(n,mf,ft) , [])      = Info(n,mf,ft)

换句话说:什么也没有。如果您将其更改为:

| remove (Info(n,mf,ft) , [])      = Unspec

它将按预期工作,用 Unspec 替换路径引导您到达的树的节点。

(Reposting as apparently my previous answer didn't survive the db crash).

You're taking the head of the list to decide whether to branch into the mother or the father subtree. This is correct. You then use the tail of the list as the rest of the path. This is also correct. However when the list is empty (i.e. you have reached your destination), you do this:

| remove (Info(n,mf,ft) , [])      = Info(n,mf,ft)

In other words: Nothing. If you change this to:

| remove (Info(n,mf,ft) , [])      = Unspec

It will work as intended, replacing the Node of the tree that your path leads you to with Unspec.

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