Build a Tree Object from a Table with Parent Child

Hallo,

I have a table in my DB with an Organisation that has each a key an a parent Id.
Further I have a C# Object OrganisationUnit that has a Property SubOrganisations.

Can someone provide me please a simple Algorithm to fill this Object so I have an Hiearchical Object? I guess I have to use rescursion but I'm stuck at the Moment.

Best regards
Patrik
LVL 1
mr-kennyAsked:
Who is Participating?
 
saraganiConnect With a Mentor Commented:
Assuming that each object can have only 1 parent (And it is probably true because a field called ParentID), then start building the tree from top to bottom.

You will need to get the start of the tree. That would be a DB Record without a parent.
I don't know how you represent a top most unit (parent ID is null, -1 or an ID of an item that does not exist).

After you find it start of the tree then use the following pseudo  code:

OrganisationUnit  startOfTree = getStartOfTreeFromDB();
fillNodeWillChildren(startOfTree);


private void fillNodeWillChildren(OrganisationUnit  node)
{
   List<OrganisationUnit> children = getChildOrganisationUnitsByParentID(node.UniqueKey);

  foreach (vart child in children)
  {
     fillNodeWillChildren(child);
  }
}

private List<OrganisationUnit> getChildOrganisationUnitsByParentID(int ID)
{
   Select from MyTable Where ParentID == ID
  // Create OrganisationUnit items here for each record and add it to a list

  // return that list
}

Open in new window



I'm assuming here that the parent id is the unique key of the parent. (and not the Database ID)

0
 
TommySzalapskiConnect With a Mentor Commented:
It would help if we could see the definition of OrganizationUnit. I assume the following method would work.(I am assuming that the SubOrganizations are the same type and the tree has no limit to its depth).
Create an KeyValuePair list of OrganizationUnits from all your key/parent pairs. (The KeyValuePair thing is just to speed it up, if it scares you, use an array. It will take longer to run, but if you don't need to do it very often it won't matter). Then just go through them one at a time and add the key to the subOrg property of the parent. After you are done, you'll have the whole tree. I assume the root has a NULL for its parent so it should be easy to find. No recursion needed.
0
 
mr-kennyAuthor Commented:
Hi TommySzalapski

Thanks for the help. The OrgUnit Class contains: Id, ParentId, Name and List<OrgUnit> SubOrgUnits.
Could you please write some pseudo code of your solution?

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
mr-kennyAuthor Commented:
Hi saragani,

Thank you, this worked.
I just have to add them to the parent. Thanks a lot

private void fillNodeWillChildren(OrganisationUnit  node)
{
   List<OrganisationUnit> children = getChildOrganisationUnitsByParentID(node.UniqueKey);

//Add them to Parent:
node.SubOrgUnits = children;


  foreach (vart child in children)
  {
     fillNodeWillChildren(child);
  }
}
0
 
saraganiCommented:
Hi, I just see that I wrote vart instead of var :-)
0
 
mr-kennyAuthor Commented:
no Problem,

here my final solution:
thanks again,

        private OrgUnit NestTreeRecursive(List<OrgUnit> orgUnitList)
        {
            //Get Top of Tree
            OrgUnit topOrgUnit = (from o in orgUnitList where o.Level == 1 select o).Single();

            fillNodeWillChildren(topOrgUnit);

            return topOrgUnit;
        }

        private void fillNodeWillChildren(OrgUnit  node)
        {
            List<OrgUnit> children = getChildOrganisationUnitsByParentID(node.TreeId);

            node.SubOrgUnits = children;

            foreach (var child in children)
            {
                fillNodeWillChildren(child);
            }
        }

        private List<OrgUnit> getChildOrganisationUnitsByParentID(int ID)
        {
            List<OrgUnit> children = new List<OrgUnit>();

            children = (from o in OrgUnitsUnnested.Items where o.ParentId == ID orderby o.Name select o).ToList();

            return children;
        }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.