heap_node dequeue (PQ *q) {
    heap_node rv = remove_min(q->heap, q->size);
    
    --q->size;
    
    return rv;
}
Пример #2
0
int main()
{
	int arr[MAX] = { 9, 6, 3, 8, 5, 1, 4, 2, 0, 7 };
	int i, heap[MAX];

	printf("testing insert...");
	for (i = 0; i < MAX; i++) {
		insert(heap, i, arr[i]);
		if (!check_heap(heap, i+1))
			goto fail;
	}
	printf("[ok]\n");

	printf("testing remove_min...");
	for (i = 0; i < MAX; i++) {
		if (remove_min(heap, MAX - i) != i)
			goto fail;
		if (!check_heap(heap, MAX - i - 1))
			goto fail;
	}
	printf("[ok]\n");

	return 0;
fail:
	printf("[failed]\n");
	return -1;
}
int remove_min(tree* t){
    if (t==NULL){
        return 0;
    }else if(t->left!=NULL){
        return remove_min(t->left);
    }
    tree* tmp = t;
    t=t->rigth;
    free(tmp);
}
Пример #4
0
void heapsort(int *arr, int size)
{
	int *heap, i;

	heap = (int *) malloc(size * sizeof(int));

	for (i = 0; i < size; i++)
		insert(heap, i, arr[i]);

	for (i = 0; i < size; i++)
		arr[i] = remove_min(heap, size - i);
}
Пример #5
0
            void remove(astar_node_t* element)
            {
                if( finder.find(element) != finder.end() )
                {
                    unsigned i = finder[element];
                    while( i != 1 )
                    {
                        swap(i, parent(i));
                        i = parent(i);
                    }

                    astar_node_t* node = remove_min();
                    delete node;
                }
            }
Пример #6
0
static TAVL_nodeptr remove_node(TAVL_treeptr tree, TAVL_nodeptr p, char *deltaht)
{
    char dh;
    TAVL_nodeptr q;

    *deltaht = 0;

    if (p->bf != LEFT) {
        if (RLINK(p)) {
            p->Rptr = remove_min(p->Rptr,&q,&dh);
            if (dh) {
                p->bf += LEFT;  /* becomes 0 or LEFT */
                *deltaht = (p->bf) ? 0 : 1;
            }
        }
        else { /* leftchild(p),rightchild(p) == NULL */
            assert(p->bf == 0);
            assert(LTHREAD(p));

            *deltaht = 1;           /* p will be removed, so height changes */
            if (p->Rptr->Lptr == p) { /* p is leftchild of it's parent */
                p->Rptr->Lbit = THREAD;
                q = p->Lptr;
            }
            else {  /* p is rightchild of it's parent */
                assert(p->Lptr->Rptr == p);
                p->Lptr->Rbit = THREAD;
                q = p->Rptr;
            }
            (*tree->dealloc)(p);
            return q;
        }
    }
    else { /* p->bf == LEFT */
        p->Lptr = remove_max((p->Lptr),&q,&dh);
        if (dh) {
            p->bf += RIGHT;      /* becomes 0 or RIGHT */
            *deltaht = (p->bf) ? 0 : 1;
        }
    }

    p->dataptr = q->dataptr;
    (*tree->dealloc)(q);
    return p;
}
Пример #7
0
static TAVL_nodeptr remove_min(TAVL_nodeptr p, TAVL_nodeptr *minnode, char *deltaht)
{
    char dh = *deltaht = 0;

    if (LLINK(p)) { /* p is not minimum node */
        p->Lptr = remove_min(p->Lptr,minnode,&dh);
        if (dh) {
            p->bf += RIGHT;
            switch (p->bf) {
                case 0: *deltaht = 1;
                        break;
                case RIGHT+RIGHT:
                        p = rebalance_tavl(p,deltaht);
            }
        }
        return p;
    }
    else { /* p is minimum */
        *minnode = p;
        *deltaht = 1;
        if (RLINK(p)) {
            assert(p->Rptr->Lptr == p);
            assert(LTHREAD(p->Rptr) && RTHREAD(p->Rptr));

            p->Rptr->Lptr = p->Lptr;
            return p->Rptr;
        }
        else
            if (p->Rptr->Lptr != p) {   /* was first call to remove_min, */
                p->Lptr->Rbit = THREAD; /* from "remove", not remove_min */
                return p->Rptr;         /* p is never rightchild of head */
            }
            else {
                p->Rptr->Lbit = THREAD;
                return p->Lptr;
            }
    }
}
Пример #8
0
struct STAT* astar(int state[SIDE][SIDE])
{
    printf("in progress...\r");
    clear();
    time_t s, e;
    struct STEP* first;
    struct STEP* next;
    time(&s);
    first = create_step(NULL);
    copy_states(state, first->state);
    rate(first);
    frontier[lenghtF++] = first;
    
