Ejemplo n.º 1
0
void deleteElement(Tree *&t, int value)
{
	if (t == NULL)
		return;
	

		if (value < t->value)
			deleteElement(t->left, value);
		else
		if (value > t->value)
				deleteElement(t->right, value);
			else
				if (t->left == NULL && t->right == NULL)
				{
					delete t;
					t = NULL;
				}
				else
					if (t->left == NULL)
					{
						t = t->right;
						delete t->right;
						t->right = NULL;
					}
					else
						if (t->right == NULL)
						{
							t = t->left;
							delete t->left;
							t->left = NULL;
						}

						else
							t->value = deleteMin(t->right);
	}
Ejemplo n.º 2
0
		void clear()
		{
			while(root != NULL)
			{
				deleteMin();
			}
		}
int main()
{
	PriorityQueue h1;
	PriorityQueue h2;	
	int data[] =  {21, 10, 23, 14, 3, 26, 17, 8};	
	int data2[] = {18, 12, 33, 24, 6, 37, 7, 18};	
	int i;

	h1 = insert(data[0], NULL);
	for(i=1; i<8; i++)
	{
		h1 = insert(data[i], h1);
	}
	printf("\n=== after the leftist heap h1 is merged===\n");
	printPreorder(1, h1);

	h2 = insert(data2[0], NULL);
	for(i=1; i<8; i++)
	{
		h2 = insert(data2[i], h2);
	}
	printf("\n=== after the leftist heap h2 is merged===\n");
	printPreorder(1, h2);
	 
	h1 = merge(h1, h2);
	printf("\n=== after both h1 and h2 are merged===\n");
	printPreorder(1, h1);

	h1 = deleteMin(h1);
	printf("\n=== after executing deleteMin operation ===\n");
	printPreorder(1, h1);

	return  0;
}
Ejemplo n.º 4
0
int main()
{
    int heap[1000]={INT_MIN};
    int k,j,t,l,m,n;
    int size = 0;

    printf("Inserting to the heap\n");
    printf("Enter the number of numbers to be inserted to the heap\n");
    scanf("%d",&n);
    printf("Enter %d numbers to heap\n",n);
    for(j=0;j<n;j++)
    {
        scanf("%d",&k);
        insert(heap,k,&size);

    }
    printf("Print heap\n");
    printHeap(heap,size);
    printf("Minimum value in heap:%d\n",getMin(heap));
    printf("Deleting the minimum value\n");
    deleteMin(heap,&size);
    printf("Minimum value in heap:%d\n",getMin(heap));
    printHeap(heap,size);
    heapSort(heap,size);
    printf("After heap sort\n");
    printHeap(heap,size);
    return 0;



}
Ejemplo n.º 5
0
void STdeleteMin(link head) {
    if (STcount(head) == 0)
    { printf("Underflow"); exit(1); }
    
    if (!head->l->red && !head->r->red) head->red = 1;
    
    head = deleteMin(head);
    if (STcount(head) > 0) head->red = 0;
}
Ejemplo n.º 6
0
int main(void){

	//printf("Queue is Empty = %d\n", isEmpty());
	//printf("Queue Size = %d\n", queueSize());


	acesendingInsert(2, 2);
	acesendingInsert(5, 5);
	acesendingInsert(1, 1);
	acesendingInsert(4, 4);
	acesendingInsert(3, 3);
	acesendingInsert(6, 6);
	acesendingInsert(8, 8);
	acesendingInsert(7, 7);
	acesendingInsert(0, 0);
	acesendingInsert(10, 10);


	printf("frontData = %d\n", frontData());
	printf("rearData = %d\n", rearData());
	printf("Queue is %sempty\n", ((isEmpty()) ? "": "not "));
	printf("Queue Size = %d\n", queueSize());

	printf("Retrieved Number = %d\n", deleteMin());
	printf("Retrieved Number = %d\n", deleteMin());
	printf("Retrieved Number = %d\n", deleteMin());
	printf("Retrieved Number = %d\n", deleteMin());
	printf("Retrieved Number = %d\n", deleteMin());
	printf("Retrieved Number = %d\n", deleteMin());
	printf("Retrieved Number = %d\n", deleteMin());
	printf("Retrieved Number = %d\n", deleteMin());
	printf("Retrieved Number = %d\n", deleteMin());
	printf("Retrieved Number = %d\n", deleteMin());




	printf("Queue is %sempty\n", ((isEmpty()) ? "": "not "));
	printf("Queue Size = %d\n", queueSize());


	return 0;
}
Ejemplo n.º 7
0
int main(void)
{
	PriorityQueue<int> queue;						//create queue
	int test = 1;									//assign test case value
	
	testCase(test);									//print out test case no.
	queue.add(19);									//add items to queue
	queue.add(7);
	queue.add(1);
	queue.add(5);
	queue.add(3);
	queue.add(20);
	queue.add(15);
	queue.add(5);
	printQueue(queue);								//print queue
	
	testCase(test);									//print out test case no.
	sizeQueue(queue);								//queue size

	testCase(test);									//print out test case no.
	minQueue(queue);								//retrieve minimum element

	testCase(test);									//print out test case no.
	while (!queue.empty())							//cycle until empty
	{
		deleteMin(queue);							//delete minimum element
		printQueue(queue);							//print queue
	}

	testCase(test);									//print out test case no.
	sizeQueue(queue);								//queue size

	testCase(test);									//print out test case no.
	minQueue(queue);								//retrieve minimum element

	testCase(test);									//print out test case no.
	deleteMin(queue);								//delete minimum element
	return EXIT_SUCCESS;							//exit success
}
Ejemplo n.º 8
0
int deleteMin(Tree *&t)
{
	if (t->left == NULL)
	{
		int result = t->value;
		//t = t->right;
		delete t;
		t = NULL;
		return result;
	}
	else
		return deleteMin(t->left);
}
Ejemplo n.º 9
0
BinaryTreeNode<Key, Value>* BinarySearchTree<Key, Value>::deleteMin(
		BinaryTreeNode<Key, Value>* subRoot, BinaryTreeNode<Key, Value>*& pNode)
{
	if (subRoot->leftChild())
	{ // find the minest
		subRoot->setLeftChild(deleteMin(subRoot->leftChild(), pNode));
		return subRoot;
	}
	else
	{  // find it! and return it's right;
		pNode = subRoot;
		return subRoot->rightChild();
	}
}
Ejemplo n.º 10
0
int main(int argc, char *argv[]) {
	int i,j;
	int arr[]={23,43,26,10,21,13,31,16,12,8,29,11,19,17};
	int N=sizeof(arr)/sizeof(int);
	buildHeap(arr,N);
	for(i=0;i<N;i++){
		printf("%d ",arr[i]);
	}
	printf("\n");
	j=N;
	for(i=0;i<N;i++){
		printf("%d\n", deleteMin(arr, j));
		j--;
	}
	return 0;
}
Ejemplo n.º 11
0
BST::BST(std::istream& input)
{
	rootPtr = nullptr;//sets the head pointer to nullptr

	int something;

	while (input >> something)
	{
		std::cout << something << " ";
        insert(something); //initially inserts everything from the data file		
	}
    std::cout << std::endl;
	preorder(rootPtr);
	deleteMin();
	preorder(rootPtr);
}
Ejemplo n.º 12
0
void handle_top_interrupt(){
	while(!isEmpty()){
		print_no_more = false;
		struct interrupt topInterrupt = findMin();
		if(topInterrupt.instr_no == isa_inst_count){
			printf("Handling current interrupt(s)   %d \n", isa_inst_count);
			deleteMin();
			// handle interrupt
			interrupt_handler(topInterrupt);
			if(!ke->running_list_head){
				printf("Something wrong happened 22\n");
			}
		}	
		else break;
	}
}
Ejemplo n.º 13
0
BinaryTreeNode<Key, Value>* BinarySearchTree<Key, Value>::removeHelper(
		BinaryTreeNode<Key, Value>* subRoot, const Key& key,
		BinaryTreeNode<Key, Value>*& pNode)
{
	if (subRoot == NULL)
	{
		return NULL;
	}
	else if (subRoot->keyEquals(key) > 0)
	{
		subRoot->setRightChild(removeHelper(subRoot->rightChild(), key, pNode));
	}
	else if (subRoot->keyEquals(key) < 0)
	{
		subRoot->setLeftChild(removeHelper(subRoot->leftChild(), key, pNode));
	}
	else
	{ // remove target is current node;
		BinaryTreeNode<Key, Value> *pTmp;
		pNode = subRoot;
		if (subRoot->leftChild() == NULL)
		{ // left child is null,
			subRoot = subRoot->rightChild();
		}
		else if (subRoot->rightChild() == NULL)
		{
			subRoot = subRoot->leftChild();
		}
		else
		{ // both children are not null, remove it's successor
			Value rTmpValue;
			subRoot->value(rTmpValue);
			subRoot->setRightChild(deleteMin(subRoot->rightChild(), pTmp));

			Value rPValue;
			Key rPKey;
			pTmp->value(rPValue);
			pTmp->key(rPKey);
			subRoot->setValue(rPValue);
			subRoot->setKey(rPKey);
			pTmp->setValue(rTmpValue);
			pNode = pTmp;
		}

	}
	return subRoot;
}
Ejemplo n.º 14
0
void find_paths(WeightedPoint ** paths, Point start, int cut_corners, int * weights, int rows, int cols) {
  WeightedPoint * distances[rows * cols];
  int ** visited;
  int size = 0;
  visited = calloc(rows, sizeof(int*));
  int r;
  for(r = 0; r < rows; r++) {
    visited[r] = calloc(cols, sizeof(int));
    int c;
    for(c = 0; c < cols; c++) {
      distances[size + 1] = &paths[r][c];
      distances[size + 1] -> point = (Point){c, r};
      if(c > 0 && c < cols - 1 && r > 0 && r < rows - 1) {
        distances[size + 1] -> index = size + 1;
        if(r == start.y && c == start.x) { distances[size + 1] -> weight = 0; }
        else { distances[size + 1] -> weight = UNVISITED_SQUARE; }
        size++;
        visited[r][c] = 0;
      }
      else {
        visited[r][c] = 1;
      }
      paths[r][c].prev.x = 0;
      paths[r][c].prev.y = 0;
    }
  }
  init_heap(size, (void**)distances, cmp_weighted_points, weighted_point_callback);
  while(size) {
    WeightedPoint * u = deleteMin(size--, (void**)distances, cmp_weighted_points, weighted_point_callback);
    explore_path(u->point.x - 1, u->point.y, size, cols, u, visited, paths, distances, weights, 0);
    explore_path(u->point.x + 1, u->point.y, size, cols, u, visited, paths, distances, weights, 0);
    explore_path(u->point.x, u->point.y - 1, size, cols, u, visited, paths, distances, weights, 0);
    explore_path(u->point.x, u->point.y + 1, size, cols, u, visited, paths, distances, weights, 0);
    if(cut_corners) {
      explore_path(u->point.x - 1, u->point.y + 1, size, cols, u, visited, paths, distances, weights, 1);
      explore_path(u->point.x + 1, u->point.y - 1, size, cols, u, visited, paths, distances, weights, 1);
      explore_path(u->point.x - 1, u->point.y - 1, size, cols, u, visited, paths, distances, weights, 1);
      explore_path(u->point.x + 1, u->point.y + 1, size, cols, u, visited, paths, distances, weights, 1);
    }
    visited[u->point.y][u->point.x] = 1;
  }
  for(r = 0; r < rows; r++) {
    free(visited[r]);
  }
  free(visited);
}
Ejemplo n.º 15
0
static RBTreeNodeHandle rbtree_delete_priv(RBTreeNodeHandle root, i64 key, RBTreeNodeHandle *deleteNode){
    if (NULL == root)
        return NULL;
    
    if (key < root->key){
        if (NULL == root->left)
            return NULL;
        
        /*delete minimal node*/
        if (!isRed(root->left) && !isRed(root->left->left))
            root = moveRedLeft(root);
        
        root->left = rbtree_delete_priv(root->left, key, deleteNode);
    }else{
        if (isRed(root->left))
            root = rotateRight(root);

        /*delete leaf node*/
        if ((root->right == NULL) && (root->key == key)){
            /*AGILE_LOGD("find the key: %lld", key);*/
            *deleteNode = root;
            return NULL;
        }

        if (NULL != root->right){
            if (!isRed(root->right) && !isRed(root->right->left))
                root = moveRedRight(root);
            
            if (root->key == key){
                RBTreeNodeHandle min_node = NULL;
                min_node = min(root->right);
                AGILE_LOGD("the min key: %lld", min_node->key);
                root->key = min_node->key;
                root->value = min_node->value;
                root->right = deleteMin(root->right, deleteNode);
                /**deleteNode = min_node;*/
            }else{
                root->right = rbtree_delete_priv(root->right, key, deleteNode);
            }
        }
    }

    return fixup(root);
}
Ejemplo n.º 16
0
void tesetPriorityQueue(){
    PriorityQueue *q = makePriorityQueue(5);
    insert(q, 10);
    insert(q, 9);
    insert(q, 29);
    insert(q, 1);
    insert(q, 3);
    insert(q, 23);
    
    printf("min:%d\n", deleteMin(q));
    printf("min:%d\n", deleteMin(q));
    printf("min:%d\n", deleteMin(q));
    printf("min:%d\n", deleteMin(q));
    printf("min:%d\n", deleteMin(q));
    printf("min:%d\n", deleteMin(q));
    printf("min:%d\n", deleteMin(q));
}
Ejemplo n.º 17
0
int main(int argc, char *argv[])
{
	int n;
	while(scanf("%d", &n) != EOF) {
		hs = 0;
		memset(heap, 0, sizeof(heap));
		while(n--) {
			int a;
			scanf("%d", &a);
			insert(a);
		}
		for(int i = 0; i <= hs; i++)
			printf("%d ", heap[i]);
		printf("\n");
		printf("The Minnum is %d\n", deleteMin());
	}
	
	return 0;
}
Ejemplo n.º 18
0
link deleteR(link h, Key v) {
    Key t = key(h->item);
    
    if (less(v,t)) {
        if (!hl->red && !hll->red) h = mvRedL(h);
        hl = deleteR(hl, v);
    }
    else {
        if (hl->red) h = rotR(h);
        if (eq(v,key(h->item)) && hr == z)
            free(h); return z;
        if (!hr->red && !hrl->red) h = mvRedR(h);
        if (eq(v,key(h->item))) {
            h->item = getMin(hr);
            hr = deleteMin(hr);
        }
        else hr = deleteR(hr, v);
    }
    return balance(h);
}
void insertMed(int *H,int n,int& N1,int& N2,int x)   // inserts an element in the med heap
{
  int m=findMedian(H,n,N1,N2);
  if(m==-1){H[n-1]=x;N2++;return;}
  if(x<=m)
  {
    if(N2==N1+1)insertMax(H,N1,x);
    else
    {
       int max=findMax(H,N1);deleteMax(H,N1);
       insertMin(H+n-N2,N2,max);insertMax(H,N1,x);
    }
  }
  else
  {
    if(N2==N1)insertMin(H+n-N2,N2,x);
    else
    {
      int min=findMin(H+n-N2,N2);deleteMin(H+n-N2,N2);
      insertMax(H,N1,min);insertMin(H+n-N2,N2,x);  
    }
  }
}
Ejemplo n.º 20
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;
}
 /**
  * @param root: The root of the binary search tree.
  * @param value: Remove the node with given value.
  * @return: The root of the binary search tree after removal.
  */
 TreeNode* removeNode(TreeNode* root, int value) {
     if (root == nullptr) {
         return nullptr;
     }
     if (value < root->val) {
         root->left = removeNode(root->left, value);
     }
     else if (value > root->val) {
         root->right = removeNode(root->right, value);
     }
     else {  // Find the target node.
         if (root->left == nullptr) {
             return root->right;
         } else if (root->right == nullptr) {
             return root->left;
         }
         TreeNode *node = root;
         root = findMin(node->right);
         root->right = deleteMin(node->right);
         root->left = node->left;
     }
     return root;
 }
