public void AddChild(Node child) { if (this.children == null) { this.children = new List<Node>(); } this.children.Add(child); }
private static void ParseTree(Dictionary<int, Node> dictionary) { int pairs = int.Parse(Console.ReadLine()); for (int i = 0; i < pairs - 1; i++) { string[] input = Console.ReadLine().Split(); int firstValue = int.Parse(input[0]); int secondValue = int.Parse(input[1]); if (dictionary.ContainsKey(firstValue)) { Node node = dictionary[firstValue]; Node secondNode; if (dictionary.ContainsKey(secondValue)) { secondNode = dictionary[secondValue]; secondNode.HasParent = true; } else { secondNode = new Node(secondValue); dictionary.Add(secondValue, secondNode); } node.AddChild(secondNode); } else { Node secondNode; if (dictionary.ContainsKey(secondValue)) { secondNode = dictionary[secondValue]; secondNode.HasParent = true; } else { secondNode = new Node(secondValue); dictionary.Add(secondValue, secondNode); } Node firstNode = new Node(firstValue, secondNode); dictionary.Add(firstValue, firstNode); } } }
private static List<Node> FindMiddleNodes(Node root) { List<Node> middle = new List<Node>(); List<Node> children = root.GetChildren; if (children.Count > 0 && root.HasParent == true) { middle.Add(root); } foreach (var item in children) { middle.AddRange(FindMiddleNodes(item)); } return middle; }
private static List<Node> FindLeafNodes(Node root) { List<Node> leafs = new List<Node>(); List<Node> children = root.GetChildren; if (children.Count == 0) { leafs.Add(root); } foreach (var item in children) { leafs.AddRange(FindLeafNodes(item)); } return leafs; }
private static List<Node> LongestPath(Node root, ref int steps, ref int maxSteps) { List<Node> path = new List<Node>(); steps++; if (maxSteps < steps) { maxSteps = steps; path.Add(root); } foreach (var item in root.GetChildren) { path.AddRange(LongestPath(item, ref steps, ref maxSteps)); } steps--; return path; }
private static void PathsWithSumS(Node root, List<List<Node>> listOfPaths, List<Node> pathSoFar, int currentSum, int targetSum) { if (currentSum == targetSum && CheckCurrentSum(pathSoFar, targetSum)) { Node[] nodePath = new Node[pathSoFar.Count]; pathSoFar.CopyTo(nodePath); listOfPaths.Add(new List<Node>(nodePath)); pathSoFar.RemoveRange(1, pathSoFar.Count - 1); } foreach (var item in root.GetChildren) { pathSoFar.Add(item); PathsWithSumS(item, listOfPaths, pathSoFar, currentSum + item.Value, targetSum); if (pathSoFar.Count > 1) { pathSoFar.RemoveAt(pathSoFar.Count - 1); } } }
private static void PrintTree(Node node) { foreach (var item in node.GetChildren) { Console.Write("{0} ", item.Value); PrintTree(item); } }
private static int CheckSubTreeSum(Node subRoot, int sum) { foreach (var node in subRoot.GetChildren) { sum += CheckSubTreeSum(node, node.Value); } return sum; }
private static void SubTreeWithSumS(Node root, List<Node> listOfSubTrees, int targetSum) { foreach (var item in root.GetChildren) { if (targetSum == CheckSubTreeSum(item, item.Value)) { listOfSubTrees.Add(item); } SubTreeWithSumS(item, listOfSubTrees, targetSum); } }
public Node(int value, Node child) : this(value) { this.AddChild(child); this.hasParent = false; }