    while(lenghtF > 0) {
        next = remove_min(frontier, &lenghtF);
        if(next->rate <= 0) {
            break;
        }
        else if(lenghtF >= MAX_SIZE-4 || lenghtE >= MAX_SIZE) {
            for(int i = 0; i < lenghtE; i++) {
                if(compare_steps(explored[i], next) < 0) {
                    next = explored[i];
                }
            }
            break;
        }
        else {
            explore(next);
        }
    }
    time(&e);
    struct STAT* stat = (struct STAT*) malloc(sizeof(struct STAT));
    stat->solve = next;
    stat->explored = lenghtE;
    stat->seconds = (int) (e - s);
    stat->speed = MAX(lenghtE, 1) / MAX(stat->seconds, 1);
    return stat;
};
void remover (int key, tree* t){
    tree* tmp=NULL;
    if(t==NULL){
        return 0;
    }
    if(key < t->id){
        remover(key,t->left);
    }else if(key > t->id){
        remover(key,t->rigth);
    }else{
        if((t->left)&&(t->rigth)){
            tmp = find_min(t->rigth);
            t->id = tmp->id;
            return remove_min(t->rigth);
        }
        tmp=t;
        if(t->left!=NULL){
            t=t->left;
        }else{
            t=t->rigth;
        }
        free(tmp);
    }
}
Пример #10
0
int main(int argc, char *argv[]) {
    struct PQueue *pq = (struct PQueue *) malloc(sizeof(struct PQueue));
    init_PQ(pq, 10, sizeof(sizeof(struct Duo)));
    struct Duo *data1 = (struct Duo *) malloc(sizeof(struct Duo));
    struct Duo *data2 = (struct Duo *) malloc(sizeof(struct Duo));
    struct Duo *data3 = (struct Duo *) malloc(sizeof(struct Duo));
    struct Duo *data4 = (struct Duo *) malloc(sizeof(struct Duo));
    struct Duo *data5 = (struct Duo *) malloc(sizeof(struct Duo));
    struct Duo *data6 = (struct Duo *) malloc(sizeof(struct Duo));
    struct Duo *data7 = (struct Duo *) malloc(sizeof(struct Duo));

    data1->x = 5;
    data1->y = 8;

    data2->x = 2;
    data2->y = 6;
    
    data3->x = 4;
    data3->y = 1;
   
    data4->x = 3;
    data4->y = 1;
    
    data5->x = 5;
    data5->y = 9;
   
    data6->x = 5;
    data6->y = 3;
    
    data7->x = 9;
    data7->y = 8;
    
    
    insert(pq, 6, data1);
    insert(pq, 4, data2);
    insert(pq, 2, data3);
    insert(pq, 1, data4);
    insert(pq, 3, data5);
    insert(pq, 5, data6);
    insert(pq, 7, data7);
    
    printf("data1: (%d, %d)\n", data1->x, data1->y);

    struct Duo *temp = (struct Duo *) malloc(2 * sizeof(struct Duo));
    
    print_PQ(pq);
    printf("Queue Values (expected 3 1 4 1 5 9 2 6 5 3 5 8 9 8): ");
    while(!is_empty(pq)) {

        remove_min(pq, temp);
        printf("(%d, %d) ", temp->x, temp->y);
    }
    printf("\n");
    free(data1);
    free(data2);
    free(data3);
    free(data4);
    free(data5);
    free(data6);
    free(data7);
    free(temp);
    free(pq);
    return 0;
}