Exemplo n.º 1
0
        public void HeapifyDown(int pos)
        {
            if (pos <= _count / 2)
            {
                if (_holdthis[pos * 2] != null && _holdthis[(pos * 2) + 1] == null)
                {
                    if (_holdthis[pos].Priority < _holdthis[pos * 2].Priority)
                    {
                        PQNode temp = _holdthis[pos * 2];
                        _holdthis[pos * 2] = _holdthis[pos];
                        _holdthis[pos]     = temp;

                        HeapifyDown(pos * 2);
                    }
                }
                else if (_holdthis[pos * 2] != null && _holdthis[(pos * 2) + 1] != null && _holdthis[pos * 2].Priority > _holdthis[(pos * 2) + 1].Priority)
                {
                    PQNode temp = _holdthis[pos * 2];
                    _holdthis[pos * 2] = _holdthis[pos];
                    _holdthis[pos]     = temp;

                    HeapifyDown(pos * 2);
                }
                else
                {
                    PQNode temp = _holdthis[(pos * 2) + 1];
                    _holdthis[(pos * 2) + 1] = _holdthis[pos];
                    _holdthis[pos]           = temp;

                    HeapifyDown((pos * 2) + 1);
                }
            }
        }
Exemplo n.º 2
0
        /**Using heapsort, produce and return an array of PQNode instances sorted in non-decreasing order of priority.
         * Despite the typical convention of ignoring index 0 of the backing array, the array you return from this method
         * should start at 0 and continue to index n-1 (like a typical array in Java or C#)
         **/
        public PQNode[] ToSortedArray()
        {
            PQNode [] outputArray = new PQNode[_count];
            Array.Copy(_holdthis, 1, outputArray, 0, _count);

            HeapSort(outputArray);

            return(outputArray);
        }
Exemplo n.º 3
0
        //Removes the root node of the queue and returns it, adjusts the count, and heapifies as needed.
        public PQNode Dequeue()
        {
            PQNode temp = _holdthis[1];

            _holdthis[1]      = _holdthis[_count];
            _holdthis[_count] = null;

            _count--;

            HeapifyDown(1);

            return(temp);
        }
Exemplo n.º 4
0
        private void HeapifyUp(int pos)
        {
            if (pos > 1)
            {
                int parentPos = (pos) / 2;

                if (_holdthis[pos].Priority > _holdthis[parentPos].Priority)
                {
                    PQNode temp = _holdthis[parentPos];
                    _holdthis[parentPos] = _holdthis[pos];
                    _holdthis[pos]       = temp;

                    HeapifyUp(parentPos);
                }
            }
        }
Exemplo n.º 5
0
        private void HeapSort(PQNode[] a)
        {
            int count = a.Length;
            int end   = count - 1;

            while (end > 0)
            {
                //swap max value of the heap with the last element of the heap
                PQNode tmp = a[end];
                a[end] = a[0];
                a[0]   = tmp;
                //put the heap back in max-heap order
                Heapify(a, end - 1);
                //decrease size of the heap so that the previous max value will stay in its new place
                end--;
            }
        }
        public void Heapify(PQNode[] a, int end)
        {
            int root = 0;

            while ((root * 2) + 1 <= end)
            {
                int child = root * 2 + 1;
                if (child + 1 <= end && a[child].Priority < a[child + 1].Priority)
                    child = child + 1;
                if (a[root].Priority < a[child].Priority)
                {
                    PQNode tmp = a[root];
                    a[root] = a[child];
                    a[child] = tmp;
                    root = child;
                }
                else
                    return;
            }
        }
Exemplo n.º 7
0
        /** Takes in the priority and value as parameters.Creates a new PQNode with this value and adds the node to the queue,
         * adjusts the count, and heapifies as needed. When heapifying, a child node with equal priority to its parent does NOT
         * promote and the heapifying of that particular child node stops.
         *  1. ignore zero
         *  2. priority matters
         *  3. p = n , left = 2n, right = 2n + 1
         **/
        public void Enqueue(int priority, int value)
        {
            _count++;
            PQNode addThis = new PQNode(priority, value);

            if (_count == _holdthis.Length)
            {
                PQNode[] doubleHold = new PQNode[_holdthis.Length * 2];

                Array.Copy(_holdthis, 0, doubleHold, 0, _count);
                _holdthis = doubleHold;
            }

            _holdthis[_count] = addThis;

            if (_count > 1)
            {
                HeapifyUp(_count);
            }
        }
        /** Takes in the priority and value as parameters.Creates a new PQNode with this value and adds the node to the queue,
        adjusts the count, and heapifies as needed. When heapifying, a child node with equal priority to its parent does NOT
        promote and the heapifying of that particular child node stops.
            1. ignore zero
            2. priority matters
            3. p = n , left = 2n, right = 2n + 1
        **/
        public void Enqueue(int priority, int value)
        {
            _count++;
            PQNode addThis = new PQNode(priority, value);

            if (_count == _holdthis.Length)
            {
                PQNode[] doubleHold = new PQNode[_holdthis.Length * 2];

                Array.Copy(_holdthis, 0, doubleHold, 0, _count);
                _holdthis = doubleHold;
            }

            _holdthis[_count] = addThis;

            if (_count > 1)
            {
                HeapifyUp(_count);

            }
        }
Exemplo n.º 9
0
        public void Heapify(PQNode[] a, int end)
        {
            int root = 0;

            while ((root * 2) + 1 <= end)
            {
                int child = root * 2 + 1;
                if (child + 1 <= end && a[child].Priority < a[child + 1].Priority)
                {
                    child = child + 1;
                }
                if (a[root].Priority < a[child].Priority)
                {
                    PQNode tmp = a[root];
                    a[root]  = a[child];
                    a[child] = tmp;
                    root     = child;
                }
                else
                {
                    return;
                }
            }
        }
        private void HeapSort(PQNode[] a)
        {
            int count = a.Length;
            int end = count - 1;

            while (end > 0)
            {
                //swap max value of the heap with the last element of the heap
                PQNode tmp = a[end];
                a[end] = a[0];
                a[0] = tmp;
                //put the heap back in max-heap order
                Heapify(a, end - 1);
                //decrease size of the heap so that the previous max value will stay in its new place
                end--;
            }
        }
        /**Using heapsort, produce and return an array of PQNode instances sorted in non-decreasing order of priority.
           Despite the typical convention of ignoring index 0 of the backing array, the array you return from this method
           should start at 0 and continue to index n-1 (like a typical array in Java or C#)
         **/
        public PQNode[] ToSortedArray()
        {
            PQNode [] outputArray = new PQNode[_count];
            Array.Copy(_holdthis, 1, outputArray, 0, _count);

            HeapSort(outputArray);

               return outputArray;
        }