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 6589997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:14:42+00:00 2026-05-25T17:14:42+00:00

I’m a C# beginner and at the moment I’m trying to challenge myself with

  • 0

I’m a C# beginner and at the moment I’m trying to challenge myself with different problems.

At the moment I’m trying to build a web application where you can input letters and wildcards to search after possible words.

Trough previous questions about this I’ve decided to build a Trie containing letters generated from 400k+ words. Later I’ll search the Trie for possible word matches depending on letter and wildcard input.

I’ve built two classes, one representing one node in the Trie and one representing the whole Trie.

I’m currently at a standstill, my problem is that I want to add multiple children to the Trie at multiple levels and every child has to be uniqe.

Doing this manually would look something like this:

//Level 1
Root.Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));
//Level 2
Root.Children[0].Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));
//Level 3
Root.Children[0].Children[0].Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));

Problem is that I want to add children with one or more loops and doing it this way seems a little “wrong”:

LetterArray = Word.ToCharArray();
int level = 0;
foreach (char Letter in LetterArray)
{
    //Level 1
    if (level == 0)
        Root.Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));
    //Level 2
    if (level == 1)    
        Root.Children[0].Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));
    //Level 3
    if (level == 2)
        Root.Children[0].Children[0].Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));

    level++;
}

What I need is one or more loops with “clean” code, think you can help me?
For the Trie to be searchable later the letters needs to come in order i think.
Here are my other questions related to this: Question 1, Question 2.

Here’s my TrieNode Class:

public class TrieNode
{
    private char _Letter;
    private bool _IsEndOfWord;
    private List<TrieNode> _Children;

    public char Letter {
        get { return _Letter; }
        set { _Letter = value; }
    }
    public bool IsEndOfWord {
        get { return _IsEndOfWord; }
        set { _IsEndOfWord = value; }
    }
    public List<TrieNode> Children {
        get { return _Children; }
        set { _Children = value; }
    }

    public TrieNode(char letter, bool isEndOfWord, List<TrieNode> children) {
        Letter = letter;
        IsEndOfWord = isEndOfWord;
        Children = children;
    }
}

… and here is my Trie Class:

public class Trie
{
    private TrieNode _Root;

    public TrieNode Root
    {
        get { return _Root; }
        set { _Root = value; }
    }

    public Trie(List<string> Words)
    {
        Root = new TrieNode('^', false, new List<TrieNode>());
        char[] LetterArray;

        foreach (String Word in Words)
        {
            LetterArray = Word.ToCharArray();

            foreach (char Letter in LetterArray)
            {
                // Here is where I want to add nodes to my Trie
            }
        }
    }
}
  • 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-25T17:14:43+00:00Added an answer on May 25, 2026 at 5:14 pm

    I’d accomplish this through recursion as well, however my recursive method would be inside the TrieNode class:

    public AddWord(string word)
    {
        if (String.IsNullOrEmpty(word))
        {
            throw new ArgumentException("word must contain values.", word);
        }
    
        var letter = word[0];
        var child = Children.FirstOrDefault(x => x.Letter = letter);
    
        if (child == null)
        {
            //The child doesn't exist.  Create it.
            child = new TrieNode(letter, false, new List<TrieNode>());
        }
    
        if (word.Length > 1)
        {
            child.AddWord(word.Substring(1);
        }
        else
        {
            child.IsEndOfWord = true;
        }
    }
    

    Finally you just have to change your initial loop in your Trie constructor:

    foreach (String Word in Words)
    {
        _Root.AddWord(Word);
    }
    

    (note: I haven’t tested this code so there may be typos).

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

Sidebar

Related Questions

Beginner here trying to get a pipeline working in bash. If somebody can see
How can I create an Endpoint for a web service? I am a beginner
I'm a beginner in vimscript and even if I search the web for hours
I'm a beginner to programming and work with Python. At the moment I'm trying
JS beginner here. I need help with a script to place different content in
Question : Beginner Level Code: Pure Javascript I had created a web page in
I am very beginner to OOP and now I am trying to write some
I am trying to add a left column to a simple web page that
I am an absolute beginner to C++ and Qt. I am broadcasting different data
I'm a beginner in android and at the moment, I'm working on simple app.

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.