int main() { int data[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int size = 10; //int *data = malloc(sizeof(int)*size); struct heap *h; int new_data = 30; h = build_max_heap(data, size); dump_heap(h); free(h); h = build_min_heap(data, size/2, size); dump_heap(h); printf("extract min=%d\n", min_heap_extract(h)); dump_heap(h); printf("insert %d\n", new_data); min_heap_insert(h, new_data); dump_heap(h); free(h); //free(data); return 0; }
int main(int argc, const char *argv[]) { min_heap *f = min_heap_init(); min_heap_insert(f, 1); min_heap_insert(f, 0); min_heap_insert(f, -1); min_heap_insert(f, -3); min_heap_insert(f, -3); min_heap_insert(f, 3); min_heap_insert(f, -19); int i = 0; for (i = 0; i < f->size; i++) { printf("%d; ", f->cont[i]); } printf("--------%d\n", f->size); while (f->size != 0) { printf("%ld ", min_heap_pull(f)); } printf("%d\n", i); return 0; }
int event_queue_push(event_level_t level, struct connectSock * psock, event_queue_pt pt, // uint8_t cmd, // uint16_t opt_code, void * pdata, int siz) { int ret = 0; //int max_conn = 0; uint32_t weight = 0; struct cmd_event * pe; event_queue_t * peq; uint16_t * p_value; timestamp++; //printf("\ntimestamp = %u\n", timestamp); if (siz > MAX_EBUF_SIZ) { log_error(LOG_NOTICE, "MAX_EBUF_SIZ"); return -1; } weight = ++weightness; if (psock != NULL) { weight = psock->addr; p_value = &psock->event_cnt; } else { p_value = &pserial_des->event_cnt; } peq = pq + get_tindex(weight); //平衡相应的队列 EVENT_LOCK(peq); debug_event("\nstart push ok line = %d\n", __LINE__); do { pe = cmd_event_new(peq, siz); if (pe == NULL) { if (psock == NULL) { peq = pq + get_tindex(weight + 1); pe = cmd_event_new(peq, siz); } if (pe == NULL) { ret = -1; log_error(LOG_NOTICE, "cmd queue full queue.no = %d", peq->queue_no); break; } } pe->level = level; // pe->cmd = cmd; // pe->opt_code = opt_code; pe->weight_value = ++(*p_value); pe->timestamp = timestamp; pe->cb = pt; pe->psock = psock; memcpy(pe->buffer, pdata, siz); //printf("\npe->level = %d, pe->timestamp = %u\n",level, timestamp); if (min_heap_insert(peq->heap, pe) == NULL) { log_error(LOG_ERROR, "min_heap_inisert !!!!"); cmd_event_del(peq, pe); } } while (0); // printf("\nccc\n"); event_queue_wakeup(peq); debug_event("\nend push ok line = %d\n", __LINE__); EVENT_UNLOCK(peq); return ret; }
int main(int argc, char *argv[]) { // Receive pairs of ints input to this program, save into struct array int numSymbols = 0; while ( scanf("%d", &s[numSymbols].freq) == 1 ) { s[numSymbols].letter = numSymbols; t[numSymbols].freq = s[numSymbols].freq; // Store into hufftree, too. numSymbols++; } printf("\n"); // these will be incremented/decremented at various times, // but 'originalSize' shall not be changed. int originalSize = numSymbols; int heapSize = numSymbols; /* printf("This is how we received all the numbers:\n"); int i; for (i=0; i<numSymbols ; i++) { printf("%5d ", s[i].freq); printf("is the freq of letter: %d\n", s[i].letter); } */ build_min_heap(s, heapSize-1); /* printf("This is the heap:\n"); //int i; for (i=0; i<numSymbols ; i++) { printf("%5ld ", s[i].freq); printf("%d\n", s[i].letter); } */ // watch out: heapSize will shrink // --------- numSymbols with grow // ...but they start out equal. //printf("heapSize: %d\n", heapSize); while (heapSize>=2) { symbol thing1 = min_heap_extract(s, &heapSize); //printf("AFTER EXTRACT heapSize: %d\n", heapSize); //printf("thing1.letter: %d\n", thing1.letter); //printf("thing1.freq: %d\n", thing1.freq); symbol thing2 = min_heap_extract(s, &heapSize); //printf("AFTER EXTRACT heapSize: %d\n", heapSize); //printf("thing2.letter: %d\n", thing2.letter); //printf("thing2.freq: %d\n", thing2.freq); symbol new_thing; new_thing.letter = numSymbols; new_thing.freq = thing1.freq + thing2.freq; //printf("new_thing.letter: %d\n", new_thing.letter); //printf("new_thing.freq: %d\n", new_thing.freq); //printf("^^^^^^^^^^^^^^fidlin' with the t tree^^^^^^^^^^^^\n"); t[thing1.letter].parent = numSymbols; //printf("t[%d].parent: %d\n", thing1.letter, t[thing1.letter].parent); t[thing1.letter].zero_or_one = 0; //printf("t[%d].zero_or_one: %d\n", thing1.letter, t[thing1.letter].zero_or_one); t[thing2.letter].parent = numSymbols; //printf("t[%d].parent: %d\n", thing2.letter, t[thing2.letter].parent); t[thing2.letter].zero_or_one = 1; //printf("t[%d].zero_or_one: %d\n", thing2.letter, t[thing2.letter].zero_or_one); t[numSymbols].parent = -1; //printf("t[%d].parent: %d\n", numSymbols, t[numSymbols].parent); numSymbols++; heapSize++; // doesn't get modified by insert function, do it manually here min_heap_insert(s, heapSize-1, new_thing); //printf("AFTER INSERT heapSize: %d\n", heapSize); /* printf("This is the heap:\n"); //int i; for (i=0; i<heapSize ; i++) { printf("%5ld ", s[i].freq); printf("%d\n", s[i].letter); } */ } /* If the given frequencies of letters were actually used, print the number of bits needed to huffman-encode the whole thing. */ int i; int tally = 0; for (i=0; i<originalSize; i++) { t[i].numBits = getBits(i); tally = tally + t[i].numBits * t[i].freq; } printf("%d\n", tally); // print the huffman codes of each given letter for (i=0; i<originalSize; i++) { printf("%c ", i+'A'); print(i); printf("\n"); } /* symbol x = min_heap_extract(s, &numSymbols); printf("the min node has value: %d\n", x.freq); printf("numSymbols is now %d\n", numSymbols); printf("This is the heap after extracting:\n"); //int i; for (i=0; i<numSymbols ; i++) { printf("%5ld ", s[i].freq); printf("%d\n", s[i].letter); } x = min_heap_extract(s, &numSymbols); printf("the min node has value: %d\n", x.freq); printf("numSymbols is now %d\n", numSymbols); printf("This is the heap after extracting again:\n"); //int i; for (i=0; i<numSymbols ; i++) { printf("%5ld ", s[i].freq); printf("%d\n", s[i].letter); } //TESTING INSERT symbol woof; woof.freq = 17; woof.letter = 17; numSymbols++; min_heap_insert(s, numSymbols, woof); printf("===================does INSERT work?====================\n"); x = min_heap_minimum(s); printf("woof node has value: %d\n", woof.freq); printf("the min node has value: %d\n", x.freq); printf("numSymbols is now %d\n", numSymbols); printf("This is the heap after inserting:\n"); //int i; for (i=0; i<numSymbols ; i++) { printf("%5ld ", s[i].freq); printf("%d\n", s[i].letter); } */ /* // heapSort(s, numSymbols-1); min_heapSort(s, numSymbols-1); printf("This is how how it looks after sorting:\n"); for (i=0; i<numSymbols ; i++) { printf("%5d ", s[i].freq); printf("for letter %d\n", s[i].letter); } */ printf("\n"); }
int main() { freopen("input.txt","r",stdin); char str[500]; scanf("%s",str); char visit[26]; node* ptr[26]; int i,l = strlen(str),j=1,k; for(i=0; i<26; i++) visit[i] = 0; /*for(i=0; i<l; i++) freq[str[i]-'A']++;*/ heap min_queue; min_queue.length = 0; node *n1, *n2, *n; for(i=0; i<l; i++) { k = str[i]-'A'; if(visit[k] == 0) { visit[k] = 1; ptr[k] = min_queue.arr[j++] = get_node(str[i], 1);//made changes here inplace of i use j for min_queue arr min_queue.length++; } else { ptr[k]->freq++; } } /*for(i=0; i<min_queue.length; i++) print_node(min_queue.arr[i+1]); */ build_min_heap(&min_queue); /*for(i=0; i<min_queue.length; i++) print_node(min_queue.arr[i+1]);*/ /*n1 = heap_extract_min(&min_queue); print_node(n1); n2 = heap_extract_min(&min_queue); print_node(n2); printf("ABCD\n"); for(i=0; i<min_queue.heap_size; i++) print_node(min_queue.arr[i+1]);*/ while(min_queue.heap_size >= 2) { n1 = heap_extract_min(&min_queue); print_node(n1); n2 = heap_extract_min(&min_queue); print_node(n2); n = get_node('0',0); n->freq = n1->freq + n2->freq; n->lchild = n1; n->rchild = n2; n1->isleft = 1; n2->isleft = 0; n1->parent = n2->parent = n; min_heap_insert(&min_queue,n); } printf("%d\n", count); n = heap_extract_min(&min_queue); postorder(n); printf("\n"); inorder(n); printf("\n"); for(i=0; i<l; i++) { huff_code(ptr[str[i]-'A']); } /*ENDDDDDDDDDDDDDDDDDD /*print_node(n); printf("\n"); print_node(n->lchild); printf("\n"); print_node(n->rchild); printf("\n"); print_node(n->rchild->lchild); printf("\n"); print_node(n->rchild->rchild); printf("\n"); print_node(n->rchild->rchild->lchild); printf("\n"); print_node(n->rchild->rchild->rchild);*/ //Used for checking if Prority Queue is working fine /*while(min_queue.heap_size >= 1) { node* tmp = heap_extract_min(&min_queue); printf("%c-%d\n",tmp->ch, tmp->freq); }*/ return 0; }