Esempio n. 1
0
        /// <summary>
        /// Deletes <paramref name="x"/> from this subtree.
        /// </summary>
        /// <param name="x">
        /// The value to delete.
        /// </param>
        /// <remarks>
        /// This method is based upon the vEB-Tree-Delete(V, x)
        /// algorithm pseudocode from the CLRS book on page 554.
        /// </remarks>
        public void Delete(int x)
        {
            if (x < 0 || x >= Universe)
            {
                // Simply return if the value to delete is outside of
                // the universe, since it can never be in this tree.
                return;
            }

            if (Minimum == Maximum)
            {
                Minimum = null;
                Maximum = null;
            }
            else if (Universe == 2)
            {
                Minimum = x == 0 ? 1 : 0;
                Maximum = Minimum;
            }
            else
            {
                if (x == Minimum)
                {
                    int?firstCluster = Summary.Minimum;
                    x       = Index(firstCluster.Value, Cluster[firstCluster.Value].Minimum.Value);
                    Minimum = x;
                }

                int highX = High(x);

                Cluster[highX].Delete(Low(x));

                if (!Cluster[highX].Minimum.HasValue)
                {
                    Summary.Delete(highX);
                    if (x == Maximum)
                    {
                        int?summaryMax = Summary.Maximum;
                        if (!summaryMax.HasValue)
                        {
                            Maximum = Minimum;
                        }
                        else
                        {
                            Maximum = Index(summaryMax.Value, Cluster[summaryMax.Value].Maximum.Value);
                        }
                    }
                }
                else if (x == Maximum)
                {
                    Maximum = Index(highX, Cluster[highX].Maximum.Value);
                }
            }
        }
Esempio n. 2
0
 public void Delete(uint x)
 {
     if (Min == Max)
     {
         Min = null;
         Max = null;
     }
     else if (U == 2)
     {
         if (x == 0)
         {
             Min = 1;
         }
         else
         {
             Min = 0;
         }
         Max = Min;
     }
     else
     {
         if (x == Min)
         {
             var firstCluster = Summary.Minimum();
             x   = Index(firstCluster.Value, Cluster[firstCluster.Value].Minimum().Value);
             Min = x;
         }
         Cluster[High(x)].Delete(Low(x));
         if (Cluster[High(x)].Minimum() == null)
         {
             Summary.Delete(High(x));
             if (x == Max)
             {
                 var summaryMax = Summary.Maximum();
                 if (summaryMax == null)
                 {
                     Max = Min;
                 }
                 else
                 {
                     Max = Index(summaryMax.Value, Cluster[summaryMax.Value].Maximum().Value);
                 }
             }
         }
         else
         {
             if (x == Max)
             {
                 Max = Index(High(x), Cluster[High(x)].Maximum().Value);
             }
         }
     }
 }