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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TommySzalapskiCommented:
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
saraganiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.