Is a node in a tree considered its own ancestor?

偶尔善良 提交于 2019-12-04 03:21:03

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.)

:-)

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!