public static void RemoveNode(INode NodeToRemove, TreeGraph Tree)
 {
     if (TreeGraph.GetNode(NodeToRemove.Identifier, NodeToRemove.Value, Tree) != null)
     {
         NodeToRemove.ParentNode.NodeChildren.Remove(NodeToRemove);
     }
 }
 public static void RemoveNode(string Identifer, string Value, TreeGraph Tree)
 {
     RemoveNode(TreeGraph.GetNode(Identifer, Value, Tree), Tree);
 }
示例#3
0
        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();
            }
        }