Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:08:12+00:00 2026-06-13T08:08:12+00:00

I’m trying to put all words in a trie in a string, a word

  • 0

I’m trying to put all words in a trie in a string, a word is denoted by the eow field being true for a certain character in the trie data structure, hence a trie can could have letters than lead up to no word, for ex “abc” is in the trie but “c”‘s eow field is false so “abc” is not a word

Here is my Data structure

struct Trie {

  bool eow; //when a Trie field isWord = true, hence there is a word
  char letter;
  Trie *letters[27];

}; 

and here is my attemped print-all function, basically trying to return all words in one string seperated by spaces for words

string printAll( string word, Trie& data)
{
  if (data.eow == 1)
    return word + " ";
  for (int i = 0; i < 26; i++) {
    if (data.letters[i] != NULL)
     printAll( word + data.letters[i]->letter, *(data.letters[i]));
 }
  return "";
}

Its not outputting what i want, any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:08:15+00:00Added an answer on June 13, 2026 at 8:08 am

    You’re not using the return value of your recursive printAll() call, so all the sub-words you generate are lost. Try something like this:

    string printAll(string word, const Trie& data)
    {
      string words;
    
      if (data.eow)
        words += word + " ";
      for (int i = 0; i < 26; i++) {
        if (data.letters[i] != NULL)
         words += printAll( word + data.letters[i]->letter, *(data.letters[i]));
      }
      return words;
    }
    

    For what it’s worth, this is a bit inefficient in that it allocates lots of temporary strings. Each recursive call has its own words string which is built up, returned, and destroyed. It’d be better to add all the words to one string.

    Also you might consider using a vector of words rather than adding them together with spaces. That way you can more easily iterate over each word.

    void getWords(const Trie& data, vector<string> &words, string word = "")
    {
      if (data.eow)
        words.push_back(word);
    
      for (int i = 0; i < 26; i++) {
        if (data.letters[i] != NULL)
          getWords(*(data.letters[i]), words, word + data.letters[i]->letter);
      }
    }
    

    Then to call it:

    vector<string> words;
    getWords(trie, words);
    
    for (size_t i = 0; i < words.size(); ++i) {
      if (i > 0)
        cout << " ";
    
      cout << words[i];
    }
    
    cout << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I want to count how many characters a certain string has in PHP, but
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to loop through a bunch of documents I have to put
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.