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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:57:54+00:00 2026-06-03T02:57:54+00:00

I got a big infrastructure with lots of nested classes and i’m looking for

  • 0

I got a big infrastructure with lots of nested classes and i’m looking for the right design pattern.

I will explain:
I got a class called “Title” and class called Item
A Title contain different items , each item contain different data

You can see in the schematic the Title contain also ItemList which contains more items.
each item can contain item.

I’m adding a schematic of the infrastructure , It looks like a non binary tree
enter image description here

I’m looking for the best data structure for this object,

Requirements:

  1. With Node id and Tree Node i will be able to find the Node in a fast way
  2. Good code maintainability
  3. Can switch from flat to tree and from tree to flat
  • 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-03T02:57:55+00:00Added an answer on June 3, 2026 at 2:57 am

    Your question seems to request a quick way to access nodes given their ID value regardless of the hierarchy. In this case you would probably be best off (as Daniel Gabriel suggests in the comments) to create a flat list optimized for “searching” the flat list of IDs, since your look-up operation doesn’t care about the hierarchy. Just because you have your objects in a tree structure doesn’t mean you can’t also index them in another structure. A dictionary should be very good at this – the key could be an integer (or whatever your node id is) and the value could refer to the node object instance. You just have to remember to keep this structure in sync with your tree structure. Remove keys from the dictionary when nodes are removed, and add them to the dictionary when nodes are added (if the structure is dynamic).

    This fulfills your requirement #1, and probably #2 (if you encapsulate the synchronization of the index in the same class if necessary). For #3 we may need more information, but if you keep these structures synchronized, you will always have access to both formats without a need for conversion. Should be very easy.

    Here is some sample code that demonstrates how to encapsulate a structure that can provide both flat index and tree structures at all times:

    class Program
    {
      static void Main(string[] args)
      {
         Node<int, string> node1 = new Node<int, string>(1, "One");
         Node<int, string> node2 = new Node<int, string>(2, "Two");
         Node<int, string> node3 = new Node<int, string>(3, "Three");
         Node<int, string> node4 = new Node<int, string>(4, "Four");
         node2.Parent = node1;
         node3.Parent = node1;
         node4.Parent = node2;
         Console.WriteLine(node1.GetDump());
    
         Node<int, string> node5 = new Node<int, string>(5, "Five");
         // Test spliting the tree attaching it and it's subtree to another parent
         node2.Parent = node5;
         Console.WriteLine(node1.GetDump());
         Console.WriteLine(node5.GetDump());
    
         // Test re-attaching the detached parent as a child
         node1.Parent = node2;
         Console.WriteLine(node5.GetDump());
    
         // Test moving a node to another parent within the tree
         node1.Parent = node5;
         Console.WriteLine(node5.GetDump());
      }
    }
    
    /// <summary>
    /// Create a tree structure whose nodes are of type T and are indexed by ID type I
    /// </summary>
    /// <typeparam name="I">Type of the index</typeparam>
    /// <typeparam name="T">Type of the node</typeparam>
    class Node<I, T>
    {
      private Dictionary<I, Node<I, T>> rootIndex; // Shared flat index
      public I Id { get; private set; }
      public T Value { get; set; }
      private Node<I, T> parent;
      List<Node<I, T>> childNodes;
    
      public Node(I id, T value)
      {
         this.Id = id;
         this.Value = value;
         this.childNodes = new List<Node<I, T>>();
      }
    
      public string GetDump()
      {
         System.Text.StringBuilder sb = new StringBuilder();
         if (parent == null)
         {
            foreach (KeyValuePair<I, Node<I,T>> node in rootIndex)
            {
               sb.Append(string.Format("{0}:{1} ", node.Key, node.Value.Value));
            }
            sb.AppendLine();
         }
         sb.AppendLine(string.Format("ID={0}, Value={1}, ParentId={2}", Id, Value,
            (parent == null)?"(null)":parent.Id.ToString()));
         foreach (Node<I, T> child in childNodes)
         {
            string childDump = child.GetDump();
            foreach (string line in childDump.Split(new string[] {Environment.NewLine},
               StringSplitOptions.RemoveEmptyEntries))
            {
               sb.AppendLine("  " + line);
            }
         }
         return sb.ToString();
      }
    
      private void RemoveFromIndex(Dictionary<I, Node<I, T>> index)
      {
         index.Remove(Id);
         foreach(Node<I, T> node in childNodes)
            node.RemoveFromIndex(index);
      }
    
      private void ReplaceIndex(Dictionary<I, Node<I, T>> index)
      {
         rootIndex = index;
         rootIndex[Id] = this;
         foreach (Node<I, T> node in childNodes)
            node.ReplaceIndex(index);
      }
    
      public Node<I, T> Parent
      {
         get
         {
            return parent;
         }
         set
         {
            // If this node was in another tree, remove it from the other tree
            if (parent != null)
            {
               // If the tree is truly a separate tree, remove it and all nodes under
               // it from the old tree's index completely.
               if (value == null || (parent.rootIndex != value.rootIndex))
               {
                  // Split the child's index from the parent's
                  Dictionary<I, Node<I, T>> parentRootIndex = parent.rootIndex;
                  RemoveFromIndex(parentRootIndex);
                  rootIndex = new Dictionary<I, Node<I, T>>();
                  ReplaceIndex(rootIndex);
               }
    
               // Remove it from it's old parent node's child collection
               parent.childNodes.Remove(this);
            }
            // These operations only apply to a node that is not being removed
            // from the tree
            if (value != null)
            {
               // If parent does not already have an index, create one with itself listed
               if (value.rootIndex == null)
               {
                  value.rootIndex = new Dictionary<I, Node<I, T>>();
                  value.rootIndex[value.Id] = value;
               }
               // If the index for the child node is separate form that of the parent
               // node, merge them as appropriate
               if (this.rootIndex != value.rootIndex)
               {
                  // If this node has a complete index, merge the two tree indexes
                  // into the parent's index
                  if (this.rootIndex != null)
                  {
                     foreach (KeyValuePair<I, Node<I, T>> node in rootIndex)
                     {
                        if (value.rootIndex.ContainsKey(node.Key))
                           throw new InvalidOperationException(string.Format(
                             "Node Id {0} in child tree already exists in the parent",
                             node.Key));
                        value.rootIndex[node.Key] = node.Value;
                     }
                  }
                  else
                  {
                     // If this node does not have an index, it is not a tree;
                     // just add it to the parent's index.
                     if (value.rootIndex.ContainsKey(this.Id))
                        throw new InvalidOperationException(string.Format(
                        "Node Id {0} already exists in the parent's tree.", Id));
                     value.rootIndex[this.Id] = this;
                  }
               }
               // Make all nodes in a tree refer to a common root index.
               this.rootIndex = value.rootIndex;
               // Attach this node to the tree via the parent's child collection.
               value.childNodes.Add(this);
            }
            // Attach this node to the tree via this node's parent property
            // (null if removing from the tree)
            this.parent = value;
         }
      }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a big Javascript project that I'm trying to refactor into pseudo-classes: jsFiddle:
Working on a database class that got quite big, and is likely to get
Right my title isn't the best in the world. I've got a big code
So ive got this big-ass collection right? Its tree-backed(RBTree), so look-ups are fast, and
I got a Big hierarchic class that i would like to loop all it's
I got a big problem, I used Lazy load of images in ListView to
I've got a big UITextView , nearly full screen. If I tap it, the
I've got a big problem with routing: all pages matches with the same route!!
I've got 2 big questions! I'm trying to make a simple JSF Component to
I've got a big Windows legacy application composed by many executables interacting with 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.