Stack Exchange
Trie data structure: How to store words without overwriting in recursion function?
So this is my implementation of the trie data structure
#include <iostream>

class TrieNode{
public:
    TrieNode* children[26];
    bool isEndOfWord;
    char letter;

    TrieNode(){

        for(int i = 0; i < 26; i++){
            this->children[i] = NULL;
            this->isEndOfWord = false;
        }
    }
    void insert(TrieNode* root, std::string word){



        for(int i = 0; i < word.size(); i++){
            int index = word[i] - 'a';
            if(root->children[index] == NULL){
                root->children[index] = new TrieNode();
                root->letter = word[i];
            }

            root = root->children[index];
        }
        root->isEndOfWord = true;
    }

    

    void printALLWords(TrieNode* root, char words[], char storeList[],
                      int level){

        if(root->isEndOfWord){
            words[level] = '\0';
            strcpy(storeList, words);
            return;


        }
        else{
            for(int i = 0; i < 26; i++){
                if(root->children[i] != NULL){
                    char letter = 'a' + i;
                    words[level] = letter;
                    printALLWords(root->children[i], words, storeList, level + 1);
                }
            }
        }

    }

};


int main(){

    TrieNode* root = new TrieNode();

    root->insert(root, "car");
    root->insert(root, "cat");

    char words[20];
     char storeList[40];
    int level = 0;
   root->printALLWords(root, words, storeList, level);

    for(int i = 0; i < 6; i++){
        std::cout << storeList[i];
    }



So I am trying to store the words into into the char array called storeList so that in main I can just do a simple for loop and take a look at the words stored in the list which are from the trie. I tried to use strcpy to copy the word into storeList array and it seem to work but the problem is that it basically overwrites the first word and only one word is stored but I want all the words in the trie to be copied to this storeList array. In the function printAllWords, specifically in the base case I try to copy the current word into StoreList array but once it finishes the other word it overwrites it and when I try to print the words from storeList I only get one word. How can I make it so that I can store a word without overwriting it?
So I want my output to look like this:
car
cat

But my current output is:
cat

which is not what I want.

You should learn to step through the code with a debugger. Try to find a video tutorial online for that! As a new user here, please also take the[tour] and read [ask].

Unrelated to your problem, but do you really need to do this->isEndOfWord = false 26 times in the constructor? Also, NULL is the old C-compatible null-pointer, in C++ use nullptr. And preferably don't use plain pointers at all, in your case use std::unique_ptr. And lastly, why is letter a member variable?

Sounds like you just want strcat in place of strcpy. Don't forget to initialize storeList to an empty string.

Thanks for all the suggestions, I will definitely try what you guys have mentioned.
