Пример #1
0
//add a leaf to the tree.  heap must stay complete at all times.
void addLeaf(heap *h,data *d)
{
	//We already have code from binary trees that we can use to
	//fill the heap completely, this should closely mirror that code

	//If the heap is empty
	if(h->root == NULL)
	{
		//Create a new leaf to put in the heap
		leaf* new_leaf = createLeaf(d);
		//Set the root to be the new leaf.
		//Note that left, right and parent are all already appropriately set for the root node
		h->root = new_leaf;
		
		//This is definitely a heap, as it contains only one node.
		dequePushFront(h->q, new_leaf);
	} 
	//If the heap is not empty
	else 
	{
		//Then our node will get linked to the first node in the deque (previously this was a queue)
		leaf* parent = dequeFront(h->q);

		//If the parent does not have a left child, then this new node becomes the left child
		if(parent->left == NULL)
		{
			//Create a new leaf, and link it in appropriately
			leaf* new_leaf = createLeaf(d);
			new_leaf->parent = parent;
			parent->left = new_leaf;
			//Enqueue the leaf
			dequePushBack(h->q, new_leaf);

			//Now we may have violated the max-heap property, we can fix this by using percolate up
			percolateUp(new_leaf);
		}
		//Otherwise the parent node should only have space in the right child
		else 
		{
			//Just to be sure that the left child was non-null
			assert(parent->left != NULL && parent->right == NULL);
			//Create a new leaf, and link it in appropriately
			leaf* new_leaf = createLeaf(d);
			new_leaf->parent = parent;
			parent->right = new_leaf;
			//Enqueue the leaf
			dequePushBack(h->q, new_leaf);

			//Now the front of the deque does not have any more space, so remove it
			dequePopFront(h->q);

			//Now we may have violated the max-heap property, we can fix this by using percolate up
			percolateUp(new_leaf);
		}
	}

	return;
}
Пример #2
0
void DijkstraMaxFlow<edgeweighttype,maxflowtype>::add_to_temp_list(node_pointer node)
{
	if(node -> position_in_tempList < 0){
		tempList.push_back(node);
		node -> position_in_tempList = (int) tempList.size()-1;
		percolateUp(node);
	}
	else{
		percolateUp(node);
	}
}
Пример #3
0
//percolate up
void percolateUp(leaf *current)
{
	//Check to see if we have any place left to percolate
	if(current->parent == NULL)
	{
		//There is no place else to go
		return;
	}
	else
	{
		data* parent_data = current->parent->d;
		data* current_data = current->d;

		//If this violates the max-heap property
//		if(parent_data->val < current_data->val)
		//if(parent_data->distance > current_data->distance) //min heap		
		if(parent_data->distance < current_data->distance)		//max heap
		{
			//Then we should switch the data
			current->parent->d = current_data;
			current->d = parent_data;

			//Since we swapped data, we need to make sure our changes are okay on the way up
			percolateUp(current->parent);
		}
		return;
	}
}
Пример #4
0
void MyPriorityQueue::setP(int index, double P)
{
	struct MyNode* node = getElement(index);
	node->P = P;
	percolateUp(index);
	return;
}
Пример #5
0
// Inserts value into the heap pointed to by h.
void insert(struct heapStruct *h, int value) {

    int* temp;
    int* throwaway;
    int i;
    
    // Our array is full, we need to allocate some new space!
    if (h->size == h->capacity) {
                
        // We will double the size of the structure.
        h->capacity *= 2;
      
        // Allocate new space for an array.
        temp = (int*)malloc(sizeof(int)*h->capacity+1);
      
        // Copy all the items over.
        for (i=1; i<=h->capacity; i++)
            temp[i] = h->heaparray[i];
            
        // Move the pointer and free the old memory.
        throwaway = h->heaparray;
        h->heaparray = temp;
        free(throwaway);
    }
      
    // Adjust all the necessary components of h, and then move the inserted
    // item into its appropriate location.  
    h->size++;
    h->heaparray[h->size] = value;
    percolateUp(h, h->size);
}
Пример #6
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);
  }
}
Пример #7
0
void heapSort() {
	int i;

	for (i = SIZE / 2;i > 0;i--)
		percolateUp(i); /* Build heap */

	for (i = SIZE;i > 0;i--)
		Arrays[i] = deleteMax();
}
// Attention, the index of the heap starts from 1
// insert the value into binary heap based on percolateUp().
void insert(ElementType value, BinaryHeap bh)
{	
	if(isFull(bh))
	{
		Error("failed insertion , for the BinaryHeap is full, from func insert!");
		return ;	
	}
	bh->array[++bh->size] = value; // startup is 1 not 0.
	percolateUp(bh->size, bh);
}
Пример #9
0
void Heap::insert(Node * node)
{
  if (occupancy >= 27){
    cout<< "Error: Not Enough Space!\n";
    return;
  }
  occupancy++;
  heap[occupancy] = node;
  percolateUp( occupancy );

} 
Пример #10
0
// Runs percolate up on the heap pointed to by h on the node stored in index.
void percolateUp(struct heapStruct *h, int index) {
    // Can only percolate up if the node isn't the root.
    if (index > 1) {
        // See if our current node is smaller in value than its parent.
        if (h->heaparray[index/2] > h->heaparray[index]) {
            // Move our node up one level.
            swap(h, index, index/2);
            // See if it needs to be done again.
            percolateUp(h, index/2);
        }
    }
}
Пример #11
0
/*update the priority of a node n to the given priority w*/
void decreasePriority(struct heap *h, int n, float w) {
	int j;
	/*need to decrease priority for both Prim's and Djikstra's algorithm*/
	for(j = 1; j < (h->size); j++) {
		/*need to percolate up once we change node's weight*/
		if (n == h->array[j]->priority) {
			h->array[j]->weight = w;
			percolateUp(h, j);
			break;	/*need to break once we find the right node*/
		}
	}
}
Пример #12
0
bool Heap::insert(void *e)
{
	if (size >= HEAP_BUFFER_SIZE)
	{
		fprintf(stderr, "Error: heap_insert: buffer overflow\n");
		return 1;
	}
	heap[size] = e;
	percolateUp(size);
	size++;
	return 0;
}
Пример #13
0
//===============================================================================================
void percolateUp(int arrpos) 
{

    if (arrpos > 1) 
    {
        if ( comparestring(h[arrpos/2].first,h[arrpos].first,h[arrpos/2].last,h[arrpos].last) > 0) 
        {
            interchange(arrpos, arrpos/2);
            percolateUp(arrpos/2);
        }
    }
}
Пример #14
0
  // should be called insert not nut, but does not comnile, b/c
  // cannot override the suner class function with different 
  // return tyne
  virtual std::pair<typename BST<Data>::iterator, bool> insert(const Data& item)  {
    std::pair<iterator, bool> target = BST<Data>::insert(item);
    if(target.second)  {

      BSTNode<Data>* newNode = target.first.curr;
      newNode->info = rand();        
      //std::cout << "PRINTING PRIORITY of " <<newNode->data << "  is:  " <<newNode-> info <<std::endl; 
      percolateUp(newNode); 
      return std::pair<iterator, bool>(iterator(newNode),target.second);
    }
    else    
      return target;
  }
