void heapSort(T a[],int size){
	int i;
	T tmp;
	for (int i=size/2-1;i>=0;i--) percolateDown(a,i,size);//´´½¨³õʼµÄ¶Ñ
	for (i=size-1;i>0;--i){
		tmp=a[0];
		a[0]=a[i];
		a[i]=tmp;
		percolateDown(a,0,i);
	}
}
示例#2
0
void * deleteMin(int size, void ** points, LeastPathCompare * cmp, LeastPathCallback * callback) {
  void * target = points[1];
  points[1] = points[size];
  callback(points[size], 1);
  percolateDown(size - 1, points, 1, cmp, callback);
  return target;
}
示例#3
0
int removeMin(struct heapStruct *h) {

    int retval;
    
    // We can only remove an element, if one exists in the heap!
    if (h->size > 0) {

        // This is where the minimum is stored.
        retval = h->heaparray[1];
        
        // Copy the last value into this top slot.
        h->heaparray[1] = h->heaparray[h->size];
        
        // Our heap will have one fewer items.
        h->size--;
        
        // Need to let this value move down to its rightful spot in the heap.
        percolateDown(h, 1);
        
        // Now we can return our value.
        return retval;
    }
    
    // No value to return, indicate failure with a -1.
    else
        return -1;
}
示例#4
0
文件: binaryheap.cpp 项目: shixv/test
void deleteMin(void)
{
	if(isEmpty())
		throw UnderflowException();
	array[i]=array[currentSize--];
	percolateDown(1);
}
示例#5
0
int heap::deleteMin(std::string * pId, int * pKey, void * ppData) {
	// Return 1 if heap is empty
	if (size == 0) {
		return 1;
	}

	// Set optional data parameters
	node n = data[1];
	
	// Delete item from hash table
	mapping->remove(n.id);

	if (pId) {
		*pId = n.id;
	}
	if (pKey) {
		*pKey = n.key;
	}
	if (ppData) {
		*(static_cast<void **>(ppData)) = n.pData;
	}

	// Swap root node with last node and new root down
	data[1] = data[size--];
	mapping->setPointer(data[1].id, &data[1]);
	percolateDown(1);

	return 0;
}
示例#6
0
void Heap::buildHeap(int * characterFreq)
{

  char letters [27] = {
    'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l',
    'm', 'n', 'o', 'p', 'q', 'r',
    's', 't', 'u', 'v', 'w', 'x',
    'y', 'z', ' '
    };

  Node * entry;

  for(int i = 0; i < 27; i++)
  {
    if(characterFreq[i] != 0)
    {
      occupancy++;
      entry = new Node(characterFreq[i], letters[i]);
      heap[occupancy] = entry;
    }
  }

  for(int j = occupancy/2-1; j > 0; --j){
    percolateDown( j );
  }

}
示例#7
0
int deletepos(int k)
{
	strcpy(h[k].first, h[hsize].first);
	strcpy(h[k].last, h[hsize].last);
	hsize--;
	percolateDown(k);
}
示例#8
0
文件: heapSort.c 项目: mcxiaoke/dsaac
// 删除树根,最大值移到最后
static void deleteMax(int *data, int size) {
//	size--;
//	swap(&data[0], &data[size]);
//	percolateDown(data, 0, size);
	swap2(data, 0, size - 1); // 堆树根与最后一个data[size-1];交换
	percolateDown(data, 0, --size);
}
示例#9
0
void percolateDown(struct heapStruct *h, int index) {

    int min;
    
    // Only try to percolate down internal nodes.
    if ((2*index+1) <= h->size) {
                    
        // Find the minimum value of the two children of this node.            
        min = minimum(h->heaparray[2*index], 2*index, h->heaparray[2*index+1], 2*index+1);
        
      // If this value is less than the current value, then we need to move
      // our current value down the heap.  
        if (h->heaparray[index] > h->heaparray[min]) {
            swap(h, index, min);
        
            // This part is recursive and allows us to continue percolating
            // down the element in question.
            percolateDown(h, min);
        }
    }
    
    // Case where our current element has exactly one child, a left child.
    else if (h->size == 2*index) {
         
        // Here we only compare the current item to its only child.
        // Clearly, no recursive call is needed since the child of this node
        // is a leaf. 
        if (h->heaparray[index] > h->heaparray[2*index]) 
            swap(h, index, 2*index);
    }
}
示例#10
0
	void heapify ( Rank n )//Floyd建堆算法
	{
		for ( int i = lastInternal ( n ); inHeap ( n, i ); i-- ) //自底而上,依次
		{
			percolateDown ( n, i ); //下滤各内部节点
		}
	}
示例#11
0
文件: binaryheap.cpp 项目: shixv/test
void deleteMin(Comparable& minItem)
{
	if(isEmpty())
		throw UnderflowException();
	minItem=array[1];
	array[1]=array[currentSize--];
	percolateDown(1);
}
示例#12
0
        void BinaryHeap<Comparable>::deleteMin( )
        {
            if( isEmpty( ) )
                throw Underflow( );

            array[ 1 ] = array[ currentSize-- ];
            percolateDown( 1 );
        }
示例#13
0
void percolateEitherWay(int size, void ** points, int index, LeastPathCompare * cmp, LeastPathCallback * callback) {
  if(index > 1 && cmp(points[index], points[index/2]) < 0) {
    percolateUp(size, points, index, cmp, callback);
  }
  else {
    percolateDown(size, points, index, cmp, callback);
  }
}
示例#14
0
  void heap::deleteMin(huffmanNode* minItem)
  {
    if(isEmpty())
      throw UnderflowException();

    minItem = array[1];
    array[1] = array[currentSize--];
    percolateDown(1);
  }
