C++ C++;在Trie中实现节点计数
我有下面的代码,我需要帮助做节点计数到下面的代码!有人能帮我写这个函数吗? 我已经计算了单词数,但在节点数方面需要s的帮助C++ C++;在Trie中实现节点计数,c++,C++,我有下面的代码,我需要帮助做节点计数到下面的代码!有人能帮我写这个函数吗? 我已经计算了单词数,但在节点数方面需要s的帮助 // C++ implementation to count words in a trie #include <bits/stdc++.h> using namespace std; #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) // Alphabet size (# of symbols) #defi
// C++ implementation to count words in a trie
#include <bits/stdc++.h>
using namespace std;
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
// Alphabet size (# of symbols)
#define ALPHABET_SIZE (26)
// Converts key current character into index
// use only 'a' through 'z' and lower case
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')
// Trie node
struct TrieNode
{
struct TrieNode *children[ALPHABET_SIZE];
// isLeaf is true if the node represents
// end of a word
bool isLeaf;
};
// Returns new trie node (initialized to NULLs)
struct TrieNode *getNode(void)
{
struct TrieNode *pNode = new TrieNode;
pNode->isLeaf = false;
for (int i = 0; i < ALPHABET_SIZE; i++)
pNode->children[i] = NULL;
return pNode;
}
// If not present, inserts key into trie
// If the key is prefix of trie node, just
// marks leaf node
void insert(struct TrieNode *root, const char *key)
{
int length = strlen(key);
struct TrieNode *pCrawl = root;
for (int level = 0; level < length; level++)
{
int index = CHAR_TO_INDEX(key[level]);
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
pCrawl->isLeaf = true;
}
// Function to count number of words
int wordCount(struct TrieNode *root)
{
int result = 0;
// Leaf denotes end of a word
if (root -> isLeaf)
result++;
for (int i = 0; i < ALPHABET_SIZE; i++)
if (root -> children[i])
result += wordCount(root -> children[i]);
return result;
}
// Driver
int main()
{
// Input keys (use only 'a' through 'z'
// and lower case)
char keys[][8] = {"the", "a", "there", "answer",
"any", "by", "bye", "their"};
struct TrieNode *root = getNode();
// Construct Trie
for (int i = 0; i < ARRAY_SIZE(keys); i++)
insert(root, keys[i]);
cout << wordCount(root);
return 0;
}
<代码> //C++实现对单词的计数
#包括
使用名称空间std;
#定义数组大小(a)sizeof(a)/sizeof(a[0])
//字母表大小(符号的大小)
#定义字母表大小(26)
//将键当前字符转换为索引
//仅使用“a”到“z”和小写字母
#定义字符到索引(c)((int)c-(int)“a”)
//三节点
结构三节点
{
结构三节点*子项[字母表大小];
//如果节点表示
//词尾
布尔岛;
};
//返回新的trie节点(初始化为空)
结构三节点*getNode(无效)
{
结构三节点*pNode=新三节点;
pNode->isLeaf=false;
对于(int i=0;icout您可以对树进行简单的有序遍历
int inorderTraversal(TrieNode* pNode)
{
if (!pNode)
return 0;
int count = 0;
for (int i = 0; i < ALPHABET_SIZE; ++i)
count += inorderTraversal(pNode->children[i]);
return count + 1;
}
int-inorderTraversal(三节点*pNode)
{
if(!pNode)
返回0;
整数计数=0;
对于(int i=0;ichildren[i]);
返回计数+1;
}
Unrelated:和。将两者结合使用会放大彼此的最坏影响,将全局名称空间变成标识符的雷区,其中许多您可能甚至不知道存在,现在必须避免。Unrelated:如果您的编译器是最新的,您可以替换\define ARRAY\u SIZE(a)sizeof(a)/sizeof(a[0])
with.Unrelated:与CHAR\u to\u INDEX
宏相比,更喜欢函数。编译器可能会为您内联它,这样您就看不到性能差异。请详细说明节点计数的含义。有时,描述意图的行为会动摇必须采取的措施,以使预期行为从大脑的黑暗深处消失。如果没有,我们可以更好地帮助您,因为现在我们确切地知道您需要什么。这可能很简单,只要在每次构造三节点时增加一个计数器。在什么情况下,可以考虑对节点进行计数?