Exemple #1
0
		void decreaseKey(int index, double newPriority)
		{
			PairNode * p = phIndex[index];
			if(p->priority < newPriority)
			{
				throw IllegalArgumentException();
			}
			p->priority = newPriority;
			if(p != root)
			{
				if(p->nextSibling != NULL) // Disconnects p from heap with children
				{
					p->nextSibling->prev = p->prev;
				}
				if(p->prev->leftChild == p){
					p->prev->leftChild = p->nextSibling;
				}
				else
				{
					p->prev->nextSibling = p->nextSibling;
				}
				p->nextSibling = NULL;
				compareAndLink(root, p); // Merges p back to heap
			}
		}
Exemple #2
0
		PairNode * combineSiblings(PairNode * firstSibling)
		{
			if(firstSibling->nextSibling == NULL)
			{
				return firstSibling; // If there are no siblings, return the firstSibling
			}

			static vector<PairNode *> treeArray(5);	

			int numSiblings = 0;
			for(;firstSibling!= NULL; numSiblings++)  // Create an array of trees
			{
				if(numSiblings == treeArray.size())
				{
					treeArray.resize(numSiblings*2);
				}
				treeArray[numSiblings] = firstSibling;
				firstSibling->prev->nextSibling = NULL;
				firstSibling = firstSibling->nextSibling; 
			}
			if(numSiblings == treeArray.size())
			{
				treeArray.resize(numSiblings+1); // Add a NULL to the end of the array
			}
			treeArray[numSiblings]=NULL;

			int i = 0;
			for(; i+1 < numSiblings; i += 2)
			{
				compareAndLink(treeArray[i], treeArray[i+1]); // Link adjacent trees
			}

			int j = i - 2;	

			if (j == numSiblings - 3) // if odd number of trees
			{
				compareAndLink(treeArray[j], treeArray[j+2]); // link to NULL
			}
			for(; j >= 2; j-=2)
			{
				compareAndLink(treeArray[j-2], treeArray[j]); // link every other tree backwards
			}
			return treeArray[0]; // return the new root
		}
Exemple #3
0
/**
 * Internal method that implements two-pass merging.
 * firstSibling the root of the conglomerate and is assumed not NULL.
 */
PairNode * combineSiblings( PairNode *firstSibling )
{
    if( firstSibling->nextSibling == NULL )
        return firstSibling;

        // Allocate the array
    static vector<PairNode *> treeArray( 5 );

        // Store the subtrees in an array
    int numSiblings = 0;
    for( ; firstSibling != NULL; numSiblings++ )
    {
        if( numSiblings == treeArray.size( ) )
            treeArray.resize( numSiblings * 2 );
        treeArray[ numSiblings ] = firstSibling;
        firstSibling->prev->nextSibling = NULL;  // break links
        firstSibling = firstSibling->nextSibling;
    }
    if( numSiblings == treeArray.size( ) )
        treeArray.resize( numSiblings + 1 );
    treeArray[ numSiblings ] = NULL;

        // Combine subtrees two at a time, going left to right
    int i = 0;
    for( ; i + 1 < numSiblings; i += 2 )
        compareAndLink( treeArray[ i ], treeArray[ i + 1 ] );

    int j = i - 2;

        // j has the result of last compareAndLink.
        // If an odd number of trees, get the last one.
    if( j == numSiblings - 3 )
        compareAndLink( treeArray[ j ], treeArray[ j + 2 ] );

        // Now go right to left, merging last tree with
        // next to last. The result becomes the new last.
    for( ; j >= 2; j -= 2 )
        compareAndLink( treeArray[ j - 2 ], treeArray[ j ] );
    return treeArray[ 0 ];
}
Exemple #4
0
		void insert(int index, Object item, double priority)
		{
			if(index >= phIndex.size())
			{
				throw IllegalArgumentException();
			}
			PairNode * newNode = new PairNode(item,priority);
			if (phIndex[index] != NULL)
			{
				delete phIndex[index];
			}
			phIndex[index] = newNode;
			if(root == NULL)
			{
				root = newNode;
			}
			else
			{
				compareAndLink(root,newNode);
			}
		}