树中根节点的级别是多少? [英] What is level of root node in a tree?

查看:77
本文介绍了树中根节点的级别是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些文章说根节点的级别是0,而有些则说它是1.

Some articles say level of a root node is 0 whereas some say it's 1.

来自 https://www.gatevidyalay.com/tree-data-结构树术语/

在树中,从上到下的每个步骤称为树的级别.级别计数从0开始,并在每个级别或每一步以1递增.

In a tree, each step from top to bottom is called as level of a tree. The level count starts with 0 and increments by 1 at each level or step.

此处的root级别为0

Here level of root is 0

http://typeocaml中.com/2014/11/26/height-depth-and-level-a-tree/

要记住的重要一点是,在谈论级别时,它从1开始并且根的级别是1.在解决与级别相关的问题时,我们需要注意这一点.

The important thing to remember is when talking about level, it starts from 1 and the level of the root is 1. We need to be careful about this when solving problems related to level.

所以它说等级是1

我不知道哪一个是正确的.

I can't understand which one is correct.

推荐答案

它们只是不同的定义.通常将节点的级别定义为距根节点的路径中的边数,这意味着根节点的级别为0;因此,根节点的级别为0.但是,如果您希望使用基于1的索引将其定义为该路径中的位置,那么这样做就没错,只是不寻常.

They are just different definitions. It is more common to define the level of a node as the number of edges in a path from the root node, which entails that the level of the root node is 0; but if you wish to define it as the position in that path using 1-based indexing then it is not wrong to do so, just unusual.

自然数的定义之间类似地存在分歧.大多数文本将0定义为第一个自然数,有些则将1定义为.实际上,这几乎没有问题,因为要么在使用定义之前就先声明了定义,要么可以从上下文中确定正在使用的定义,或者可以在没有任何区别的上下文中使用它.

There is similarly a disagreement between definitions of the natural numbers; most texts define that 0 is the first natural number, some say 1 is. In practice, this creates almost no problems because either the definition is stated before it is used, or you can work out from the context which definition is being used, or it's used in a context where it makes no difference.

例如,如果我说子节点的级别比其父节点的级别高一倍,则定义的选择无关紧要.另外,如果我说一个完整的二叉树在 h 级别上有2 ^ h 个节点,那么您可以确定我是从0开始计数,而不是从1开始计数.

For example, if I say that the level of a child node is one more than the level of its parent, then the choice of definition doesn't matter. Alternatively, if I say that a complete binary tree has 2^h nodes at level h, then you can determine I am counting from 0 instead of 1.

这篇关于树中根节点的级别是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