/// <summary> /// Load from an unsorted list. /// </summary> /// <param name="list"></param> public Tree(IEnumerable <Element> list, HasParent hasParent, IsParent isParent, IsEqual isEqual) { List <Element> copy = new List <Element>(); IEnumerator <Element> enumerator = list.GetEnumerator(); while (enumerator.MoveNext()) { copy.Add(enumerator.Current); } LoadFromCopy(copy, hasParent, isParent, isEqual); }
public Tree(System.Collections.IEnumerable list, HasParent hasParent, IsParent isParent, IsEqual isEqual) { List <Element> copy = new List <Element>(); System.Collections.IEnumerator enumerator = list.GetEnumerator(); while (enumerator.MoveNext()) { copy.Add((Element)enumerator.Current); } LoadFromCopy(copy, hasParent, isParent, isEqual); }
void LoadFromCopy(List <Element> copy, HasParent hasParent, IsParent isParent, IsEqual isEqual) { while (copy.Count > 0) { int i = 0; while (i < copy.Count) { bool fAdded = false; Element f = copy[i]; if (!hasParent(f)) { mRoot.Children.Add(new Node <Element>(f)); fAdded = true; } else { IEnumerator <Node <Element> > innerenumerator = GetDepthFirstNodeEnumerator(); while (innerenumerator.MoveNext()) { if (isParent(innerenumerator.Current.Data, f)) { innerenumerator.Current.Children.Add(new Node <Element>(f)); fAdded = true; break; } } } if (fAdded) { copy.RemoveAt(i); } else { i++; } } } }