static void Main(string[] args)
        {
            DirectoryInfo dir = new DirectoryInfo("../..");
            Folder fold = new Folder(dir.Name, null, null);

            Tree<Folder> folders = new Tree<Folder>(fold);
            DirectoryTraversal(folders, dir);
        }
        static Tree<int> GetTreeNodeByValue(int value)
        {
            if (! nodeByValue.ContainsKey(value))
            {
                nodeByValue[value] = new Tree<int>(value);
            }

            return nodeByValue[value];
        }
Exemplo n.º 3
0
        public static Tree GetTreeNodeByValue(int value)
        {
            if (!NodeByValue.ContainsKey(value))
            {
                NodeByValue[value] = new Tree(value);
            }

            return NodeByValue[value];
        }
Exemplo n.º 4
0
        private static void FindMiddleNodes(List<int> middleNodes, Tree<int> node)
        {
            if (node.Children.Count != 0 && node.Parent != null)
            {
                middleNodes.Add(node.Value);
            }

            foreach (var child in node.Children)
            {
                FindMiddleNodes(middleNodes, child);
            }
        }
Exemplo n.º 5
0
        private static void FindLeafNodes(List<int> leafNodes, Tree<int> node)
        {
            if (node.Children.Count == 0)
            {
                leafNodes.Add(node.Value);
            }

            foreach (var child in node.Children)
            {
                FindLeafNodes(leafNodes, child);
            }
        }
Exemplo n.º 6
0
        private static void FindLongestPath(Tree<int> node, int currentDepth)
        {
            int depth = currentDepth;

            if (depth > longestPath && node.Children.Count == 0)
            {
                longestPath = depth;
                longestPathLeaf = node;
            }

            foreach (var child in node.Children)
            {
                FindLongestPath(child, depth + 1);
            }
        }
Exemplo n.º 7
0
        private static void FindAllPaths(Tree<int> node, int sum)
        {
            tempPath.Push(node);
            foreach (var child in node.Children)
            {
                FindAllPaths(child, sum);
            }

            int tempPathSum = tempPath.Sum(n => n.Value);
            if (tempPathSum == sum)
            {
                var foundPath = tempPath.ToList();
                foundPath.Reverse();
                pathsWithGivenSum.Add(foundPath);
            }

            tempPath.Pop();
        }
Exemplo n.º 8
0
        private static void Main(string[] args)
        {
            Tree<int> testTree = new Tree<int>();

            int nodeCount = int.Parse(Console.ReadLine());

            for (int node = 1; node < nodeCount; node++)
            {
                string[] edge = Console.ReadLine().Split();
                int parentValue = int.Parse(edge[0]);
                int childValue = int.Parse(edge[1]);
                testTree.MakeConnectionNodes(parentValue, childValue);
            }
            int pathSum = int.Parse(Console.ReadLine());

            TreeTraverseUtils.PrintTree(testTree.Nodes);
            TreeTraverseUtils.GetLongestPathInTree(testTree.RooTNode);
            TreeTraverseUtils.GetPathWithGivenSum(testTree.RooTNode, pathSum);
        }
Exemplo n.º 9
0
        private static void FindAllPathsWithGiveSum(Tree<int> node, int sum, int currentSum)
        {
            currentSum += node.Value;

            if (currentSum == sum)
            {
                PrintPathFoSum(node, sum);
            }

            if (currentSum > sum)
            {
                return;
            }

            foreach (var child in node.Children)
            {
                FindAllPathsWithGiveSum(child, sum, currentSum);

            }
        }
        public static void DirectoryTraversal(Tree<Folder> folder, DirectoryInfo dir)
        {
            File[] files = new File[dir.GetFiles().Length];
            FileInfo[] filesInfo = dir.GetFiles();
            for (int i = 0; i < filesInfo.Length; i++)
            {
                files[i] = new File(filesInfo[i].Name, filesInfo[i].Length);
            }

            folder.Value.Files = files;
            List<Folder> childFolders = new List<Folder>();
            foreach (var directory in dir.GetDirectories())
            {
                Folder fold = new Folder(directory.Name, null, null);
                Tree<Folder> node = new Tree<Folder>(fold);
                DirectoryTraversal(node, directory);
                folder.Children.Add(node);
                childFolders.Add(fold);
                node.Parent = folder;
            }

            folder.Value.ChildFolders = childFolders.ToArray();
        }
Exemplo n.º 11
0
        private static List<Tree<int>> FindTheLongestPath(Tree<int> rootNode)
        {
            List<Tree<int>> path = new List<Tree<int>>();

            foreach (var child in rootNode.Children)
            {
                List<Tree<int>> tempPath = FindTheLongestPath(child);
                if (tempPath.Count > path.Count)
                {
                    path = tempPath;
                }
            }

            path.Insert(0, rootNode);

            return path;
        }
Exemplo n.º 12
0
        private static void FindSubtree(List<int> subtree, Tree<int> node)
        {
            subtree.Add(node.Value);

            if (subtree.Sum() == subtreeSum)
            {
                Console.WriteLine("{0}", string.Join(" + ", subtree));
                return;
            }

            if (node.Parent != null)
            {
                foreach (var child in node.Children)
                {
                    FindSubtree(subtree, child);
                }
            }
        }
Exemplo n.º 13
0
        private static void PrintPathFoSum(Tree<int> node, int sum)
        {
            int currentSum = 0;
            Tree<int> currentNode = node;
            Stack<int> path = new Stack<int>();

            while (currentSum < sum)
            {
                path.Push(currentNode.Value);
                currentSum += currentNode.Value;
                currentNode = currentNode.Parent;
            }

            Console.WriteLine(string.Join(" -> ", path));
        }
        static IList<Tree<int>> FindLongestPath(Tree<int> treeNode)
        {
            IList<Tree<int>> longestPath = new List<Tree<int>>();
            foreach (var childNode in treeNode.Children)
            {
                var currentPath = FindLongestPath(childNode);
                if (currentPath.Count > longestPath.Count)
                {
                    longestPath = currentPath;
                }
            }

            longestPath.Add(treeNode);
            return longestPath;
        }
        static List<Tree<int>> FindSubtreesWithSum(Tree<int> root, int targetSum)
        {
            var results = new List<Tree<int>>();
            var currentSum = FindTreeSum(root);
            if (currentSum == targetSum)
            {
                results.Add(root);
            }

            foreach (var child in root.Children)
            {
                results.AddRange(FindSubtreesWithSum(child, targetSum));
            }

            return results;
        }
        static int FindTreeSum(Tree<int> node)
        {
            int sum = node.Value;
            foreach (var child in node.Children)
            {
                sum += FindTreeSum(child);
            }

            return sum;
        }