tags:

views:

119

answers:

2

We have a large collection of objects that we need to create a tree structure from. However, our problem is that we only know each objects parent...which makes it a bit tricky since this needs to be fast.

What is the best algorithm for creating an tree graph from a collection of objects where only parent node is known?

+6  A: 
Henrik
So sort of like this? List<someobject> millionsOfObjects = List<someobject>();FillMillonsOfObject();for(int i = 0 i< millionsOfObjects.Count; i++){string parentId = millionsOfObjects[i].ParentId; someobject parent = millionsOfObjects.Find(parentId);parent.Children.Add(millionsOfObjects[i]);} the proses needs to be fast and efficient... not sure if this is what we're looking for.
flalar
Yes, this is, what I'm proposing. Edited my answer.
Henrik
A: 

Would this be easier? You don't have to replicate the objects, do you?

List millionsOfObjects = new List&ltSomeObject>(); 
FillMillonsOfObject(); 
foreach(SomeObject someObject in millionsOfObjects) 
{ 
  someObject.GetParent().children.add(someobject)
}
xpda