Building a hierarchy from flat data?

  • Thread starter Thread starter Gustaf Liljegren
  • Start date Start date
G

Gustaf Liljegren

My input data consists of a list of parent-child relationships. One
item in this list can be modelled like this:

+---------+
| Item |
+---------+
| Parent |
| Child |
+---------+

The Parent and Child properties are unique strings. One item's child
may be another item's parent and so on, so all together the list of
Item objects can be transformed into a hiearchy of Item objects. The
question is HOW.

I imagine a collection class called Item, with the List containing
other Item objects (since one parent can have many children).

I didn't start coding yet, becuase I have no idea how to approach the
problem. Hope someone can help me outlining the procedure.

Gus
 
Hi,

If you want to create structure that support hierarchical
relationship then your "Item" should be more like:

+----------+
| Item |
+----------+
| Id |
| ParentId |
+----------+

You can not point to child item, because in common hierarchy
one parent can have zero or more children, and a child can have only
single parent (or none).

If you want to support more complicated hierarchy (e.g. human's
hierarchy) then you should extend the Item's structure or provide
additional structure:

+----------+
| ItemLink |
+----------+
| ParentId |
| ChildId |
+----------+

Regards

Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top