如何找到尺码一棵树的高度? [英] How to find Size & Height for a tree?

查看:68
本文介绍了如何找到尺码一棵树的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

树的大小 =树中的节点数

树的高度 =树的最大深度

我正在用以下方法在c ++中实现一棵树:类节点私人的:列出孩子;char *标签;整数值;

I am implementing a tree in c++ with: class node private: list children; char* tag; int value;

推荐答案

如果size是Element的子级(直接和间接)数和element本身的数目:

If size is number of Element's children (direct and indirect) and element itself:

int Element::size(){
   if (children.empty())
     return 0;

   size_t size = 0;
   for (const auto &child : children)
      size += child->size();

   size += children.size();

   if (_depth == 0) return size + 1;
   else return size;
}

这篇关于如何找到尺码一棵树的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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