求多路树的高度

发布于 2024-07-25 16:05:00 字数 234 浏览 6 评论 0原文

如何求多路树的高度? 如果我想找到二叉树的高度,我可以这样做:

int height(node *root) {
  if (root == NULL)
    return 0;
  else
    return max(height(root->left), height(root->right)) + 1;
}

但我不确定是否可以将类似的递归方法应用于多路树。

How do you find the height of a multi-way tree? If I wanted to find the height of a binary tree, I could do something like this:

int height(node *root) {
  if (root == NULL)
    return 0;
  else
    return max(height(root->left), height(root->right)) + 1;
}

But I am not sure if I can apply a similar recursive method to a multiway tree.

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

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

发布评论

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

评论(5

太阳公公是暖光 2024-08-01 16:05:00

一般情况是:

int height(node *root)
{
    if (root == NULL)
        return 0;
    else {
        // pseudo code
        int max = 0;
        for each child {
            int height = height(child);
            if (height > max) max = height;
        }
        return max + 1;
    }
}

The general case would be:

int height(node *root)
{
    if (root == NULL)
        return 0;
    else {
        // pseudo code
        int max = 0;
        for each child {
            int height = height(child);
            if (height > max) max = height;
        }
        return max + 1;
    }
}
骄兵必败 2024-08-01 16:05:00

这取决于子节点的存储方式。 让我们假设它们存储在向量中。 然后您可以使用以下内容来计算它们的高度。

int height(node* root ) {
  if ( !root ) {
    return 0;
  }
  int max = 0;
  for ( vector<node*>::const_iterator it = root->children.begin();
        it != root->children.end();
        it++ ) {
    int cur = height(*it);
    if ( cur > max ) {  
      max = cur;
    }
  }
  return max+1;
}

This depends on how the child nodes are stored. Lets assume for a second that they are stored in a vector. You could then use the following to calculate their height.

int height(node* root ) {
  if ( !root ) {
    return 0;
  }
  int max = 0;
  for ( vector<node*>::const_iterator it = root->children.begin();
        it != root->children.end();
        it++ ) {
    int cur = height(*it);
    if ( cur > max ) {  
      max = cur;
    }
  }
  return max+1;
}
书信已泛黄 2024-08-01 16:05:00

就其价值而言(几乎没有价值),这个问题在 SML 等纯函数式语言中可以完美呈现:

fun height Node(children) = (foldl max -1 (map height children)) + 1

For what it's worth (almost nothing), this problem renders beautifully in pure-functional languages like SML:

fun height Node(children) = (foldl max -1 (map height children)) + 1
夜还是长夜 2024-08-01 16:05:00

如果非空:

  • 找到每个孩子的高度,
  • 取最大值
  • 加1

if non-null:

  • find the height of each of the children
  • take the maximum
  • add 1
鲸落 2024-08-01 16:05:00

不是'1+从(当前)根节点的任何子节点开始的子树的最大高度'吗?

请注意,二叉树只是多路树的一种特殊情况,其中子节点已知为左子节点和右子节点。 如果根节点指针为空,则结果为零。

Isn't it '1 + maximum height of sub-tree starting from any of the child nodes of the (current) root node'?

Note that the binary tree is just a special case of the multi-way tree where the child nodes are known to be the left child and the right child. The result is zero if the root node pointer is null.

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