Example #1
0
        public static List<Node> GetNodes()
        {
            int count = int.Parse(Console.ReadLine());
            var nodesSoFar = new HashSet<int>();
            var nodes = new List<Node>();
            while (count > 1)
            {
                string[] input = Console.ReadLine().Split(' ');
                int parentValue = int.Parse(input[0]);
                int childValue = int.Parse(input[1]);
                var parent = nodes.FirstOrDefault(n => n.Value == parentValue);
                var child = nodes.FirstOrDefault(n => n.Value == childValue);
                if (child == null)
                {
                    child = new Node(childValue);
                    nodes.Add(child);
                }

                if (parent == null)
                {
                    parent = new Node(parentValue);
                    nodes.Add(parent);
                }

                parent.AddChild(child);
                count--;
            }
            return nodes;
        }
Example #2
0
 public void AddChild(Node child)
 {
     this.Children.Add(child);
     child.Parent = this;
 }