如何在C++中实现按级别计数/显示节点的功能
How to implement a function to count/display nodes by level in C++
我在一本在线书中得到了这个作业,但我无法弄清楚。我想我必须使用某种BFS或DFS,但我不知道该怎么做。
我没有尝试过很多东西,害怕浪费太多时间,但我尝试的是使用从节点开始的迭代并使用大量 if 语句来查找我需要的节点上的不同值,但它根本不起作用。
string CharacterAnalyzer::nodeCountByLevel(nodeptr_t const node) const {
/* TODO (1):
* Return a formatted string of the node count at each level. For example for the
* text "Hello all!" the string will be:
*
* Each line is terminated with a newline.
*
* Node count by level:
* Level 1: TN(1), LRN(2), LON(0), RON(0), LN(0)
* Level 2: TN(2), LRN(0), LON(1), RON(1), LN(0)
* Level 3: TN(2), LRN(0), LON(0), RON(0), LN(2)
*
* where
* TN - level node count
* LRN - two child node count
* LON - left only child count
* RON - right only child count
* LON - leaf node count
*/
}// end nodeCountByLevel()
////////////////////////
//The accompanying code in .h
////////////////////////
bool hasTwoChildren(nodeptr_t const node) const { return (node->left && node->right); }// end hasTwoChildren()
bool hasLeftChildOnly(nodeptr_t const node) const { return (node->left && !node->right); }// end hasLeftChildOnly()
bool hasRightChildOnly(nodeptr_t const node) const { return (node->right && !node->left); }// end hasRightChildOnly()
bool isLeaf(nodeptr_t const node) const { return (!node->left && !node->right); }// end isLeaf()
实现 DFS 的一种方法是使用递归函数,如下所示
void depth_first_search(const nodeptr_t node)
{
// Do something with node
// ...
if (node->left)
depth_first_search(node->left);
if (node->right)
depth_first_search(node->right);
}
您可以轻松地使此功能知道它的深度
如下void depth_first_search(const nodeptr_t node, unsigned depth)
{
// Do something with node and depth
// ...
if (node->left)
depth_first_search(node->left, depth + 1);
if (node->right)
depth_first_search(node->right, depth + 1);
}
在您的情况下,"做某事"是用它在此深度遇到的节点类型的计数更新某个容器(例如std::vector
)。为此,您当然需要从函数内部访问此结构。这可以通过以下任一方式实现
- 使容器(或指向它的指针或引用)成为全局变量
- 在将容器作为成员的类中实现
depth_first_search
- 将容器作为附加参数传递给
depth_first_search
最后一个选项的一个变体是使用"访客模式",如下所示
void depth_first_search(const nodeptr_t node, unsigned depth, Visitor& visitor)
{
// Do something with node and depth
// ...
visitor.visit(node, depth);
if (node->left)
depth_first_search(node->left, depth + 1, visitor);
if (node->right)
depth_first_search(node->right, depth + 1, visitor);
}
从Visitor
派生的一些类现在确切地知道如何处理节点和深度信息(例如,对它们进行计数并将结果存储在容器中)。这样做的好处是,如果你下次需要对节点做一些不同的事情,你只需实现一个新的访问者。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 反向给定链表中的K节点
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 为什么将函数的返回类型从结构节点*更改为void后,链表的元素没有显示create_ll和显示?
- 为什么我的节点临时节点不能显示正确的数据?
- 如何在C++中实现按级别计数/显示节点的功能
- 简单树插入节点函数显示为空
- 我应该在此代码中更改以显示路由上每个节点的点
- 不显示链表节点
- 我写了一个函数来删除循环链接列表中的第 1 个节点,但输出显示无限次"55 44 33 22 11 99",那么我该如何解决呢?
- 在二进制搜索树中显示到节点的路径
- 错误-无法访问Omnet中创建新节点的显示字符串
- 显示属于二叉搜索树深度路径的节点
- 提升图遍历显示节点"hidden"
- 二叉搜索树递归显示节点
- 我的程序在将节点读入链表后不断循环,只重复显示一个节点
- 在C++中垂直和水平显示链表节点
- 为什么我的打印函数不会显示链表任一子节点中的元素?
- 如何在链表中显示特定的节点
- 如何使用QSortFilterProxyModel来过滤只显示子节点及其父节点的树模型