public static ISimpleTree <T> ToTree <T>(this IList <Tuple <T, T> > pairs) where T : class { var top = pairs.Single(x => x.Item2 == null).Item1; var topNode = new SimpleTree <T>(top); Add(topNode, pairs); return(topNode); }
public static ISimpleTree <T> ToSimpleTree <T>(this IDictionary <T, T> pairs) where T : class { var top = pairs.Single(x => x.Value == null).Key; var topNode = new SimpleTree <T>(top); Add(topNode, pairs); return(topNode); }
private static void Add <T>(SimpleTree <T> node, IDictionary <T, T> pairs) where T : class { var childrenPairs = pairs.Where(pair => pair.Value == node.Data).ToList <KeyValuePair <T, T> >(); foreach (var cp in childrenPairs) { var c = new SimpleTree <T>(cp.Key); node.Add(c); pairs.Remove(cp); Add <T>(c, pairs); } }
private static void Add <T>(SimpleTree <T> node, IList <Tuple <T, T> > pairs) where T : class { var childrenPairs = pairs.Where(tuple => tuple.Item2 == node.Data).ToList <Tuple <T, T> >(); foreach (var cp in childrenPairs) { var c = new SimpleTree <T>(cp.Item1); node.Add(c); pairs.Remove(cp); Add <T>(c, pairs); } }