Пример #15
0
void insert(struct heap *h , int data , int priority)
{
	if(h -> count == h -> capacity)
	{
		resizeHeap(h) ;
		printf("resizing...\n");
		// printf("heap full\n");
		// return ;
	}
	(h -> array[h -> count]).dat = data ;
	(h -> array[h -> count]).priority = priority ;
	h -> count++ ;
	percolateUp(h , h -> count - 1) ;
}
Пример #16
0
/**
 *  Takes in a key and priority and adds them to numsVec 
 *  and nums Map. Adds to numsVec as a pair.
 * 
 *  @param key key, integer
 *  @param priority priority, integer
 */
void priorityqueue62::push(int key, int priority) {
	//check that key does not exist already in our map
	assert(!is_present(key));

	//increment our queue size
	priorityqueue62::queue_size++;

	//add to our vector and percolate up
	numsVec.push_back(make_pair(key,priority));
	percolateUp(numsVec.size() - 1);

	//add to our map
	numsMap.insert(make_pair(key,priority));
}
// Attention, the index of the heap starts from 1
// insert the weight into binary heap based on percolateUp().
void insert(ElementType e, BinaryHeap bh)
{		
	if(e == NULL)
	{
		Error("failed insertion , for e is NULL.");
		return;
	}
	if(isFull(bh))
	{
		Error("failed insertion , for the BinaryHeap is full.");
		return ;	
	}
	bh->array[++bh->size] = e; // startup is 1 not 0.
	percolateUp(bh->size, bh);
}
Пример #18
0
int heap::insert(const std::string &id, int key, void * pv) {
	if (size == capacity) {
		return 1;
	}

	if (mapping->contains(id)) {
		return 2;
	}

	data[++size] = node(id, key, pv);
	mapping->insert(id, &data[size]);
	percolateUp(size);

	return 0;
}
Пример #19
0
void percolateUp(struct heap *h , int i)
{
	if(i == 0)
		return ;
	int p = (i-1)/2 ;
	// printf("%d is the parent priority and %d is the passes priority\n", h -> array[p].priority , h -> array[i].priority );
	if(h -> array[i].priority < h -> array[p].priority)
	{
		// printf("in if statement\n");
		struct data temp = h -> array[i] ;
		h -> array[i] = h -> array[p] ;
		h -> array[p] = temp ;
	}
	else
		return ;
	percolateUp(h , p) ;
}
Пример #20
0
void MyPriorityQueue::percolateUp(int index)
{
	struct MyNode* node =  getElement(index);
	
	int parent = ceil( (double)((index - 1) / 2) );
	int left = 2*index + 1;
	int right = 2*index + 2;

	if( pq[index]->P > pq[parent]->P)
	{
		struct MyNode * temp = pq[index];
		pq[index] = pq[parent];
		pq[index]->PQind = index;
		pq[parent] = temp;
		pq[parent]->PQind = parent;

		percolateUp(parent);
	}
	
	return;
}
Пример #21
0
int heap::remove(const std::string &id, int * pKey, void * ppData) {
	// Return 1 if node with given id does not exist
	if (!mapping->contains(id)) {
		return 1;
	}

	// Set optional data parameters
	node * pn = static_cast<node *>(mapping->getPointer(id));

	if (pKey) {
		*pKey = pn->key;
	}
	if (ppData) {
		ppData = pn->pData;
	}

	// Set key to least possible value, percolate up, then deleteMin
	pn->key = INT_MIN;
	percolateUp(getPos(pn));
	deleteMin();

	return 0;
}
Пример #22
0
int heap::setKey(const std::string &id, int key) {
	// Return 1 if node with given id does not exist
	if (!mapping->contains(id)) {
		return 1;
	}

	// Swap out old key for new key
	node * pn = static_cast<node *>(mapping->getPointer(id));
	int oldKey = pn->key;
	pn->key = key;
	int hole = getPos(pn);

	// If new key is a reduction, percolate up
	if (key < oldKey) {
		percolateUp(hole);
	}

	// If new key is an increase, percolate down
	if (key > oldKey) {
		percolateDown(hole);
	}

	return 0;
}
Пример #23
0
template<typename T> void PQ_ComplHeap<T>::insert(T e) //插入
{
    Vector<T>::insert(e);
    percolateUp( _size - 1);
}
Пример #24
0
int main()
{
	int t,l;
	int k;
	char a[100],b[100];
	char c[100];
	scanf("%lld",&t);
	
	while(t--)
	{
		scanf("%s",c);
		if(strcmp("InitHeap",c)==0)
		{
			hsize++;
			scanf("%s",a);
			scanf("%s",b);
			strcpy(h[hsize].first,a);
			strcpy(h[hsize].last,b);
			h[hsize].arrpos=hsize;
			percolateUp(hsize);
			}
		else if(strcmp("Insert",c)==0)
		{
			hsize++;
			scanf("%s",a);
			scanf("%s",b);
			strcpy(h[hsize].first,a);
			strcpy(h[hsize].last,b);
			h[hsize].arrpos=hsize;
			percolateUp(hsize);
			k=find(a,b);
			printf("%d\n",k);
		}
		else if(strcmp("FindMin",c)==0)
		{	
			if(hsize>=1)
			printf("%s %s\n",h[1].first,h[1].last);
			else
			printf("-1\n");		
		}
		else if(strcmp("DeleteMin",c)==0)
		{
			if(hsize<1)
				printf("-1\n");
			else
			{
				printf("%s %s\n",h[1].first,h[1].last);
				DeleteMin();
			}
		}
		else if(strcmp("Delete",c)==0)
		{
			scanf("%lld",&l);
			if(l==0)
			printf("-1\n");
			else if(hsize<l)  
			printf("-1\n");
			else
			{	
				printf("%s %s\n",h[l].first,h[l].last);
				deletepos(l);
			}
		}
	}
	return 0;
}