Exemplo n.º 1
0
        public static Dictionary<int, Node> ReadAndCreateTree()
        {
            Dictionary<int, Node> tree = new Dictionary<int, Node>();
            Console.WriteLine("Please enter number of nodes: ");
            int numberOfNodes = int.Parse(Console.ReadLine());
            for (int i = 0; i < numberOfNodes - 1; i++)
            {
                Console.WriteLine("Please enter parent-child pair: ");
                string line = Console.ReadLine();
                string[] parentChildPair = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                int parent = int.Parse(parentChildPair[0]);
                int child = int.Parse(parentChildPair[1]);

                Node parentNode = new Node(parent);
                Node childNode = new Node(child);
                
                if (!tree.ContainsKey(parent))
                {
                    tree.Add(parent, parentNode);
                }

                if (!tree.ContainsKey(child))
                {
                    tree.Add(child, childNode);
                }

                tree[parent].AddChild(tree[child]);
            }

            return tree;
        }
Exemplo n.º 2
0
        public void AddChild(Node child)
        {
            if (this.childrens == null)
            {
                this.childrens = new List<Node>();
            }

            child.hasParent = true;
            this.Childrens.Add(child);
        }
Exemplo n.º 3
0
        private static void FindPathWithSum(Node root, int sum)
        {
            pathWithCurrentSum.Push(root.Value);
            if (pathWithCurrentSum.Sum() == sum)
            {
                Console.WriteLine(string.Join(" -> ", pathWithCurrentSum.Reverse()));
                pathWithCurrentSum.Reverse();
            }

            if (root.Childrens != null)
            {
                foreach (var node in root.Childrens)
                {
                    FindPathWithSum(node, sum);
                    pathWithCurrentSum.Pop();
                }
            }
            else
            {
                return;
            }
        }
Exemplo n.º 4
0
 private static void FindLongPath(Node root)
 {
     currentPath.Push(root.Value);
     if (root.Childrens != null)
     {
         foreach (var node in root.Childrens)
         {
             FindLongPath(node);
             currentPath.Pop();
         }
     }
     else
     {
         if (currentPath.Count > path.Count)
         {
             path.Clear();
             foreach (var item in currentPath)
             {
                 path.Push(item);
             }
         }
     }
 }
Exemplo n.º 5
0
        private static void SubTreesWithSum(Node root)
        {
            currentSubTreePath.Push(root.Value);

            if (root.Childrens == null)
            {
                return;
            }

            foreach (var leaf in root.Childrens)
            {
                SubTreesWithSum(leaf);
            }
        }
Exemplo n.º 6
0
 public Node(int value, Node child) :
     this(value)
 {
     this.AddChild(child);
 }