示例#15
0
文件: heapSort.c 项目: mcxiaoke/dsaac
// 建立最大堆
static void buildMaxHeap(int *data, int size) {
	int i;
	//由于所有树叶无需进行下滤(没有孩子)
	//所以只对0 - size/2的结点进行下滤即可
	for (i = size / 2 - 1; i >= 0; i--) {
		percolateDown(data, i, size);
	}

}
示例#16
0
文件: BinaryHeap.cpp 项目: compmix/60
        void BinaryHeap<Comparable>::deleteMax( Comparable & maxItem )
        {
            if( isEmpty( ) )
                throw Underflow( );

            maxItem = array[ 1 ];
            array[ 1 ] = array[ currentSize-- ];
            percolateDown( 1 );
        }
示例#17
0
        void BinaryHeap<Comparable>::deleteMin( int & minItem )
        {
            if( isEmpty( ) )
                throw Underflow( );

            minItem = cities[ 1 ]->current;
            array[ 1 ] = array[ currentSize-- ];
            percolateDown( 1 );
        }
示例#18
0
        void BinaryHeap<Comparable>::deleteMin( Comparable & minItem )
        {
//            if( isEmpty( ) )
//                throw Underflow( );

            minItem = array[ 1 ];
            array[ 1 ] = array[ currentSize-- ];
            percolateDown( 1 );
        }
示例#19
0
void deleteMin( ) {
	if (isEmpty()) {
		printf ("#error: deleteMin() called with <pq> empty\n");
		return;
	}

	pq.array[1] = pq.array[pq.currentSize--];
	percolateDown(1);
}
// delete minimal from binary heap based on percolateDown().
ElementType deleteMin(BinaryHeap bh)
{	
	ElementType* array = bh->array;	
	
	swap(array[1], array[bh->size]); // &variable means nickname of the variable
	bh->size-- ; // size-- 在下滤函数执行之前发生.
	percolateDown(1, bh) ;	

	return array[bh->size+1];
} 
示例#21
0
// h points to a heap structure that has values inside of it, but isn't yet
// organized into a heap and does exactly that.
void heapify(struct heapStruct *h) {
     
    int i;
     
    // We form a heap by just running percolateDown on the first half of the 
    // elements, in reverse order.
    for (i=h->size/2; i>0; i--) 
        percolateDown(h, i);
    
}
示例#22
0
Comparable minHeap<Comparable>::deleteMin() //delete the root of the minheap
{

  Comparable minItem = heapArray[1]; //the first item is the root item
  heapArray[1] = heapArray[currentSize--]; //decrement the heap
  //        exchanges++;
  percolateDown(1); //readjust the heap to accomadate for the item removed

  return minItem; //return the min item
}
// delete minimal from binary heap based on percolateDown().
ElementType deleteMin(BinaryHeap bh)
{		
	ElementType *data = bh->array;		

	swap(&data[1], &data[bh->size]); // &variable means nickname of the variable
	bh->size-- ; // size-- occurs prior to percolateDown(). 
	percolateDown(1, bh) ;	

	return data[bh->size+1];
} 
示例#24
0
Node * Heap::deleteMin()
{
  Node * min = heap[1];
  swap( 1, occupancy );
  heap[occupancy] = NULL;
  occupancy--;
  percolateDown( 1 );  
  return min;

}
示例#25
0
void* Heap::extract()
{
	if (size <= 0)
	{
		return NULL;
	}
	void *tmp = heap[0];
	swap(--size, 0);
	percolateDown(0);
	return tmp;
}
示例#26
0
文件: BinaryHeap.cpp 项目: compmix/60
        Comparable BinaryHeap<Comparable>::deleteMax( )
        {
            if( isEmpty( ) )
                throw Underflow( );

            Comparable temp = array[1];
            array[ 1 ] = array[ currentSize-- ];
            percolateDown( 1 );

            return temp;
        }
void
MSVehicleContainer::pop()

{
    if (isEmpty()) {
        throw 1;    //!!!Underflow( );
    }

    assert(array.size() > 1);
    array[ 1 ] = array[ currentSize-- ];
    percolateDown(1);
}
示例#28
0
void heapSort( std::vector<Comp> & array){
        std::cout<<"ENTERING HEAP SORT"<<std::endl;

        int N = array.size();
        for ( int j = N-1; j > 0; j--){
                /*swap the biggest element and the bottom of the heap  */
                std::cout << "\nSWAP:"<<"arr["<<0<<"]="<<array[0]<<" & "<<"arr["<<j<<"]="<<array[j]<<std::endl;
                std::swap( array[0], array[j]);
                percolateDown(array,0,j);
        }


}
示例#29
0
POINT* open_del() {
    if (size == 0) {
        return NULL;
    }

    POINT* min = heap[1];

    // Move last element to root and let it percolate down
    heap[1] = heap[size--];
    percolateDown(1);

    return min;
}
示例#30
0
void DeleteMin() 
{   
    if (hsize > 0) 
    {
        strcpy(h[1].first, h[hsize].first);
        strcpy(h[1].last, h[hsize].last);       
        hsize--;
        percolateDown(1);
    }
    
    else
        return;

}