public void HeapifiyNodeDown(int index)
        {
            int tempIndex;

            if (index * 2 + 1 < heap.Length)
            {
                if (heap[index * 2 + 1] != null)
                {
                    tempIndex = heap[index * 2].Priority > heap[index * 2 + 1].Priority ? index * 2 : index * 2 + 1;
                }
                else
                {
                    tempIndex = index * 2;
                }
                if (heap[tempIndex] != null)
                {
                    if (heap[index].Priority < heap[tempIndex].Priority)
                    {
                        //swap nodes
                        PQNode tempNode = heap[index];
                        heap[index]     = heap[tempIndex];
                        heap[tempIndex] = tempNode;
                        HeapifiyNodeDown(tempIndex);
                    }
                }
            }
        }
        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);
                }
            }
        }
Esempio n. 3
0
        public void Enqueue(int priority, int value)
        {
            PQNode pQNode = new PQNode()
            {
                Priority = priority, Value = value
            };

            if (root == null)
            {
                root = pQNode;
            }
            else
            {
                PQNode tempPQNode = root;
                tempPQNode          = AddPQNode(tempPQNode);
                tempPQNode.NextNode = pQNode;
                Count++;

                int counter = Count - 1;

                while (counter > 0)
                {
                    int i = (counter - 1) / 2;
                    if (Find(counter).Value.CompareTo(Find(i).Value) >= 0)
                    {
                        break;
                    }

                    SwitchPQNodes(counter, i);
                    counter = i;
                }
            }
        }
Esempio n. 4
0
        private PQNode AddPQNode(PQNode pQNode)
        {
            while (pQNode.NextNode != null)
            {
                pQNode = pQNode.NextNode;
            }

            return(pQNode);
        }
        /**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);
        }
Esempio n. 6
0
        public PQNode Dequeue()
        {
            PQNode pQNode = root;

            root          = root.NextNode;
            root.PrevNode = null;
            Count--;

            return(root);
        }
Esempio n. 7
0
        private PQNode Find(int index)
        {
            PQNode temp = root;

            for (int i = 0; i < index; i++)
            {
                temp = temp.NextNode;
            }
            return(temp);
        }
        public PQNode[] ToSortedArray()
        {
            PQNode[] array = new PQNode[Count];
            int      i     = 0;

            while (Count > 0)
            {
                array[i] = Dequeue();
                i++;
            }
            return(array);
        }
Esempio n. 9
0
        public int CompareTo(object obj)
        {
            PQNode <T> other        = obj as PQNode <T>;
            int        compareValue = Priority.CompareTo(other.Priority);

            if (compareValue == 0)
            {
                // priority is the same, compare by their value
                compareValue = Value.CompareTo(other.Value);
            }
            return(compareValue);
        }
Esempio n. 10
0
        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            PQNode        pQNode        = root;

            while (pQNode.NextNode != null)
            {
                stringBuilder.Append(pQNode.Priority + ":" + pQNode.Value + ", ");
                pQNode = pQNode.NextNode;
            }

            return(stringBuilder.ToString());
        }
        //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);
        }
Esempio n. 12
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            for (int j = 0; j < Count; ++j)
            {
                PQNode <T> node = Queue.Values[j];
                sb.Append(node + ", ");
            }
            string str = sb.ToString().Trim().TrimEnd(',');

            return(str);
        }
        public PQNode Dequeue()
        {
            PQNode tempNode = null;

            if (Count >= 1)
            {
                tempNode    = heap[1];
                heap[1]     = heap[Count];
                heap[Count] = null;
                Count--;
                HeapifiyNodeDown(1);
            }

            return(tempNode);
        }
        public void HeapifiyNodeUp(int index)
        {
            PQNode parent = heap[index / 2];

            if (parent != null && heap[index].Priority > parent.Priority)
            {
                //swap parent with node
                heap[index / 2] = heap[index];
                heap[index]     = parent;
                if (index / 2 != 0)
                {
                    HeapifiyNodeUp(index / 2);
                }
            }
        }
Esempio n. 15
0
        private void SwitchPQNodes(int firstIndex, int secondIndex)
        {
            PQNode firstNode  = Find(firstIndex);
            PQNode secondNode = Find(secondIndex);

            PQNode tempNode = firstNode;

            firstNode.NextNode = secondNode.NextNode;
            firstNode.PrevNode = secondNode.PrevNode;
            firstNode.Priority = secondNode.Priority;

            secondNode.NextNode = tempNode.NextNode;
            secondNode.PrevNode = tempNode.PrevNode;
            secondNode.Priority = tempNode.Priority;
        }
        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);
                }
            }
        }
        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 Enqueue(int priority, int value)
        {
            Count++;
            if (Count > heap.Length - 1)
            {
                PQNode[] tempHeap = new PQNode[heap.Length * 2];
                heap.CopyTo(tempHeap, 0);
                heap = tempHeap;
            }
            PQNode tempNode = new PQNode()
            {
                Value = value, Priority = priority
            };

            heap[Count] = tempNode;

            HeapifiyNodeUp(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);
            }
        }
        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;
                }
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Creates a PQNode with the specified priority and value
        /// and adds it to this Queue. PQNodes with the highest
        /// priority will be added to the front of this Queue.
        /// </summary>
        /// <param name="priority">priority level</param>
        /// <param name="value">value</param>
        public void Enqueue(int priority, T value)
        {
            PQNode <T> nodeToAdd = new PQNode <T>(priority, value);

            Queue.Insert(nodeToAdd);
        }
Esempio n. 22
0
        /// <summary>
        /// Removes the first PQNode in this queue, which has the
        /// highest priority.
        /// </summary>
        /// <returns>PQNode that we removed</returns>
        public PQNode <T> Dequeue()
        {
            PQNode <T> nodeToRemove = Queue.RemoveMax();

            return(nodeToRemove);
        }
Esempio n. 23
0
        /// <summary>
        /// Returns but doesn't remove the first PQNode in this queue,
        /// which has the highest priority.
        /// </summary>
        /// <returns>PQNode at the start of this Queue</returns>
        public PQNode <T> Peek()
        {
            PQNode <T> node = Queue.Values[0];

            return(node);
        }