Ejemplo n.º 22
0
int *astar_unopt_compute (const char *grid, 
		    int *solLength, 
		    int boundX, 
		    int boundY, 
		    int start, 
		    int end)
{
	astar_t astar;

	if (!init_astar_object (&astar, grid, solLength, boundX, boundY, start, end))
		return NULL;


	//coord_t bounds;
	//struct coord_t;// bounds;//endCoord;
	//bounds = {boundX, boundY};
	//endCoord = getCoord (bounds, end);
	
	struct coord_t bounds, endCoord;
	bounds.x = boundX; bounds.y = boundY;
	endCoord = getCoord(bounds, end);

	while (astar.open->size) {
		int dir;
		int node = findMin (astar.open)->value; 
		struct coord_t nodeCoord = getCoord (bounds, node);
		if (nodeCoord.x == endCoord.x && nodeCoord.y == endCoord.y) {
			freeQueue (astar.open);
			free (astar.closed);
			free (astar.gScores);

			int *rv = recordSolution (&astar);

			free (astar.cameFrom);

			return rv;
		}

		deleteMin (astar.open);
		astar.closed[node] = 1;

		for (dir = 0; dir < 8; dir++)
		{
			struct coord_t newCoord = adjustInDirection (nodeCoord, dir);
			int newNode = getIndex (bounds, newCoord);

			if (!contained (bounds, newCoord) || !grid[newNode])
				continue;

			if (astar.closed[newNode])
				continue;
			
			addToOpenSet (&astar, newNode, node);

		}
	}
	freeQueue (astar.open);
	free (astar.closed);
	free (astar.gScores);
	free (astar.cameFrom);
	return NULL;
}
void deleteMed(int *H,int n,int& N1,int& N2)       // deletes an element from the med heap
{
  if(N1==N2) {deleteMax(H,N1);return;}
  else {deleteMin(H+n-N2,N2);return;}
}
Ejemplo n.º 24
0
// prints the heap.
void print(HeapStruct heap)
{
	while(!HIsEmpty(heap))
		printf("%d ", deleteMin(heap));
	printf("\n");
}
Ejemplo n.º 25
0
void pqueue_delete(pqueue_t *q, int ind)
{
	pqueue_change_priority (q, ind, INT_MIN);
	deleteMin (q);
}
Ejemplo n.º 26
0
int *astar_unopt_compute (const char *grid, 
		    int *solLength, 
		    int boundX, 
		    int boundY, 
		    int start, 
		    int end)
{
	astar_t astar;
	coord_t bounds = {boundX, boundY};

	int size = bounds.x * bounds.y;


	if (start >= size || start < 0 || end >= size || end < 0)
		return NULL;

	coord_t startCoord = getCoord (bounds, start);
	coord_t endCoord = getCoord (bounds, end);

	if (!contained (bounds, startCoord) || !contained (bounds, endCoord))
		return NULL;

	queue *open = createQueue();
	char closed [size];
	double gScores [size];
	int cameFrom [size];

	astar.solutionLength = solLength;
	*astar.solutionLength = -1;
	astar.bounds = bounds;
	astar.start = start;
	astar.goal = end;
	astar.grid = grid;
	astar.open = open;
	astar.closed = closed;
	astar.gScores = gScores;
	astar.cameFrom = cameFrom;

	memset (closed, 0, sizeof(closed));

	gScores[start] = 0;
	cameFrom[start] = -1;

	insert (open, start, estimateDistance (startCoord, endCoord));

	while (open->size) {
		int node = findMin (open)->value; 
		coord_t nodeCoord = getCoord (bounds, node);
		if (nodeCoord.x == endCoord.x && nodeCoord.y == endCoord.y) {
			freeQueue (open);
			return recordSolution (&astar);
		}

		deleteMin (open);
		closed[node] = 1;

		for (int dir = 0; dir < 8; dir++)
		{
			coord_t newCoord = adjustInDirection (nodeCoord, dir);
			int newNode = getIndex (bounds, newCoord);

			if (!contained (bounds, newCoord) || !grid[newNode])
				continue;

			if (closed[newNode])
				continue;
			
			addToOpenSet (&astar, newNode, node);

		}
	}
	freeQueue (open);
	return NULL;
}
Ejemplo n.º 27
0
int *astar_compute (const char *grid, 
		    int *solLength, 
		    int boundX, 
		    int boundY, 
		    int start, 
		    int end)
{
	*solLength = -1;
	astar_t astar;
	coord_t bounds = {boundX, boundY};

	int size = bounds.x * bounds.y;


	if (start >= size || start < 0 || end >= size || end < 0)
		return NULL;

	coord_t startCoord = getCoord (bounds, start);
	coord_t endCoord = getCoord (bounds, end);

	if (!contained (bounds, startCoord) || !contained (bounds, endCoord))
		return NULL;

	queue *open = createQueue();
	char closed [size];
	double gScores [size];
	int cameFrom [size];

	astar.solutionLength = solLength;
	astar.bounds = bounds;
	astar.start = start;
	astar.goal = end;
	astar.grid = grid;
	astar.open = open;
	astar.closed = closed;
	astar.gScores = gScores;
	astar.cameFrom = cameFrom;

	memset (closed, 0, sizeof(closed));

	gScores[start] = 0;
	cameFrom[start] = -1;

	insert (open, start, estimateDistance (startCoord, endCoord));
	while (open->size) {
		int node = findMin (open)->value; 
		coord_t nodeCoord = getCoord (bounds, node);
		if (nodeCoord.x == endCoord.x && nodeCoord.y == endCoord.y) {
			freeQueue (open);
			return recordSolution (&astar);
		}

		deleteMin (open);
		closed[node] = 1;

		direction from = directionWeCameFrom (&astar, 
						      node,
						      cameFrom[node]);

		directionset dirs = 
			forcedNeighbours (&astar, nodeCoord, from) 
		      | naturalNeighbours (from);

		for (int dir = nextDirectionInSet (&dirs); dir != NO_DIRECTION; dir = nextDirectionInSet (&dirs))
		{
			int newNode = jump (&astar, dir, node);
			coord_t newCoord = getCoord (bounds, newNode);

			// this'll also bail out if jump() returned -1
			if (!contained (bounds, newCoord))
				continue;

			if (closed[newNode])
				continue;
			
			addToOpenSet (&astar, newNode, node);

		}
	}
	freeQueue (open);
	return NULL;
}
Ejemplo n.º 28
0
void deleteq (queue *q, int ind)
{
	changePriority (q, ind, INT_MIN);
	deleteMin (q);
}
int main()
{
	int data[] = {85, 80, 40, 30, 10, 70, 110}; // P141	
	int buildHeapData[] = {150, 80, 40, 30, 10, 70, 110, 100, 20, 90, 60, 50, 120, 140, 130};
	BinaryHeap bh;	
	int size;
	int i;	
	int capacity;
	Distance tempDisStruct;
	int *indexOfVertexInHeap;
	
	printf("\n\t=== test for inserting the binary heap with {85, 80, 40, 30, 10, 70, 110} in turn ===\n");
	capacity = 14;
	bh = initBinaryHeap(capacity);
	size = 7;	
	
	tempDisStruct = makeEmptyDistance(); 
	indexOfVertexInHeap = makeEmptyArray(size);

	for(i = 0; i < size; i++) 
	{
		tempDisStruct->distance = data[i];
		tempDisStruct->vertexIndex = i;
		insert(tempDisStruct, bh, indexOfVertexInHeap);
	}	
	printBinaryHeap(bh);
	printIndexOfVertexInHeap(bh->size, indexOfVertexInHeap);

	printf("\n\t=== test for inserting the binary heap with element {100, 20, 90} in turn ===\n");
	
	tempDisStruct->distance = 100;
	tempDisStruct->vertexIndex = size;
	insert(tempDisStruct, bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);

	tempDisStruct->distance = 20;
	tempDisStruct->vertexIndex = size+1;
	insert(tempDisStruct, bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);

	tempDisStruct->distance = 90;
	tempDisStruct->vertexIndex = size+2;
	insert(tempDisStruct, bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);

	printIndexOfVertexInHeap(bh->size, indexOfVertexInHeap);

	printf("\n\t=== test for inserting the binary heap with 5 ===\n");	
	tempDisStruct->distance = 5;
	tempDisStruct->vertexIndex = size+3;
	insert(tempDisStruct, bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);

	printf("\n\t=== test for 3 deletings towards the minimum in binary heap ===\n");
	deleteMin(bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);
	deleteMin(bh, indexOfVertexInHeap);		
	printBinaryHeap(bh);
	deleteMin(bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);
}
Ejemplo n.º 30
0
int *astar_compute (const char *grid, 
		    int *solLength, 
		    int boundX, 
		    int boundY, 
		    int start, 
		    int end)
{
	astar_t astar;
	if (!init_astar_object (&astar, grid, solLength, boundX, boundY, start, end))
		return NULL;

	struct coord_t bounds = {boundX, boundY};
	struct coord_t endCoord = getCoord (bounds, end);

	while (astar.open->size) {
		int dir;
		int node = findMin (astar.open)->value; 
		struct coord_t nodeCoord = getCoord (bounds, node);
		if (nodeCoord.x == endCoord.x && nodeCoord.y == endCoord.y) {
			freeQueue (astar.open);
			free (astar.closed);
			free (astar.gScores);

			int *rv = recordSolution (&astar);

			free (astar.cameFrom);

			return rv;
		}

		deleteMin (astar.open);
		astar.closed[node] = 1;

		direction from = directionWeCameFrom (&astar, 
						      node,
						      astar.cameFrom[node]);

		directionset dirs = 
			forcedNeighbours (&astar, nodeCoord, from) 
		      | naturalNeighbours (from);

		for (dir = nextDirectionInSet (&dirs); dir != NO_DIRECTION; dir = nextDirectionInSet (&dirs))
		{
			int newNode = jump (&astar, dir, node);
			struct coord_t newCoord = getCoord (bounds, newNode);

			// this'll also bail out if jump() returned -1
			if (!contained (bounds, newCoord))
				continue;

			if (astar.closed[newNode])
				continue;
			
			addToOpenSet (&astar, newNode, node);

		}
	}
	freeQueue (astar.open);
	free (astar.closed);
	free (astar.gScores);
	free (astar.cameFrom);

	return NULL;
}