示例#1
0
        public void Enqueue(PQueueItem item)
        {
            int index = numElem;

            if (item == null || index >= 39)
            {
                return;                             // first element stored at 1
            }
            while (index > 1 && item.Priority < pQ[index / 2].Priority)
            {
                pQ[index] = pQ[index / 2];
                index     = index / 2;
            }
            pQ[index] = item;
            numElem++;
        }
示例#2
0
        public PQueueItem Dequeue()
        {
            if (pQ[1] == null || numElem <= 1)
            {
                return(null);
            }
            PQueueItem itemtoBeReturned = pQ[1];
            PQueueItem lastElement      = pQ[--numElem];
            int        childIndex;
            int        i;

            //IMP: this for loop is critical! see i being init to childIndex! (i tried while loop and failed miserably)
            for (i = 1; i * 2 <= numElem; i = childIndex)
            {
                //find smaller child
                childIndex = i * 2;
                if (childIndex != numElem)
                {
                    if (pQ[childIndex + 1].Priority < pQ[childIndex].Priority)
                    {
                        childIndex++;
                    }
                }
                //percolate one level
                if (lastElement.Priority > pQ[childIndex].Priority)
                {
                    pQ[i] = pQ[childIndex];
                }
                else
                {
                    break;
                }
            }
            pQ[i]       = lastElement;
            pQ[numElem] = null;
            return(itemtoBeReturned);
        }
示例#3
0
        private void KruskalGraphInit()
        {
            //populate graph - TODO
            PQueueItem a = new PQueueItem(1, new int[2] {
                1, 2
            });
            PQueueItem b = new PQueueItem(1, new int[2] {
                2, 3
            });
            PQueueItem c = new PQueueItem(1, new int[2] {
                3, 4
            });
            PQueueItem d = new PQueueItem(1, new int[2] {
                4, 5
            });
            PQueueItem e = new PQueueItem(4, new int[2] {
                2, 5
            });
            PQueueItem f = new PQueueItem(3, new int[2] {
                3, 5
            });
            PQueueItem g = new PQueueItem(6, new int[2] {
                1, 4
            });
            PQueueItem h = new PQueueItem(5, new int[2] {
                1, 5
            });

            GraphPQ.Enqueue(a);
            GraphPQ.Enqueue(b);
            GraphPQ.Enqueue(c);
            GraphPQ.Enqueue(d);
            GraphPQ.Enqueue(e);
            GraphPQ.Enqueue(f);
            GraphPQ.Enqueue(g);
            GraphPQ.Enqueue(h);
        }