public static void Main(string[] args) { int nodeCount = int.Parse(Console.ReadLine()); var tree = new Tree(); for (int i = 1; i < nodeCount; i++) { var edge = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var parentValue = edge[0]; var parentNode = tree.GetTreeNodeByValue(parentValue); var chieldValue = edge[1]; var chieldNode = tree.GetTreeNodeByValue(chieldValue); chieldNode.Parent = parentNode; parentNode.Children.Add(chieldNode); } var pathSum = int.Parse(Console.ReadLine()); var subreeSum = int.Parse(Console.ReadLine()); var rootNode = tree.FindRootNode(); Console.WriteLine("First Node: {0}", rootNode.Value); var leafs = tree.FindAllLeafs().Select(l => l.Value); Console.WriteLine("Leaf nodes: {0}", string.Join(", ", leafs)); var middleNodes = tree.FindAllMiddleNodes().Select(l => l.Value); Console.WriteLine("Middle nodes: {0}", string.Join(", ", middleNodes)); var longesPath = tree.FindLongestPath().Select(l => l.Value); Console.WriteLine("Longest path: {0} (length = {1})", string.Join(" -> ", longesPath), longesPath.Count()); var sumPaths = tree.FindAllPathsWithGivenSum(pathSum); Console.WriteLine("Path of sum: {0}", pathSum); foreach (var sumPath in sumPaths) { var path = sumPath.Select(p => p.Value); Console.WriteLine("{0}", string.Join(" -> ", path)); } }
static void Main() { int nodesCount = int.Parse(Console.ReadLine()); var tree = new Tree(); for (int i = 1; i < nodesCount; i++) { string[] edge = Console.ReadLine().Split(' '); int parentValue = int.Parse(edge[0]); Tree parentNode = tree.GetTreeNodeByVlue(parentValue); int childValue = int.Parse(edge[1]); Tree childNode = tree.GetTreeNodeByVlue(childValue); parentNode.Children.Add(childNode); childNode.Parent = parentNode; } int pathSum = int.Parse(Console.ReadLine()); int subtreeSum = int.Parse(Console.ReadLine()); var middleNodes = tree.FindMiddleNodes().Select(m => m.Value); var leafs = tree.FindAllLeafs().Select(l => l.Value); var longestPath = tree.FindLongestPath().Select(l => l.Value); var sumOfElemetns = tree.PathsOfSum(pathSum); foreach (var sumOfElement in sumOfElemetns) { var path = sumOfElement.Select(s => s.Value); Console.WriteLine(string.Join(", ", path)); } }
static Tree<int> GetNodeByValue(int value) { if (!nodeByValue.ContainsKey(value)) { nodeByValue[value] = new Tree<int>(value); } return nodeByValue[value]; }
private List<Tree> FindLongestPath(Tree node, List<Tree> elements, List<Tree> bestElements) { var children = node.Children; foreach (var child in children) { elements.Add(child); if (child.Children.Count != 0) { bestElements = this.FindLongestPath(child, elements, bestElements); } if (elements.Count > bestElements.Count) { bestElements = elements; } elements = new List<Tree>(); } return bestElements; }
private static List<Tree> GetElementsSoFar(Tree child) { var elements = new List<Tree>(); var parent = child.Parent; while (parent != null) { elements.Insert(0, parent); parent = parent.Parent; } return elements; }
private List<List<Tree>> PathsOfSum(Tree firstNode, List<Tree> currentElements, List<List<Tree>> sumOfElements, int result) { var children = firstNode.Children; foreach (var child in children) { currentElements.Add(child); if (currentElements.Sum(e => e.Value) == result) { sumOfElements.Add(currentElements); currentElements = GetElementsSoFar(child); } if (child.Children.Count != 0) { sumOfElements = this.PathsOfSum(child, currentElements, sumOfElements, result); } currentElements = GetElementsSoFar(child); } return sumOfElements; }
static int TreeSum(Tree<int> tree) { return tree.Value + tree.Children.Select(s => s.Value).Aggregate((a, b) => a + b); }
static void PrintTree(Tree<int> tree) { Console.WriteLine(tree.Value); foreach (var child in tree.Children) { Console.WriteLine(" " + child.Value); } }
static int PathSum(Tree<int> leaf) { int sum = 0; var temp = leaf; while (temp.Parent != null) { sum += temp.Value; temp = temp.Parent; } sum += temp.Value; return sum; }
private static void FindLongestPath(Tree<int> tree, int depth = 1) { if (depth > longestPath) { longestPath = depth; longestPathLeaf = tree; } foreach (var child in tree.Children) { FindLongestPath(child, depth + 1); } }
private static void PrintAllPathsWithSum(Tree<int> tree, int sum) { if (sum == pathSum) { var path = GetPathValues(tree); Console.WriteLine(string.Join(" -> ", path)); } foreach (var child in tree.Children) { PrintAllPathsWithSum(child, sum + child.Value); } }
private static IEnumerable<int> GetPathValues(Tree<int> tree) { var result = new Stack<int>(); while (tree != null) { result.Push(tree.Value); tree = tree.Parent; } return result; }
/// <summary> /// Finds all paths witch sum is equal to given sum using recursion. /// </summary> /// <param name="sum">Sum to be found.</param> /// <param name="node">Given child for start point.</param> /// <param name="elements">Current path.</param> /// <param name="bestElements">All paths equal to the sum.</param> /// <returns>All paths equal to the sum.</returns> private List<List<Tree>> FindAllPathsWithGivenSum(int sum, Tree node, List<Tree> elements, List<List<Tree>> bestElements) { var children = node.Children; foreach (var child in children) { elements.Add(child); if (elements.Sum(e => e.Value) == sum) { bestElements.Add(elements); elements = GetElementsSoFar(child); } if (child.Children.Count != 0) { bestElements = this.FindAllPathsWithGivenSum(sum, child, elements, bestElements); } elements = GetElementsSoFar(child); } return bestElements; }