public TreeOfNodes <T> Remove(T element) { TreeOfNodes <T> removedElement = null; for (int i = 0; i < this.descendants.Count; i++) { if (this.descendants[i].Value.Equals(element)) { removedElement = this.descendants[i]; this.descendants.RemoveAt(i); this.size--; break; } else if (this.descendants[i].Size > 1) { removedElement = this.descendants[i].Remove(element); if (removedElement != null) { this.size--; break; } } } return(removedElement); }
public static IList <TreeOfNodes <T> > CalculateLongestPath <T>(TreeOfNodes <T> tree) where T : IComparable <T> { List <TreeOfNodes <T> > chain = new List <TreeOfNodes <T> >(); chain.Add(tree); if (tree.Size > 1) { List <TreeOfNodes <T> > subChain = new List <TreeOfNodes <T> >(); foreach (TreeOfNodes <T> descendant in tree.DirectDescendants) { List <TreeOfNodes <T> > descendantChain = new List <TreeOfNodes <T> >(CalculateLongestPath(descendant)); if (descendantChain.Count > subChain.Count) { subChain = descendantChain; } } chain.AddRange(subChain); } return(chain); }
public TreeOfNodes <T> Add(TreeOfNodes <T> tree) { if (this.Size > 0) { tree.Parent = this; this.descendants.Add(tree); this.size++; } return(tree); }
private static Queue <TreeOfNodes <T> > ParseUserInput <T>(T template, char[] splitChars) where T : IComparable <T> { try { Clipboard.SetText(File.ReadAllText("sampleInput.txt")); helper.ConsoleMio.WriteLine( "You can paste the sample input copied to your clipboard", ConsoleColor.DarkRed); } catch (IOException) { } Queue <TreeOfNodes <T> > createdNodes = new Queue <TreeOfNodes <T> >(); HashSet <T> distinctNodes = new HashSet <T>(); helper.ConsoleMio.Write("Enter total count of nodes N = ", ConsoleColor.DarkRed); int remainingNodes = int.Parse(helper.ConsoleMio.ReadInColor(ConsoleColor.DarkBlue)); while (remainingNodes > 0) { helper.ConsoleMio.Write( "Remaining Nodes(Repeating nodes are exluded): ", ConsoleColor.DarkCyan); helper.ConsoleMio.WriteLine(remainingNodes.ToString(), ConsoleColor.DarkRed); helper.ConsoleMio.Write("Enter a sequence of nodes (first will be root): ", ConsoleColor.DarkCyan); int newNodesCount; T[] nodesToBe = GetNodesToBe(splitChars, distinctNodes, out newNodesCount); if (newNodesCount > remainingNodes) { throw new ApplicationException( nodesToBe.Length + " Nodes were entered, but there is only space for " + remainingNodes + " nodes"); } remainingNodes -= newNodesCount; var rootNode = new TreeOfNodes <T>(nodesToBe[0]); for (int i = 1; i < nodesToBe.Length; i++) { rootNode.Add(nodesToBe[i]); } createdNodes.Enqueue(rootNode); } return(createdNodes); }
public static IList <TreeOfNodes <T> > GetLeafNodes <T>(TreeOfNodes <T> tree) where T : IComparable <T> { List <TreeOfNodes <T> > leafs = new List <TreeOfNodes <T> >(); if (tree.Size > 1) { foreach (TreeOfNodes <T> descendant in tree.DirectDescendants) { leafs.AddRange(GetLeafNodes <T>(descendant)); } } else { leafs.Add(tree); } return(leafs); }
public TreeOfNodes <T> Add(T element) { TreeOfNodes <T> result; if (this.size == 0) { this.Value = element; result = this; } else { result = new TreeOfNodes <T>(element) { parent = this }; this.descendants.Add(result); } this.size++; return(result); }
public static IList <TreeOfNodes <T> > GetMiddleNodes <T>(TreeOfNodes <T> tree) where T : IComparable <T> { List <TreeOfNodes <T> > middleNodes = new List <TreeOfNodes <T> >(); if (tree.Parent != null && tree.Size > 1) { middleNodes.Add(tree); } else if (tree.Size > 1) { foreach (TreeOfNodes <T> descendant in tree.DirectDescendants) { IList <TreeOfNodes <T> > descendantMiddleNodes = GetMiddleNodes(descendant); if (descendantMiddleNodes.Count > 0) { middleNodes.AddRange(descendantMiddleNodes); } } } return(middleNodes); }
public static TreeOfNodes <T> AssociateNodes <T>(Queue <TreeOfNodes <T> > availableNodes) where T : IComparable <T> { TreeOfNodes <T> rootTree = availableNodes.Dequeue(); // Max possible itreations if there are unmached elements at this point // They will never match so brake the loop int remainingIterations = availableNodes.Count * availableNodes.Count; while (availableNodes.Count > 0 && remainingIterations > 0) { TreeOfNodes <T> tree = availableNodes.Dequeue(); TreeOfNodes <T> parent = rootTree.Contains(tree.Value); if (parent != null) { parent.AddMany(tree.DirectDescendants); } else { parent = tree.Contains(rootTree.Value); if (parent != null) { parent.AddMany(rootTree.DirectDescendants); rootTree = tree; } else { availableNodes.Enqueue(tree); } } remainingIterations--; } return(rootTree); }
/// <summary> /// Attempts to find the given value in all tree nodes (including self) /// </summary> /// <param name="element">The searched element</param> /// <returns> /// Returns The TreeNode which value is equal to <paramref name="element"/> or null if /// it doesn't exist /// </returns> public TreeOfNodes <T> Contains(T element) { if (this.Value.Equals(element)) { return(this); } else if (this.descendants.Count > 0) { foreach (TreeOfNodes <T> descendant in this.descendants) { TreeOfNodes <T> result = descendant.Contains(element); if (result != null) { return(result); } } return(null); } else { return(null); } }
private static void Main() { helper.ConsoleMio.Setup(); helper.ConsoleMio.PrintHeading("Task 1 Tree Of N Nodes"); Queue <TreeOfNodes <int> > availableNodes = ParseUserInput( template: 0, splitChars: new[] { ' ' }); TreeOfNodes <int> tree = TreeProcessing.AssociateNodes(availableNodes); Console.WriteLine(); helper.ConsoleMio.WriteLine("Nodes Structure\n{node} -> {subNodes}", ConsoleColor.DarkBlue); Console.WriteLine(tree.ToString()); helper.ConsoleMio.WriteLine("Root Node: {0}", ConsoleColor.DarkGreen, tree.Value); IList <TreeOfNodes <int> > leafNodes = TreeProcessing.GetLeafNodes(tree); helper.ConsoleMio.WriteLine( "Leaf Nodes: {0}", ConsoleColor.DarkGreen, string.Join(", ", leafNodes)); IList <TreeOfNodes <int> > middleNodes = TreeProcessing.GetMiddleNodes(tree); helper.ConsoleMio.WriteLine( "Middle Nodes: {0}", ConsoleColor.DarkGreen, string.Join(", ", middleNodes.Select(n => n.Value))); IList <TreeOfNodes <int> > longestPath = TreeProcessing.CalculateLongestPath(tree); helper.ConsoleMio.WriteLine( "Longest Path: {0}", ConsoleColor.DarkGreen, string.Join(" -> ", longestPath.Select(n => n.Value))); }
public TreeOfNodes(TreeOfNodes <T> parent) : this() { this.parent = parent; }