Exemple #1
0
        /// <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]);
            }
        }
Exemple #2
0
        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();
        }
Exemple #3
0
 public TreeNodeEx(string x, TreeNodeEx parent = null)
 {
     Val      = x;
     Children = new List <TreeNodeEx>();
     Parent   = parent;
 }