Пример #1
0
        public tuple insertItem(tuple newData)                  // O(log k) time
        {
            int   index;
            tuple pointer;

            if (heaplimit >= (arraysize - 1))
            {
                grow();
            }                                        // if heap is full, grow by factor of 2

            index              = heaplimit;          //
            A[index].d.m       = newData.m;          // copy newData onto the bottom of the heap
            A[index].d.i       = newData.i;          //
            A[index].d.j       = newData.j;          //
            A[index].d.k       = index;
            A[index].d.enabled = true;
            pointer            = A[index].d;            // store pointer to container
            pointer.enabled    = true;
            heaplimit++;                                // note the larger heap
            upsift(index);                              // upsift new item up to proper spot
            if (heaplimit > 1)
            {
                isempty = false;
            }

            return(pointer);
        }
Пример #2
0
        public tuple    popMaximum()            // removes and returns heap max, reheapifies
        {                                       // O(log k) time
            tuple temp = returnMaximum();

            deleteItem(A[1].d);
            return(temp);
        }
Пример #3
0
        public void             shrink()                        // decrease size of array A
        {                                                       // O(k) time
            tuple newtuple;

            hnode[] B;                                                          // scratch space for contraction of A
            B = new hnode [heaplimit];                                          //
            for (int i = 0; i < heaplimit; i++)
            {
                B[i].d = A[i].d;
            }                                                      // copy A into B
            // delete old array of addresses
            A = new hnode [arraysize / 2];                         // shrink A by factor of 2
            for (int i = 0; i < heaplimit; i++)
            {
                A[i].d = B[i].d;
            }                                                      // copy B into A
            for (int i = heaplimit; i < (arraysize / 2); i++)      // initialize new heap values
            {
                newtuple = new tuple();                            //
                A[i].d   = newtuple;                               //
                A[i].d.m = -4294967296.0;                          //
                A[i].d.i = 0;                                      //
                A[i].d.j = 0;                                      //
                A[i].d.k = i;                                      //
            }
            // delete scratch space B
            arraysize = arraysize / 2;                                                    // update size of array
            return;
        }
Пример #4
0
        public void grow()  //increase size of array A
        {
            tuple newtuple;

            hnode[] B;                                                          // scratch space for expansion of A
            B = new hnode [arraysize];                                          //
            for (int i = 0; i < arraysize; i++)
            {
                B[i].d = A[i].d;
            }                                                      // copy A into B
            // delete old array of addresses
            A = new hnode [2 * arraysize];                         // grow A by factor of 2
            for (int i = 0; i < arraysize; i++)
            {
                A[i].d = B[i].d;
            }                                                      // copy B into first half of A
            for (int i = arraysize; i < (2 * arraysize); i++)      // initialize new heap values
            {
                newtuple = new tuple();                            //
                A[i].d   = newtuple;                               //
                A[i].d.m = -4294967296.0;                          //
                A[i].d.i = 0;                                      //
                A[i].d.j = 0;                                      //
                A[i].d.k = i;                                      //
            }
            // delete scratch space B
            arraysize = 2 * arraysize;                                                    // update size of array
            return;
        }
Пример #5
0
        public void updateItemdub(tuple address, double newStored)      // O(log k) time
        {
            double oldm = address.m;

            address.m = newStored;                                              // udpate the dQ stored
            if (oldm > newStored)
            {
                downsift(address.k);
            }                                                           // downsift if old value was larger
            else
            {
                upsift(address.k);
            }                                                                           // upsift otherwise
            return;
        }
Пример #6
0
        public void updateItem(tuple address, tuple newData) // O(log k) time
        {
            double oldm = address.m;

            address.m = newData.m;                                              //   udpate the dQ stored
            address.j = newData.j;                                              //   udpate the community to which to join
            if (oldm > newData.m)
            {
                downsift(address.k);
            }                                                //   downsift if old value was larger
            else
            {
                upsift(address.k);
            }                                                                //   upsift otherwise
            return;
        }
Пример #7
0
        public modmaxheap(int size)                             // default constructor
        {
            tuple newtuple;

            heaplimit = 1;                                      // first free location is A[1]
            arraysize = size + 1;                               //
            isempty   = true;                                   // heap is initially empty
            A         = new hnode [arraysize];                  // allocate array for heap
            for (int i = 0; i < arraysize; i++)                 // initialize heap values
            {
                newtuple = new tuple();                         //
                A[i].d   = newtuple;                            //
                A[i].d.m = -4294967296.0;                       // a very negative value; unlikely to be a valid dQ
                A[i].d.i = 0;                                   //
                A[i].d.j = 0;                                   //
                A[i].d.k = i;                                   //
            }
        }
Пример #8
0
        public void deleteItem(tuple address)
        {
            tuple swap;
            int   small = heaplimit - 1;
            int   index = address.k;

            if (heaplimit == 2)                                         // check if deleting last item in heap
            {
                A[1].d.m       = -4294967296.0;                         // zero out root data
                A[1].d.i       = 0;                                     //
                A[1].d.j       = 0;                                     //
                A[1].d.k       = 1;                                     //
                A[1].d.enabled = false;
                isempty        = true;                                  // note the heap's emptiness
                heaplimit--;                                            // decrement size of heap to empty
            }
            else
            {
                if (index >= A.Length)
                {
                    grow();
                }
                A[index].d.m       = -4294967296.0;             // zero out the deleted item's data
                A[index].d.i       = 0;                         //
                A[index].d.j       = 0;
                A[index].d.enabled = false;
                swap         = A[index].d;                      // swap this element with item at end of heap
                A[index].d   = A[small].d;                      //
                A[small].d   = swap;                            //
                A[index].d.k = index;                           // note the change in locations
                A[small].d.k = small;                           //

                heaplimit--;                                    // note change in heap size
                downsift(index);                                // downsift moved element to new location; O(log k)
                if ((heaplimit / 2 > heapmin) && (heaplimit == (arraysize / 2)))
                {
                    shrink();
                }                                                                              // shrink by factor of 2 if necessary
            }
            return;
        }