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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:37:14+00:00 2026-05-17T22:37:14+00:00

I have a data structure that looks like this: public class Node { public

  • 0

I have a data structure that looks like this:

public class Node
{
     public string Code { get; set; }
     public string Description { get; set; }
     ...
     public List<Node> Children { get; set; }
}

I want to write a method that will return a specific node, given the specified Code. Normally I would just do a recursive walk through the hierarchy to find the node, but I’m concerned about performance. There will be several thousand nodes in the hierarchy, and this method will be called many, many times.

How do I structure this to make it faster? Can I use an existing data structure that perhaps performs a binary search on Code while retaining the hierarchical structure, without re-implementing some form of binary search myself?

  • 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-17T22:37:15+00:00Added an answer on May 17, 2026 at 10:37 pm

    Add all the nodes to dictionary with the code as key. (you can do it once), the look-up in dictionary is basically O(1).

    void FillDictionary(Dictionary<string, Node> dictionary, Node node)
    {
      if (dictionary.ContainsKey(node.Code))
        return;
    
      dictionary.Add(node.Code, node);
    
      foreach (Node child in node.Children)
        FillDictionary(dictionary, child)
    }  
    

    If you know the root, usage will be:

    var dictionary = new Dictionary<string, Node>();
    FillDictionary(dictionary, rootNode);
    

    If you don’t you can call the FillDictionary() method on all your nodes with the same dictionary.

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

Sidebar

Related Questions

I have data to get listings according to their location. The locations are mapped
Let's assume we are designing a Stack class test-first (TDD): public class Stack<T> {
This is the directory structure of the project (maven2 is used): pom.xml /src /main
I know this looks a bit long but i tried to explain the problem
If I have a set of tags (<100), and a set of objects (~25000),
I have a simulation written in C++ in which I need to maintain a
I'm having a difficult time expressing this problem, so bear with me and please
I currently am trying to add tags around certain words in a webpage. This
I have a MySQL table with about 1000 rows, filled with different types of
I'm having a problem creating a C# P/invoke wrapper around a third party C

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.