Search code examples
c#-4.0data-structurescomplex-data-types

complex datatype in c#


i need some help for my problem related to complex datatype in C#. I have following type of data and i want to save it in variable, but it will be performance efficient as i have to use it for search and there will be lot of data in it. Data sample is as follow:

ParentNode1
           ChildNode1
           ChildNode2
           ChildNode3
ParentNode2
ParentNode3
ParentNode4
           ChildNode1
           ChildNode2
                     Node1
                     Node2
                     Node3
                          Nth level Node1
           ChildNode3
ParentNode5

Above data is just a sample to show hierarchy of data. I'm not sure nested List, Dictionary, ienumerable or link list which will be best related to performance. Thanks


Solution

  • If you know that a search will take place at a single level, then you might want a list of lists: one list for each level. If your hierarchy has N levels, then you have N lists. Each one contains nodes that are:

    ListNode
        Data // string
        ParentIndex // index of parent in the previous list
    

    So to search level 4, you go to the list for that level and do your contains or regex test on each node in that level. If it matches, then the ParentIndex value will get you the parent, and its ParentIndex will get you the grandparent, etc.

    This way, you don't have to worry about navigating the hierarchy except when you find a match, and you don't have to write nested or recursive algorithms to traverse the tree.

    You could maintain your hierarchy, as well, with each top-level node containing a list of child nodes, and build this secondary list only for searching.