예제 #1
0
파일: BTree.cs 프로젝트: hugheshe1/Project5
        /// <summary>
        /// Method for adding a value to the BTree
        /// </summary>
        /// <param name="value">Represents the value to be added</param>
        /// <returns>Boolean representing if the value was added successfully or not</returns>
        public bool AddValue(int value)
        {
            #region Initialize first value and Root

            if (NodeCount == 0)
            {
                Index FirstIndex = new Index(NodeSize);
                Leaf  FirstLeaf  = new Leaf(NodeSize);

                //Add value to the first Index and Leaf
                FirstIndex.Items.Add(value);
                FirstLeaf.Items.Add(value);

                //Reference initial and first Leaf
                FirstIndex.LeafList.Add(new Leaf(NodeSize));
                FirstIndex.LeafList.Add(FirstLeaf);

                //Set IndexLevel
                FirstIndex.IndexLevel = 0;

                //Add index to IndexList and Root
                Root = new Index(FirstIndex);


                //Increment Counts
                NodeCount += 3;
                TreeIndexes++;
                TreeLeaves += 2;

                return(true);
            }

            #endregion

            #region Attempt to put value into a Leaf

            else
            {
                //Find Leaf to insert value
                Leaf   LeafToFill = FindLeaf(value);
                INSERT response   = LeafToFill.Insert(value);
                if (response == INSERT.DUPLICATE)
                {
                    //Do nothing since you need a unique new value
                    return(false);
                }
                else if (response == INSERT.NEEDSPLIT)
                {
                    //Split Leaf and Indexes if needed
                    SplitLeaf(LeafToFill);
                    return(true);
                }
                else
                {
                    //Success!
                    return(true);
                }
            }

            #endregion
        }
예제 #2
0
        /// <summary>
        /// Main method for the BTreeDriver class which is used for handling method calls for menu and BTree functionality
        /// </summary>
        static void Main()
        {
            ConsoleStartUp();

            //Main Menu
            while (selection != 5)
            {
                try
                {
                    Clear();
                    MenuDialog();

                    switch (selection)
                    {
                        #region Case 1

                    case 1:
                        Clear();
                        Write("What is the arity of the tree to be created? ");
                        response = Convert.ToInt16(ReadLine());
                        tree     = new BTree(response);
                        FillTree();
                        WriteLine($"The tree has been built; {totalAdded} values were added in {totalAddAttempts} loops.");
                        WriteLine("\n\n\n\nPress any key to continue...");
                        ReadKey();
                        break;

                        #endregion

                        #region Case 2

                    case 2:
                        Clear();
                        if (tree != null)
                        {
                            List <string> Output = tree.DisplayTree();
                            WriteLine("===============================================");
                            for (int i = 0; i < Output.Count; i++)
                            {
                                WriteLine($"\n{Output[i]}");
                                WriteLine("===============================================");
                                Thread.Sleep(100);
                            }
                            WriteLine(tree.Stats());
                            WriteLine($"Number of Values Added: {totalAdded}");
                            ReadKey();
                        }
                        break;

                        #endregion

                        #region Case 3

                    case 3:
                        WriteLine("What value do you want to add to the tree? ");
                        response = Convert.ToInt16(ReadLine());

                        //ToDo: add validation for input

                        if (tree.AddValue(response))
                        {
                            WriteLine($"{response} was added to the tree.");
                        }
                        else
                        {
                            WriteLine($"{response} was not added to the tree.");
                        }
                        WriteLine("\n\n\n\nPress any key to continue...");
                        ReadKey();
                        break;

                        #endregion

                        #region Case 4

                    case 4:
                        WriteLine("What value do you want to find? ");
                        response = Convert.ToInt16(ReadLine());
                        Leaf leaf  = null;
                        bool match = tree.FindValue(response, out leaf);

                        Clear();

                        if (match)
                        {
                            WriteLine($"{response} was found in the tree.");
                        }
                        else
                        {
                            WriteLine($"{response} was not found in the tree.");
                        }

                        //Display Nodes Traveled
                        WriteLine($"Nodes Traveled: ");
                        for (int i = 0; i < tree.GetNodesTraveled.Count; i++)
                        {
                            WriteLine($"{tree.GetNodesTraveled[i]}");
                            ReadKey();
                        }

                        WriteLine("\n\n\n\nPress any key to continue...");
                        ReadKey();
                        break;

                        #endregion

                        #region Default Case

                    default:
                        WriteLine("Error: Your selection needs to be an integer 1-5");
                        break;

                        #endregion
                    }
                }
                catch (Exception e)
                {
                    Clear();
                    WriteLine(e.Message);
                }
            }
        }