/// <param name="parent"></param> /// <param name="sequence">First one is the parent: "D4,G7,I9"</param> private static void BuildTree(TreeNodeEx parent, string sequence) { if (parent == null || string.IsNullOrEmpty(sequence)) { return; } var splited = sequence.Split(new[] { _delimeter }, StringSplitOptions.RemoveEmptyEntries); if (splited.Length <= 1) { return; } // Skip parent at the start parent.Children.AddRange(splited.Skip(1).Select(ch => new TreeNodeEx(ch, parent))); foreach (var child in parent.Children) { if (!_data.ContainsKey(child.Val)) { continue; } BuildTree(child, _data[child.Val]); } }
public static void Print(List <string> data) { if (data == null) { return; } _data = data.ToDictionary(x => x.Split(new[] { _delimeter }, StringSplitOptions.RemoveEmptyEntries)[0], y => y); string rootVal = GetRoot(); if (string.IsNullOrEmpty(rootVal)) { return; } string rootSequence = data.FirstOrDefault(x => x.StartsWith(rootVal, StringComparison.InvariantCultureIgnoreCase)); // Build Tree from the root TreeNodeEx root = new TreeNodeEx(rootVal); BuildTree(root, rootSequence); // Visualize nodes root.Visualize(); }
public TreeNodeEx(string x, TreeNodeEx parent = null) { Val = x; Children = new List <TreeNodeEx>(); Parent = parent; }