树中的节点是否被视为其自己的祖先?

发布于 2024-09-06 07:05:01 字数 697 浏览 3 评论 0原文

我想知道计算机科学背景下对“祖先”定义的共识是什么。

我之所以这么问,是因为在算法简介,第二版,第 14 页中。 259 有一个关于算法Tree-Successor(x)的描述看起来很奇怪。在寻找节点x的后继者时,

[...] 如果节点 x 的右子树为空且 x 有后继 y,则 y x 的最低祖先,其左孩子也是 x 的祖先。

在根具有键 2 以及子元素 13 的二叉搜索树中,1 的后继是它的父级 2。在本例中,xx 的后继者y 的左子级。根据这本书的定义,那么,x一定是它自己的祖先,除非我遗漏了一些东西。

我在勘误表中没有找到任何关于此的内容。

I'm wondering what the consensus is on the definition of "ancestor" in a computer science context.

I only ask because in Introduction to Algorithms, Second Edition, p. 259 there is a description of the algorithm Tree-Successor(x) that seems odd. In finding the successor of node x,

[...] if the right subtree of node x is empty and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x.

In a binary search tree with a root having key 2 and children 1 and 3, the successor of 1 is its parent 2. In this case, x is the left child of x's successor, y. According to the book's definition, then, x must be its own ancestor, unless I'm missing something.

I haven't found anything in the errata about this.

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

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

发布评论

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

评论(3

空城缀染半城烟沙 2024-09-13 07:05:01

这只是一个定义问题,但在本例中,。 CLRS 将 x 的祖先定义为从根到 x 的唯一路径上的任何节点,根据定义,该路径包括 x。

您引用的句子片段首先提到下一页的练习 12.2-6,其中指定了这一点:

(回想一下,每个节点都是它自己的祖先。)

:-)

It's merely a matter of definition, but in this case, yes. CLRS define an ancestor of x as any node on the unique path from the root to x, which by definition includes x.

The sentence fragment you quoted begins by mentioning exercise 12.2-6 on the next page, which specifies this:

(Recall that every node is its own ancestor.)

:-)

挽你眉间 2024-09-13 07:05:01

树中的节点是否被视为其自己的祖先?

通常情况下不会,据我所知。例如,在二叉树的维基百科页面中,祖先定义如下:

如果存在从节点 p 到节点 q 的路径,其中节点 p 比 q 更接近根节点,则 p 是 q 的祖先,q 是 p 的后代。

但显然,教科书对祖先的定义是节点是它自己的祖先。这个定义并不完全直观,但是教科书可以自由地为其使用的术语引入自己的定义。也许这个定义简化了一些相关的描述/定理/等。

Is a node in a tree considered its own ancestor?

Not normally, AFAIK. For example, in the Wikipedia page on binary trees, ancestor is defined thus:

If a path exists from node p to node q, where node p is closer to the root node than q, then p is an ancestor of q and q is a descendant of p.

But apparently that text book's definition of ancestor is such that a node is its own ancestor. This definition is not exactly intuitive, but a textbook is free to introduce its own definitions for the terminology that it uses. Maybe this definition simplifies some of the related descriptions / theorems / etc.

浮生面具三千个 2024-09-13 07:05:01

不,节点不是其自身的祖先。根据我的说法,它应该是:如果节点 x 的右子树为空并且 x 有后继 y,则 y 是 x 的最低祖先,其左孩子是 x 或 x 的祖先。但书中给出的代码据说可以处理此类情况。

No, a node is not ancestor of itself. According to me it should be: if the right subtree of node x is empty and x has a successor y, then y is the lowest ancestor of x whose left child is either x or an ancestor of x. but the code given in the book supposedly handling such type of cases.

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