Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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++;trie-即使在插入之后,每个指针都指向null 我试图通过C++树来实现C++中的TIE。每个节点都有一个键,一个指向其他节点的指针数组,以及一个确定它是否为叶的布尔值。默认情况下,初始化新节点时,所有指针都指向null,并且每当添加新节点时,前一个节点都指向新节点_C++_Pointers_Trie - Fatal编程技术网

c++;trie-即使在插入之后,每个指针都指向null 我试图通过C++树来实现C++中的TIE。每个节点都有一个键,一个指向其他节点的指针数组,以及一个确定它是否为叶的布尔值。默认情况下,初始化新节点时,所有指针都指向null,并且每当添加新节点时,前一个节点都指向新节点

c++;trie-即使在插入之后,每个指针都指向null 我试图通过C++树来实现C++中的TIE。每个节点都有一个键,一个指向其他节点的指针数组,以及一个确定它是否为叶的布尔值。默认情况下,初始化新节点时,所有指针都指向null,并且每当添加新节点时,前一个节点都指向新节点,c++,pointers,trie,C++,Pointers,Trie,但由于某些原因,从未调用my else if(currentNode.children[arrayPointer]!=NULL)。我完全被难住了。我不确定这是因为我贴错了标签,还是因为标签顺序不对 #include <iostream> using namespace std; numberOfWords = 0; numberOfNodes = 0; struct Node; struct Node; struct Node { char key; bo

但由于某些原因,从未调用my else if(currentNode.children[arrayPointer]!=NULL)。我完全被难住了。我不确定这是因为我贴错了标签,还是因为标签顺序不对

#include <iostream>
using namespace std;

numberOfWords = 0;
numberOfNodes = 0;

struct Node;
struct Node;
    struct Node {
    char key;
    bool isLeaf;
    struct Node *children[36]; //26 letters + 10 digits
};



main() {
    //initializing the root
    Node root;
    root.key ='$';
    int i = 0;
    while (i < 36) {
        root.children[i] = NULL;    
        i++;
    }
    root.isLeaf = 1;
    numberOfNodes++;

    insertWord("the", root);
    insertWord("and", root);
    insertWord("there", root);

    cout << numberOfWords << " words found." << endl;
    cout << numberOfNodes << " nodes found." << endl;

}

void insertWord(string word, Node currentNode) {
    int wordLength = word.length();
    char letterToInsert;
    letterToInsert = word[0];
    int arrayPointer;
    arrayPointer = charToInteger(letterToInsert);


    //if the node is not found, a new path is inserted
    if (currentNode.children[arrayPointer] == NULL) {
        insertNewPath(word, currentNode);
    }

    //if the node with the current letter is found, I want to recursively call
    //insertWord and pass in the updated word and the next node.    

    //For some reason, this statement is never being called.
    else if (currentNode.children[arrayPointer] != NULL)  {
        string updatedWord;
        updatedWord = word.substr(1,wordLength);
        Node nextNode;
        currentNode.children[arrayPointer] = &nextNode;
        insertWord(updatedWord, nextNode);
    }

}       

//This function will keep recursively calling itself and passing in the new word 
//and the new node until the each letter is added.
void insertNewPath(string word, Node currentNode) {
    int wordLength;
    wordLength = word.length();
    if (wordLength > 0) {
        char letterToInsert;
        letterToInsert = word[0];
        int arrayPointer;
        //charToInteger takes a character and converts it into an integer value - a=0,b=1,...,8=34,9=35
        arrayPointer = charToInteger(letterToInsert);
        Node newNode;
        numberOfNodes++;
        newNode.key = letterToInsert;
        // Here, I set the current node's pointer to point to the new node. 
        // This should cause line 168 to execute when entering an existing
        // letter to the trie but for some reason it never does.
        currentNode.children[arrayPointer] = &newNode;
        //setting every pointer in the new node to null
        int i = 0;
        while (i < 36) {
            newNode.children[i] = NULL; 
            i++;
        }
        currentNode.isLeaf = 0;
        string updatedWord;
        updatedWord = word.substr(1, wordLength);
        insertNewPath(updatedWord, newNode);
    }
    else if (wordLength == 0){
        currentNode.isLeaf = 1;
        //Once all the letters have been added, the last 
        //node/letter to be added is set as a leaf, making
        //it a complete word.
    }   
}
相反,我得到了

3 words found.
11 nodes found.

因此,几乎每个字母都要创建一个节点。

您的代码太复杂了。您最多可以在5-8行代码中实现insert。简化它的第一步是为节点定义一个构造函数,将所有子节点设置为
nullptr
。另一个提示是,编写一个助手,它接受参数
(Node*root,const std::string&s,int index)
。递归现在很容易。如果索引等于字符串的长度,则完成。否则,请确保要递归到的子级存在,然后执行此操作。我很确定此代码中有许多其他错误,但在
insertNewPath
中,
newNode
被分配为自动(堆栈)局部变量,这意味着指向它的指针(
currentNode.childrent[arrayPointer]=&newNode;
)一旦执行离开定义它的块,则无效。添加到@ChristopherOicles。函数结束后,newNode超出范围。所以,你在参数中传递的引用使你的程序指向随机内存。你的代码太复杂了。您最多可以在5-8行代码中实现insert。简化它的第一步是为节点定义一个构造函数,将所有子节点设置为
nullptr
。另一个提示是,编写一个助手,它接受参数
(Node*root,const std::string&s,int index)
。递归现在很容易。如果索引等于字符串的长度,则完成。否则,请确保要递归到的子级存在,然后执行此操作。我很确定此代码中有许多其他错误,但在
insertNewPath
中,
newNode
被分配为自动(堆栈)局部变量,这意味着指向它的指针(
currentNode.childrent[arrayPointer]=&newNode;
)一旦执行离开定义它的块,则无效。添加到@ChristopherOicles。函数结束后,newNode超出范围。因此,在参数中传递的引用使程序指向随机内存。
3 words found.
11 nodes found.