Beispiel #1
0
		}//End BTree (int)

		#endregion Constructors

		#region Methods

		/// <summary>
		/// Adds the value.
		/// </summary>
		/// <param name="nValueToAdd">The n value to add.</param>
		/// <returns>If value was successfully added</returns>
		public bool AddValue (int nValueToAdd)
		{
			if (FindValue (nValueToAdd))
			{
				return false;
			}//End if statement
			 //Used for the start of the program when root is a leaf
			if (Root.IsLeaf)
			{
				Root = (Leaf) stack.Pop ( );
				INSERT temp = Root.Insert (nValueToAdd);
				switch (temp)
				{
					case INSERT.NEEDSPLIT:
						Leaf tempLeaf = (Leaf) Root;
						Root = new Index (NodeSize);
						SplitLeaf (tempLeaf, nValueToAdd);
						stack.Push (Root);
						IndexCount++;
						Index tempIndex = (Index) Root;
						break;

					case INSERT.SUCCESS:
						stack.Push (Root);
						break;

					default:
						break;
				}//End switch statement
			}//End if statement	TEST
			else
			{
				Leaf addLeaf = FindLeaf (nValueToAdd);
				INSERT temp = addLeaf.Insert (nValueToAdd);
				switch (temp)
				{
					case INSERT.NEEDSPLIT:
						;
						SplitLeaf (addLeaf, nValueToAdd);
						break;

					case INSERT.SUCCESS:
						break;

					default:
						break;
				}//End switch statement
			}//End else statement TEST
			if (!Root.IsLeaf || trace)
			{
				ReturnToTop ( );
			}//End if statement
			Count++;
			return true;
		}//End AddValue (int)
Beispiel #2
0
		}//End SplitIndex (Index, Node, int)

		 /// <summary>
		 /// Splits the leaf.
		 /// </summary>
		 /// <param name="leaf">The leaf.</param>
		 /// <param name="nInsertedValue">The n inserted value.</param>
		public void SplitLeaf (Leaf leaf, int nInsertedValue)
		{
			Index newRoot = (Index) Root;
			Leaf leafLess = new Leaf (NodeSize);
			Leaf leafMore = new Leaf (NodeSize);
			List<int> values = new List<int> (NodeSize);
			double half = (double) (NodeSize) / 2.0;
			int postion = (int) Math.Ceiling (half) - 1;
			int numberofSplit;
			for (int i = 0 ; i < NodeSize - 1 ; i++)
			{
				values.Add (leaf.Value [i]);
			}//end for loop
			values.Add (nInsertedValue);
			values.Sort ( );
			numberofSplit = values [(int) half];
			for (int i = 0 ; i < (int) Math.Ceiling (half) - 1 ; i++)
			{
				leafLess.Insert (values [i]);
			}//end for loop
			for (int i = (int) Math.Ceiling (half) ; i < NodeSize ; i++)
			{
				leafMore.Insert (values [i]);
			}//end for loop
			INSERT results = newRoot.Insert (numberofSplit);
			if (IndexCount > 0)
			{
				int pos = newRoot.Indexes.IndexOf (leaf);
				newRoot.Indexes.RemoveAt (pos);
			}//End if statement
			if (results == INSERT.NEEDSPLIT)
			{
				Index OverPacked = new Index (NodeSize);
				OverPacked.Insert (leafLess);
				OverPacked.Insert (leafMore);
				SplitIndex (newRoot, OverPacked, values [(int) half]);
			}//End if statement
			else
			{
				newRoot.Insert (leafLess);
				newRoot.Insert (leafMore);
			}//End else statement
			LeafCount++;
		}//End SplitLeaf (Leaf, int)
Beispiel #3
0
        /// <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
        }