Joan Venge
Joan Venge

Reputation: 330952

Collections for hierarchies

In the app I am writing, I am trying to find a way to store hierarchies effectively. Here is an example.

At the bottom, you can see the nodes to be stored. Should I use multi dimensional lists? That doesn't seem very optimal, right? I was thinking holding references like so:

node.Parent
node.Children { collection }

Anyone has experience with this kind of stuff?

Upvotes: 0

Views: 297

Answers (3)

Lucero
Lucero

Reputation: 60190

If not all items are of the same type, I may use an abstract base class for a linked list and children collections in such a situation.

Upvotes: 1

Chad Ruppert
Chad Ruppert

Reputation: 3680

This is a fairly basic implementation of a tree, yes. If choose to make your collection for the children an IList or IEnumable or ArrayList, etc is up to you.

I would strongly suggest you build a generic implementation instead of one typed to your domain model, however that is up to you.

Upvotes: 1

Brian Genisio
Brian Genisio

Reputation: 48127

Yeah. You have the right idea. If you need a two-directional hierarchy, I wouldn't use a multi-dimensional list... I would add a node to the tree and each node contains a parent and a collection of children.

You are on the right track.

Upvotes: 1

Related Questions