Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;在Trie中实现节点计数_C++ - Fatal编程技术网

C++ C++;在Trie中实现节点计数

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

我有下面的代码,我需要帮助做节点计数到下面的代码!有人能帮我写这个函数吗? 我已经计算了单词数,但在节点数方面需要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) 
#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;ichildren[i]=NULL; 返回pNode; } //如果不存在,则将密钥插入trie //如果键是trie节点的前缀,则只需 //标记叶节点 空插入(结构三节点*根,常量字符*键) { int length=strlen(键); 结构三节点*pCrawl=根; 对于(int级别=0;级别<长度;级别++) { int index=CHAR_TO_index(键[级别]); 如果(!pCrawl->子项[索引]) pCrawl->children[index]=getNode(); pCrawl=pCrawl->children[索引]; } //将最后一个节点标记为叶 pCrawl->isLeaf=true; } //计算字数的函数 int字数(结构三节点*根) { int结果=0; //叶子表示一个单词的结尾 如果(根->isLeaf) 结果++; 对于(int i=0;ichildren[i]) 结果+=字数(根->子项[i]); 返回结果; } //司机 int main() { //输入键(仅使用“a”到“z” //(小写) 字符键[][8]={“the”,“a”,“there”,“answer”, “任何”、“通过”、“再见”、“他们的”}; 结构三节点*root=getNode(); //构造Trie 对于(int i=0;i<数组大小(键);i++) 插入(根,键[i]);
cout您可以对树进行简单的有序遍历

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
宏相比,更喜欢函数。编译器可能会为您内联它,这样您就看不到性能差异。请详细说明节点计数的含义。有时,描述意图的行为会动摇必须采取的措施,以使预期行为从大脑的黑暗深处消失。如果没有,我们可以更好地帮助您,因为现在我们确切地知道您需要什么。这可能很简单,只要在每次构造
三节点时增加一个计数器。在什么情况下,可以考虑对节点进行计数?