Beispiel #1
0
    public object Dequeue()
    {
        heapSize = heapSize - 1;

        object max = heap [0].obj;

        PriorityObject newElement = heap [heapSize];

        FixHeap(newElement, 0);

        return(max);
    }
Beispiel #2
0
    private void AppendToArray(PriorityObject el)
    {
        int cap = heap.GetLength(0);

        if (heapSize + 1 >= cap)
        {
            PriorityObject[] newHeap = new PriorityObject[cap * 2];

            for (int i = 0; i < heapSize; i++)
            {
                newHeap [i] = heap [i];
            }

            heap = newHeap;
        }

        heap [heapSize] = el;
        heapSize        = heapSize + 1;
    }
Beispiel #3
0
    public void Enqueue(object el, float priority)
    {
        PriorityObject obj = new PriorityObject(el, priority);

        AppendToArray(obj);

        // Percolate-up
        int currentIndex = heapSize - 1;
        int parentIndex  = (currentIndex - 1) / 2;

        while (parentIndex >= 0 && heap [parentIndex] < heap [currentIndex])
        {
            var temp = heap [parentIndex];
            heap [parentIndex]  = heap [currentIndex];
            heap [currentIndex] = temp;

            currentIndex = parentIndex;
            parentIndex  = (currentIndex - 1) / 2;
        }
    }
Beispiel #4
0
    private void FixHeap(PriorityObject obj, int index)
    {
        PriorityObject maxChild;
        int            maxIndex;

        if (index * 2 + 1 < heapSize)
        {
            maxChild = heap [index * 2 + 1];
            maxIndex = index * 2 + 1;

            if (index * 2 + 2 < heapSize)
            {
                var child = heap [index * 2 + 2];
                if (child > maxChild)
                {
                    maxChild = child;
                    maxIndex = index * 2 + 2;
                }
            }

            if (maxChild > obj)
            {
                heap [index] = maxChild;
                FixHeap(obj, maxIndex);
            }
            else
            {
                heap [index] = obj;
            }
        }
        else
        {
            // else stop! no children to check; we're a leaf now
            heap [index] = obj;
        }
    }