Beispiel #1
0
        private Comparable RightChild(Comparable X)
        {
            int idx = RightChildIndex(X) - 1;

            if (idx < ImplicitHeap.Count)
            {
                return((Comparable)ImplicitHeap[idx]);
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
        private Comparable Parent(Comparable X)
        {
            int idx = ParentIndex(X) - 1;

            if (idx > -1)
            {
                return((Comparable)ImplicitHeap[idx]);
            }
            else
            {
                return(null);
            }
        }
Beispiel #3
0
        public void Insert(Comparable X)
        {
            //for (int i = 0; i < ImplicitHeap.Count; i++)
            //{
            //    if (ImplicitHeap[i] == X)
            //    {
            //        BasicConsole.Write(Name);
            //        BasicConsole.Write(" - ");
            //        BasicConsole.WriteLine("Priority queue : Error! Attempted to re-insert item.");
            //        return;
            //    }
            //}

            X.Position = ImplicitHeap.Count + 1;
            ImplicitHeap.Add((FOS_System.Object)X);
            DecreaseKey(X, X.Key);
        }
Beispiel #4
0
        public void DecreaseKey(Comparable X, int NewKey)
        {
            if (NewKey > X.Key)
            {
                ExceptionMethods.Throw(new Exceptions.ArgumentException("New key is larger than old key!"));
            }

            X.Key = NewKey;

            Comparable parent = Parent(X);
            while (parent != null &&
                   X.Key < parent.Key)
            {
                Swap(parent, X);

                parent = Parent(X);
            }
        }
Beispiel #5
0
        public void DecreaseKey(Comparable X, int NewKey)
        {
            if (NewKey > X.Key)
            {
                ExceptionMethods.Throw(new Exceptions.ArgumentException("New key is larger than old key!"));
            }

            X.Key = NewKey;

            Comparable parent = Parent(X);

            while (parent != null &&
                   X.Key < parent.Key)
            {
                Swap(parent, X);

                parent = Parent(X);
            }
        }
Beispiel #6
0
 private int RightChildIndex(Comparable X)
 {
     return((X.Position * 2) + 1);
 }
Beispiel #7
0
 private int LeftChildIndex(Comparable X)
 {
     return(X.Position * 2);
 }
Beispiel #8
0
        public bool Insert(Comparable X)
        {
            //for (int i = 0; i < ImplicitHeap.Count; i++)
            //{
            //    if (ImplicitHeap[i] == X)
            //    {
            //        BasicConsole.Write(Name);
            //        BasicConsole.Write(" - ");
            //        BasicConsole.WriteLine("Priority queue : Error! Attempted to re-insert item.");
            //        return false;
            //    }
            //}

            X.Position = ImplicitHeap.Count + 1;
            ImplicitHeap.Add((FOS_System.Object)X);
            DecreaseKey(X, X.Key);
            return true;
        }
Beispiel #9
0
 private int RightChildIndex(Comparable X)
 {
     return (X.Position * 2) + 1;
 }
Beispiel #10
0
 private int ParentIndex(Comparable X)
 {
     if (X.Position > 1)
     {
         return X.Position / 2;
     }
     return 0;
 }
Beispiel #11
0
 private Comparable RightChild(Comparable X)
 {
     int idx = RightChildIndex(X)-1;
     if (idx < ImplicitHeap.Count)
     {
         return (Comparable)ImplicitHeap[idx];
     }
     else
     {
         return null;
     }
 }
Beispiel #12
0
 private Comparable Parent(Comparable X)
 {
     int idx = ParentIndex(X)-1;
     if (idx > -1)
     {
         return (Comparable)ImplicitHeap[idx];
     }
     else
     {
         return null;
     }
 }
Beispiel #13
0
        private void Swap(Comparable X, Comparable Y, bool print = false)
        {
            int tempPos = X.Position;
            X.Position = Y.Position;
            Y.Position = tempPos;

            ImplicitHeap[X.Position - 1] = (FOS_System.Object)X;
            ImplicitHeap[Y.Position - 1] = (FOS_System.Object)Y;
        }
Beispiel #14
0
        private void Rebalance(Comparable Y)
        {
            //BasicConsole.WriteLine("Rebalance (1)");

            Comparable ALeftChild = LeftChild(Y);
            Comparable ARightChild = RightChild(Y);
            
            while ((ALeftChild != null && Y.Key > ALeftChild.Key) ||
                  (ARightChild != null && Y.Key > ARightChild.Key))
            {
                //BasicConsole.WriteLine("Rebalance (2)");

                if (ALeftChild != null && Y.Key > ALeftChild.Key)
                {
                    if (ARightChild != null && ARightChild.Key < ALeftChild.Key)
                    {
                        Swap(ARightChild, Y);
                    }
                    else
                    {
                        Swap(ALeftChild, Y);
                    }
                }
                else if (ARightChild != null && Y.Key > ARightChild.Key)
                {
                    Swap(ARightChild, Y);
                }

                ALeftChild = LeftChild(Y);
                ARightChild = RightChild(Y);
            }
        }
Beispiel #15
0
        public void Delete(Comparable X)
        {
            //BasicConsole.WriteLine("Deleting");

            //bool OK = false;
            //for (int i = 0; i < ImplicitHeap.Count; i++)
            //{
            //    if (ImplicitHeap[i] == X)
            //    {
            //        OK = true;
            //        break;
            //    }
            //}
            //if (!OK)
            //{
            //    BasicConsole.WriteLine("Deleting when not present.");
            //}

            if (X.Position <= 0 || X.Position > ImplicitHeap.Count || ImplicitHeap[X.Position - 1] != X)
            {
                //BasicConsole.WriteLine("No delete");
                return;
            }

            //BasicConsole.WriteLine("Will delete");
            if (ImplicitHeap.Count > 1)
            {
                //BasicConsole.WriteLine("Using last item");
                Comparable Y = (Comparable)ImplicitHeap.Last();
                if (X != Y)
                {
                    //BasicConsole.WriteLine("X not equal to Y");
                    //BasicConsole.WriteLine("Removing Y");
                    ImplicitHeap.RemoveAt(Y.Position - 1);

                    //BasicConsole.WriteLine("Setting Y position");
                    Y.Position = X.Position;
                    //BasicConsole.WriteLine("Setting Y in array");
                    ImplicitHeap[Y.Position - 1] = (FOS_System.Object)Y;

                    //BasicConsole.WriteLine("Rebalancing");
                    Rebalance(Y);
                }
                else
                {
                    //BasicConsole.WriteLine("X is Y, removing X");
                    ImplicitHeap.RemoveAt(X.Position - 1);
                }
            }
            else
            {
                //BasicConsole.WriteLine("Removing at index 0");
                ImplicitHeap.RemoveAt(0);
            }

            //BasicConsole.WriteLine("Reset position");
            X.Position = 0;
        }
Beispiel #16
0
 public virtual int Compare(Comparable x, Comparable y)
 {
     return x.Key < y.Key ? -1 : x.Key == y.Key ? 0 : 1;
 }
Beispiel #17
0
 public virtual int Compare(Comparable x, Comparable y)
 {
     return(x.Key < y.Key ? -1 : x.Key == y.Key ? 0 : 1);
 }
Beispiel #18
0
 private int LeftChildIndex(Comparable X)
 {
     return X.Position * 2;
 }