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

  • SEARCH
  • Home
  • 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 7026917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:10:49+00:00 2026-05-28T00:10:49+00:00

I have a specific question regarding the Trie data structure and what is going

  • 0

I have a specific question regarding the Trie data structure and what is going wrong with my code. Function parameter root always is NULL when I recursively call insert. Here is my code:

Code:

//subNodes is an array of TrieNode pointers that contains indices for all letters in the alphabet

bool insert(const string& word, TrieNode* root, int curI = 0)
//PRE:  word must be a valid word in a dictionary
//POST: True when a word is inserted into the Trie, false otherwise
{
    if(curI >= word.length())        //word has been scanned fully
    {
        root->isWord = true;
        return true;
    }
    else                             //word has more letters to be scanned
    {
        if(root->subNodes[word[curI] - 'A'] == NULL)    //if the current letter of the word is not in the trie
        {                                            //   insert the letter and advance the current letter of the word
            root->subNodes[word[curI] - 'A'] = new TrieNode(word[curI]);
            insert(word, root->subNodes[word[curI] - 'A'], curI++);
        }
        else                                         //if the currrent letter of the word is in the trie
        {                                            //   advance the current letter of the word
            insert(word, root->subNodes[word[curI] - 'A'], curI++);
        }
    }

}

I tested this by replacing subNodes[word[curI] - 'A'] with subNodes[word[13]] (13 is the index for N in the alphabet and I was testing the word not) and root was no longer NULL for that call. Therefore something is going wrong with the indexing. Does anyone know what is wrong? I considered using a C++ map or vector. Does anybody have any disagreements with using an array?

  • 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-05-28T00:10:50+00:00Added an answer on May 28, 2026 at 12:10 am

    Did you mean ++curl – i.e. pass in the incremented value to the recursion call? As it stands you’re passing the same value into each recursion since curl++ is post-increment. It may be easier to just write curl + 1 anyway since you don’t need the curl value again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I have a question regarding code structure, and was wondering what the best
I have a quick question regarding getting a specific value from a data grid,
I have a specific question regarding the usage of profiler. I am new to
i have a question regarding some complex data-binding. I want to be able to
I have a question regarding restricted pointer assignments. See the comments in code for
I have a specific question, that could use a general answer... When building Multi-Tier
I have a fairly specific question about concurrent programming in C. I have done
Sorry for a pretty specific question. I have a table (see bottom), and when
This is quite a specific question, and I have had no luck on the
Skip to the specific question as needed. Some background: The scenario: I have a

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.