Beispiel #1
0
        protected void DownHeap()
        {
            int i = 0;

            while (i <= (Size - 1) / 2)
            {
                PriorityQueueNode <T> node = heap[i];

                PriorityQueueNode <T> biggerChild = GetBiggerChild(GetChildren(i));
                if (biggerChild == null)
                {
                    break;
                }

                if (comparator(biggerChild.Priority, node.Priority))
                {
                    Swap(biggerChild.Index, node.Index);
                    i = node.Index;
                }
                else
                {
                    break;
                }
            }
        }
Beispiel #2
0
        public virtual void Add(T item, float priority)
        {
            Size++;
            PriorityQueueNode <T> newNode = new PriorityQueueNode <T>(item, priority, Size - 1);

            heap.Add(newNode);
            newNode.Index = heap.IndexOf(newNode);
            if (Size >= 2)
            {
                Upheap();
            }
        }
Beispiel #3
0
 public void Reverse()
 {
     for (int i = 0; i < Size; i++)
     {
         if (i >= Size - 1 - i)
         {
             break;
         }
         PriorityQueueNode <T> temp = heap[i];
         heap[i]            = heap[Size - 1 - i];
         heap[Size - 1 - i] = temp;
     }
 }
Beispiel #4
0
        public virtual PriorityQueueNode <T> SearchNode(T item)
        {
            PriorityQueueNode <T> searchResult = null;

            foreach (PriorityQueueNode <T> node in heap)
            {
                if (node.Item.Equals(item))
                {
                    searchResult = node;
                    break;
                }
            }
            return(searchResult);
        }
Beispiel #5
0
        protected PriorityQueueNode <T> GetBiggerChild(List <PriorityQueueNode <T> > children)
        {
            PriorityQueueNode <T> biggerChild = null;

            if (children.Count == 1)
            {
                biggerChild = children[0];
            }
            if (children.Count == 2)
            {
                biggerChild = (comparator(children[0].Priority, children[1].Priority))? children[0] : children[1];
            }
            return(biggerChild);
        }
Beispiel #6
0
        protected void BottomUp()
        {
            for (int i = Size / 2 - 1; i >= 0; i++)
            {
                PriorityQueueNode <T> father = heap[i];

                List <PriorityQueueNode <T> > children    = GetChildren(i);
                PriorityQueueNode <T>         biggerChild = GetBiggerChild(children);


                if (comparator(biggerChild.Priority, father.Priority))
                {
                    Swap(biggerChild.Index, father.Index);
                }
            }
        }
Beispiel #7
0
        protected void Swap(int firstElementIntex, int secondElementIndex)
        {
            if (firstElementIntex < 0 || firstElementIntex >= Size)
            {
                Debug.Log("firstElementIntex: " + firstElementIntex + " out of range. Size is: " + Size);
            }
            if (secondElementIndex < 0 || secondElementIndex >= Size)
            {
                Debug.Log("secondElementIndex: " + secondElementIndex + " out of range");
            }

            PriorityQueueNode <T> firstElement  = heap[firstElementIntex];
            PriorityQueueNode <T> secondElement = heap[secondElementIndex];

            heap[firstElementIntex]  = secondElement;
            heap[secondElementIndex] = firstElement;

            firstElement.Index  = secondElementIndex;
            secondElement.Index = firstElementIntex;
        }
Beispiel #8
0
        public T Pop()
        {
            PriorityQueueNode <T> highestPriorityElement = heap[0];

            if (Size >= 2)
            {
                heap[0]       = heap[Size - 1];
                heap[0].Index = 0;
                heap.RemoveAt(Size - 1);
            }
            else
            {
                heap.RemoveAt(0);
            }
            Size--;
            if (Size >= 2)
            {
                DownHeap();
            }
            return(highestPriorityElement.Item);
        }
Beispiel #9
0
        protected void Upheap()
        {
            int i = Size - 1;

            while (i >= 0)
            {
                PriorityQueueNode <T> node   = heap[i];
                PriorityQueueNode <T> father = GetFather(node.Index);


                if (comparator(node.Priority, father.Priority))
                {
                    Swap(node.Index, father.Index);
                    i = node.Index;
                }
                else
                {
                    break;
                }
            }
        }