// Add child node. Return new child. public NodeTree <T> SpawnChild(Guid key, T nodeData) { var child = new NodeTree <T>(key, nodeData); this.children.Add(child); return(child); }
private static List<NodeTree<int>> ReadAndParseInput() { List<NodeTree<int>> nodes = new List<NodeTree<int>>(); int edges = int.Parse(Console.ReadLine()); for (int i = 0; i < edges - 1; i++) { int[] values = Console.ReadLine() .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(value => int.Parse(value)) .ToArray(); var parent = nodes.FirstOrDefault(node => values[0] == node.Value); if (parent == null) { parent = new NodeTree<int>(values[0]); nodes.Add(parent); } var child = nodes.FirstOrDefault(node => values[1] == node.Value); if (child == null) { child = new NodeTree<int>(values[1]); nodes.Add(child); } parent.AddChild(child); } return nodes; }
public void TraversePrint(NodeTree <T> node, TreeVisitor <string> visitor, int depth) { visitor(new string(' ', depth * 3) + "|---[" + node.Value + "]"); foreach (NodeTree <T> kid in node.Children) { TraversePrint(kid, visitor, depth + 1); } }
// Selects random node (at bottom of tree) public NodeTree <T> SelectRandomNode(NodeTree <T> node) { if (node.children.Count() == 0) { return(node); } return(node.SelectRandomNode(node.TraversToRandomChild())); }
// Find gretest depth. Travers each path and return the max public int GreatestDepth(NodeTree <T> node) { if (node.children == null || !node.children.Any()) { return(1); } return(1 + node.children.Max(n => GreatestDepth(n))); }
private static void GetPathsFromNode(NodeTree<int> node, Stack<NodeTree<int>> stack, List<NodeTree<int>[]> paths) { stack.Push(node); paths.Add(stack.ToArray()); foreach (var child in node.Children) { GetPathsFromNode(child, stack, paths); } stack.Pop(); }
private static void GetPath(NodeTree<int> node, Stack<NodeTree<int>> path, List<NodeTree<int>[]> paths) { path.Push(node); foreach (var child in node.Children) { GetPath(child, path, paths); } if (!node.HasChildren) paths.Add(path.ToArray()); path.Pop(); }
// Generate Random Tree of depth N public void GenrateTree(int depth, NodeTree <T> node, T nodeData) { if (depth <= 1) { return; } int randomChildCount = rand.Next(1, 5); for (int i = 0; i < randomChildCount; i++) { node.SpawnChild(Guid.NewGuid(), nodeData); } // ToList used to resolve "Collection was modified; enumeration operation may not execute." node.children.ToList().ForEach(n => GenrateTree(depth - 1, n, nodeData)); }
// Loop through each child and traverse, reapeat untill found. Null returned if not found public NodeTree <T> FindNode(NodeTree <T> node, Guid targetKey) { if (node.keypair.Key == targetKey) { return(node); } foreach (NodeTree <T> child in node.children) { var result = FindNode(child, targetKey); if (result != null) { return(result); } } return(null); }
private static int GetSubtreeSum(int sum, NodeTree<int> node, List<NodeTree<int>> roots) { if (!node.HasChildren) { if (node.Value == sum) roots.Add(node); return node.Value; } else { int subtreeSum = node.Value; foreach (var child in node.Children) { subtreeSum += GetSubtreeSum(sum, child, roots); } if (subtreeSum == sum) roots.Add(node); return subtreeSum; } }