-1

I have the following data structure and was wanting to turn it into a tree. I have this working with recursion but was wondering if there was a more efficient/better way?

public class Structure
{
    public int Id {get;set;}
    public int Level1 {get;set;}
    public int Level2 {get;set;}
    public int Level3 {get;set;}
    public int Level4 {get;set;}
}

var data = new List<Structure> {
    new Structure{Id=1,Level1=1,Level2=11,Level3=35,Level4=105},
    new Structure{Id=2,Level1=1,Level2=11,Level3=35,Level4=106},
    new Structure{Id=3,Level1=1,Level2=11,Level3=36,Level4=107},
    new Structure{Id=4,Level1=1,Level2=11,Level3=36,Level4=108},
    new Structure{Id=5,Level1=1,Level2=11,Level3=36,Level4=109},
    new Structure{Id=6,Level1=1,Level2=11,Level3=36,Level4=110},
    new Structure{Id=7,Level1=1,Level2=11,Level3=36,Level4=111},
    new Structure{Id=8,Level1=2,Level2=12,Level3=37,Level4=112},
    new Structure{Id=9,Level1=2,Level2=12,Level3=37,Level4=113},
    new Structure{Id=10,Level1=2,Level2=12,Level3=37,Level4=114},
    new Structure{Id=11,Level1=2,Level2=12,Level3=37,Level4=115},
    new Structure{Id=12,Level1=2,Level2=12,Level3=37,Level4=116},
    new Structure{Id=13,Level1=2,Level2=12,Level3=38,Level4=117},
    new Structure{Id=14,Level1=2,Level2=12,Level3=39,Level4=118},
    new Structure{Id=15,Level1=2,Level2=12,Level3=39,Level4=119},
    new Structure{Id=16,Level1=2,Level2=12,Level3=40,Level4=120},
    new Structure{Id=17,Level1=2,Level2=12,Level3=40,Level4=121},
    new Structure{Id=18,Level1=2,Level2=12,Level3=40,Level4=122},
    new Structure{Id=19,Level1=2,Level2=12,Level3=40,Level4=123},
    new Structure{Id=20,Level1=2,Level2=12,Level3=40,Level4=124},
    new Structure{Id=21,Level1=2,Level2=12,Level3=40,Level4=125},
    new Structure{Id=22,Level1=2,Level2=12,Level3=41,Level4=126},
    new Structure{Id=23,Level1=2,Level2=12,Level3=41,Level4=127},
    new Structure{Id=24,Level1=3,Level2=13,Level3=42,Level4=128},
    new Structure{Id=25,Level1=3,Level2=13,Level3=43,Level4=129},
    new Structure{Id=26,Level1=3,Level2=13,Level3=44,Level4=130},
    new Structure{Id=27,Level1=3,Level2=13,Level3=45,Level4=131},
    new Structure{Id=28,Level1=3,Level2=13,Level3=45,Level4=132},
    new Structure{Id=29,Level1=3,Level2=13,Level3=46,Level4=133},
    new Structure{Id=30,Level1=3,Level2=13,Level3=47,Level4=134},
    new Structure{Id=31,Level1=3,Level2=13,Level3=47,Level4=135},
    new Structure{Id=32,Level1=3,Level2=13,Level3=47,Level4=136},
    new Structure{Id=33,Level1=3,Level2=13,Level3=48,Level4=137},
    new Structure{Id=34,Level1=3,Level2=14,Level3=49,Level4=138},
    new Structure{Id=35,Level1=3,Level2=14,Level3=49,Level4=141},
    new Structure{Id=36,Level1=3,Level2=14,Level3=49,Level4=142},
    new Structure{Id=37,Level1=3,Level2=14,Level3=49,Level4=143},
    new Structure{Id=38,Level1=3,Level2=14,Level3=49,Level4=144},
    new Structure{Id=39,Level1=3,Level2=14,Level3=50,Level4=145}
}

So wanting the output to look something like:

1 - 11 - 35 - 105
            - 106
       - 36 - 107
            - 108
            - 109
            - 110
            - 111
.....
.....
.....
3 - 14 - 49 - 138
            - 141
            - 142
            - 143
            - 144
       - 50 - 145
garethb
  • 3,951
  • 6
  • 32
  • 52
  • 1
    Recursion is *generally* the efficient way of solving problems.. :) Anything specific you have in mind? Can you post the recursive version? – Vikas Gupta Nov 12 '14 at 02:45
  • 1
    and are you looking for an output dump? or do you have a resultant object model in mind? – thewyzard Nov 12 '14 at 02:46
  • Recursion is *sometimes* an efficient way of solving problems. Working with tree structures is indeed one of the situations in which recursion is useful. Why don't you post the code you have working? Is it actually causing performance problems, or are you just nervous about recursion? – Blorgbeard Nov 12 '14 at 02:50

2 Answers2

0

Why not using a tree, like :

class TREE
{
    NODE root;

    public TREE(NODE root)
    {
        this.root = root;
    }


    public void insert(NODE node, NODE parent)
    {
        NODE temp = parent;
        if (temp.child == null)
            temp.child = node;
        else
        {
            temp = temp.child;
            insert(node, temp);
        }
    }


}

On the other side you create a class for NODE:

class NODE
{
    public int data;
    public NODE child;

    public NODE(int data)
    {
        this.data = data;

        child = null;
    }
}

In my example, the node is permitted to hold a unique child, you can work around this point, then only insert your nodes!

chouaib
  • 2,763
  • 5
  • 20
  • 35
  • I've done something similar but this still requires recursion. I was wanting to see if there was some magic that would do away with the recursion. – garethb Nov 12 '14 at 03:55
0

I assume the recursive solution you have come up with is using method recursion. If your tree data structure is deep then you will likely run into problems with stack space. Convert the implicit recursion you already have into explicit recursion within the method using a Stack or similar.

There is plenty of advice on how to do this, for example:

Community
  • 1
  • 1
Xcalibur
  • 3,613
  • 2
  • 32
  • 26
  • Was hoping for no recursion answer, but valid point! will mark as answer if no better solution comes along. – garethb Nov 12 '14 at 03:56