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, 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"); }