順序付けされていないツリーを構築するために使用する必要のあるオブジェクト(キーとその親キーを使用してSQLデータベースからロードされた行)の隣接リストがあります。サイクルがないことが保証されています。
これには時間がかかりすぎます(約5分で87万ノードのうち約3Kしか処理されませんでした)。十分なRAMを搭載したワークステーションCore2Duoで実行しています。
これをより速くする方法について何かアイデアはありますか?
public class StampHierarchy {
private StampNode _root;
private SortedList<int, StampNode> _keyNodeIndex;
// takes a list of nodes and builds a tree
// starting at _root
private void BuildHierarchy(List<StampNode> nodes)
{
Stack<StampNode> processor = new Stack<StampNode>();
_keyNodeIndex = new SortedList<int, StampNode>(nodes.Count);
// find the root
_root = nodes.Find(n => n.Parent == 0);
// find children...
processor.Push(_root);
while (processor.Count != 0)
{
StampNode current = processor.Pop();
// keep a direct link to the node via the key
_keyNodeIndex.Add(current.Key, current);
// add children
current.Children.AddRange(nodes.Where(n => n.Parent == current.Key));
// queue the children
foreach (StampNode child in current.Children)
{
processor.Push(child);
nodes.Remove(child); // thought this might help the Where above
}
}
}
}
public class StampNode {
// properties: int Key, int Parent, string Name, List<StampNode> Children
}