递归遍历字典尝试以计算总字数
Recursively traverse dictionary trie to count total words
我已经从链表中构建了一个trie。每个节点包含一个 char 和一个由 27 个节点组成的数组(字母表中的字母 + $ 的额外位置表示单词的末尾(。我尝试编写一个递归方法来计算单词数,但它返回 1。我不确定如何解决它或确切的问题是什么。
int recursiveCount(Node* temp, int count)
{
if (temp->value == '$')
{
count++;
}
for (int i = 0; i < 27; i++)
{
if (temp->array[i] != NULL)
{
return recursiveCount(temp->arr[i],count);
}
}
return count;
}
您按值传递count
,这意味着当递归解开并且只返回一个"最远"的时,它超出了范围,因为这是第一个递增的count
,它只是 1。改为按引用传递int recursiveCount(Node* temp, int& temp);
正如在其他答案中已经指出的,您的问题是您有不同的count
变量,每个递归调用一个。递增一个不会改变其他的。除了传递(非常量(引用之外,您还可以采用更函数化的编程方法并从函数返回计数。当然,您需要汇总您所做的所有递归调用的返回计数:
unsigned recursiveCount(Node const * node) /* you don't change the node, so make
it const. Why a pointer btw? A
reference would do fine! */
{
unsigned count = 0; /* You aren't expecting a negative number of
words, are you? So use unsigned. */
if (node->value == '$')
{
count++;
}
for (int i = 0; i < 27; i++)
{
if (node->array[i] != NULL) /* "array"!? change that to a meaningful
name ... "children" is bad, but not as
bad as "array" ... */
{
count += recursiveCount(node->array[i]); /* "arr" was a typo I
suppose */
}
}
return count; /* consistent indentation, please! */
}
您已将其标记为C++,所以我很困惑为什么该方法不是 Node 的一部分。 请参阅封装。
递归代码中的重大错误是没有添加在每个后续节点中找到的每个 '$'。 特别是,您的代码仅返回 for 循环的一次调用计数,所有其他调用都将被丢弃。
for (int i = 0; i < 27; i++)
{
if (temp->array[i] != NULL)
{
return recursiveCount(temp->arr[i],count);
// This only returns one value,
// and ignores all the other 26 possible values
// these should sum together
}
}
请考虑使该方法成为节点的一部分。 注意 retVal 如何累积找到的每个 '$'。
int Node::recursiveCount()
{
int retVal = 0; // accumulation
if (value == '$')
{
retVal += 1; // 1 word found here
}
// keep searching here, there might still be words with prefix
for (size_t i = 0; i < 27; i++)
{
if (nullptr != nxt[i])
{
retVal += (nxt[i]->recursiveCount());
// ^^ accumulate any other words found
}
}
return (retVal); // return full count
}
相关文章:
- 有什么方法可以遍历结构吗
- 在循环中按顺序遍历成员变量
- 遍历模板参数
- 在遍历处理程序的向量时注册和注销处理程序
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 遍历并行数组以确定C++中的最大数字
- 遍历顺序由 std::文件系统directory_iterator给出
- 遍历链表时的无限循环
- 遍历unordered_map向量
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- C++声明双链表,使用两个 for 循环双向遍历列表并打印
- 如何正确地推回然后遍历堆中对象的向量?
- 遍历二维数组的所有子数组
- 如何在可变参数模板函数中遍历可变参数元组?
- 避免在遍历 IShellItemArray 时出现代码重复
- 从特定键开始遍历地图
- 递归遍历字典尝试以计算总字数
- 如何遍历二叉树并在不传递值的情况下计算值的出现次数
- 我正在尝试遍历 20x20 网格并计算产品,但总是得到不正确的结果。你能帮忙发现我的错误吗?
- 计算序列中元素的平均值,而不遍历它