// Initial tree creation public static TreeGraph CreateTree(string[] Data) { TreeGraph NewTree = new TreeGraph(); List <float> Depth = new List <float>(); List <INode> Nodes = new List <INode>(); // Get each string depth for (int i = 0; i < Data.Length; i++) { float TempDepth = 0; TempDepth += Data[i].Count(character => character == '\t'); TempDepth += Data[i].Substring(0, ReturnCharacterFirstOccurence(Data[i])).Count(character => character == ' ') * 0.125f; Depth.Add(TempDepth); // Create Temporary String string Value = Data[i]; Value = Value.Replace("\t", ""); //Value = Value.Remove(0, ReturnCharacterFirstOccurence(Data[i])); // Add Node Nodes.Add(new Node(Value, TempDepth, Value)); } if (Depth[0] > 0) { Console.WriteLine("FILE IS NOT IN CORRECT FORMAT"); } for (int i = 0; i < Nodes.Count; i++) { // If Depth is 0 then add to root node if (Nodes[i].Depth == 0) { Node.ChangeParentNode(Nodes[i], NewTree.RootNode); } // If Depth is the same as previous node, add to that node's parent else if (Nodes[i].Depth == Nodes[i - 1].Depth) { Node.ChangeParentNode(Nodes[i], Nodes[i - 1].ParentNode); } // If Node depth is greater than previous node, add as a child to the last else if (Nodes[i].Depth > Nodes[i - 1].Depth) { Node.ChangeParentNode(Nodes[i], Nodes[i - 1]); } // if node depth is less than previous, loop through node list starting from previous index and determine close lowest else if (Nodes[i].Depth < Nodes[i - 1].Depth) { for (int j = i - 1; j != 0; j--) { if (Nodes[i].Depth == Nodes[j].Depth) { Node.ChangeParentNode(Nodes[i], Nodes[j].ParentNode); break; } } } } return(NewTree); }
private static void DisplayMenu(TreeGraph Tree, string TreeNumber) { string Option = string.Empty; while (Option.ToUpper() != "RETURN") { List <string> Output = new List <string>(); Console.Clear(); Console.WriteLine("Tree Systems v2.1"); Console.WriteLine("============================================================"); Console.WriteLine(TreeNumber); TreeGraph.DisplayTree(Tree, ref Output); Console.WriteLine("============================================================"); Console.WriteLine("\n\n"); Console.WriteLine("1) Add Node"); Console.WriteLine("2) Remove Node"); Console.WriteLine("3) Move Node"); Console.WriteLine("4) Write Tree"); Console.WriteLine("Type \"RETURN\" to go back to Tree Menu"); // Console.WriteLine(); Console.Write("Input:"); Option = Console.ReadLine().ToUpper(); switch (Option) { case "1": { Console.WriteLine("New Node's Identifier:"); string Identifier = Console.ReadLine(); while (TreeGraph.IdentifierCheck(Identifier, Tree.RootNode)) { Console.WriteLine("Identifier Taken, Use a different one"); Console.WriteLine("Node Identifier:"); Identifier = Console.ReadLine(); } Console.WriteLine("New Node's Value:"); string Value = Console.ReadLine(); // Console.WriteLine("What's the Parent's Identifier?"); string ParentIdentifer = Console.ReadLine(); Console.WriteLine("What's the Parent's value?"); string ParentValue = Console.ReadLine(); if (ParentIdentifer == "Root" && ParentValue == "Root") { TreeGraph.AddNode(new Node(Value, 0, Identifier), TreeGraph.GetNode(ParentIdentifer, ParentValue, Tree), Tree); } else if (TreeGraph.GetNode(ParentIdentifer, ParentValue, Tree) != null) { TreeGraph.AddNode(new Node(Value, TreeGraph.GetNode(ParentIdentifer, ParentValue, Tree).Depth + 1, Identifier), TreeGraph.GetNode(ParentIdentifer, ParentValue, Tree), Tree); } else { Console.WriteLine("Node does not exist!"); } break; } case "2": { Console.WriteLine("What's the Identifier of the node to remove?"); string NodeId = Console.ReadLine(); Console.WriteLine("What's the Value of the node to remove?"); string Value = Console.ReadLine(); if (NodeId == "Root" && Value == "Root") { Tree.RootNode.NodeChildren.Clear(); } else { TreeGraph.RemoveNode(NodeId, Value, Tree); } break; } case "3": { Console.WriteLine("Child's Identifier:"); string Identifier = Console.ReadLine(); Console.WriteLine("Child's Value:"); string Value = Console.ReadLine(); // Console.WriteLine("New Parent's Identifier?"); string ParentIdentifer = Console.ReadLine(); Console.WriteLine("New Parent's value?"); string ParentValue = Console.ReadLine(); if (Identifier == ParentIdentifer && Value == ParentValue) { Console.WriteLine("No operations done since child is equal to parent!"); } else if (Identifier == "Root" && Value == "Root") { Console.WriteLine("You cannot move the Tree's root node!"); } else if (TreeGraph.GetNode(Identifier, Value, Tree) != null && TreeGraph.GetNode(ParentIdentifer, ParentValue, Tree) != null) { Node.ChangeParentNode(TreeGraph.GetNode(Identifier, Value, Tree), TreeGraph.GetNode(ParentIdentifer, ParentValue, Tree)); } break; } case "4": { Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory + TreeNumber + ".txt"); TreeGraph.WriteOutlineFile(TreeNumber, Output.ToArray <string>()); break; } case "RETURN": { Option = "RETURN"; break; } default: { Console.WriteLine("Invalid Input! Please type the number correctly"); break; } } Console.ReadLine(); } }