Exemplo n.º 1
0
// The main function that builds Huffman tree
struct node* buildHuffmanTree(char data[], int freq[], int size)
{
    struct node *left, *right, *top;
 
    // Step 1: Create a min heap of Limit equal to size.  Initially, there are
    // modes equal to size.
    struct Heap* Heap = createAndBuildHeap(data, freq, size);
 
    // Iterate while size of heap doesn't become 1
    while (!isSizeOne(Heap))
    {
        // Step 2: Extract the two minimum freq items from min heap
        left = extractMin(Heap);
        right = extractMin(Heap);
 
        // Step 3:  Create a new internal node with frequency equal to the
        // sum of the two nodes frequencies. Make the two extracted node as
        // left and right children of this new node. Add this node to the min heap
        // '$' is a special value for internal nodes, not used
        top = newNode('$', left->freq + right->freq);
        top->left = left;
        top->right = right;
        insertHeap(Heap, top);
    }
 
    // Step 4: The remaining node is the root node and the tree is complete.
    return extractMin(Heap);
}
Exemplo n.º 2
0
void heapsort() {

    int i, 

        elem;

    freopen(FIN, "r", stdin);

    scanf("%d", &n); 

    for(i = 1; i <= n; i++) {
 
        scanf("%d", &elem);

        insertHeap( elem ); 
    }

    fclose( stdin ); 

    freopen(FOUT, "w", stdout);

    for(i = 1; i <= n; i++) {

        printf("%d ", removeHeap());
    }    

    fclose( stdout ); 

}
int main(int argc, char *argv[]) {
    // Check arguments
    if (argc < 2) {
        printf("Wrong arguments\n");
        return EXIT_FAILURE;
    }
    int maxnum = atoi(argv[1]);
    char *filename = argv[2];
    // Create new heap
    heap *theHeap = newHeap(maxnum);

    // Read lines of file
    FILE *fp = fopen(filename, "rt");
    while(!feof(fp)) {
        char buffer[256];
        fgets(buffer, 256, fp);
        if (feof(fp)) break;
        int value = atoi(buffer);
	insertHeap(theHeap, value);
        if (isFullHeap(theHeap)) {
            removeHeap(theHeap);
        }
    }

    // Show contents
    while(!isEmptyHeap(theHeap)) {
        int value = removeHeap(theHeap);
        printf("%d\n", value);
    }
    
    // Delete heap
    deleteHeap(theHeap);

    return EXIT_SUCCESS;
}
Exemplo n.º 4
0
void main() {

	init_List();


	FILE* fp = fopen("test.txt", "rb");
	char buff[1000];
	//fread(buff, 1, sizeof(buff), fp);



	int i = 0;
	while (!feof(fp) && i< MOVIE_NUM)
	{


		if (buff != NULL)
		{
			fgets(buff, 1000, fp);
			parsing(buff, i++);

		}

	}

	fclose(fp);

	for (i = 0; i < MOVIE_NUM; i++) {
		//printf("%s         %d   %d   %s   %s   %s   %s\n", chart[i]->name, chart[i]->audience, chart[i]->screen_num, chart[i]->nation, chart[i]->genre[0], chart[i]->director, chart[i]->actors[0]);
		//for(int j =0; j<3; j++)if(chart[i]->actors[j]!=NULL)printf("%s\n", chart[i]->actors[j]);

	}

	i = 0;

	makeVisit();

	genre_heap* heap = createGenreHeap(20);

	for (i = 0; i < 20; i++) {

		insertHeap(heap, *genres[i]);

	}
	for (i = 0; i < 20; i++){
		printf("%s - %d\n", deletHeap(heap).name, deletHeap(heap).genre_count); }
	/*while (genres[i]->name != NULL) {

	printf("%s -%d\n", genres[i]->name, genres[i]->genre_count);
	i++;
	}

	while (actors[i]->name != NULL) {
	printf("%s -%d\n", actors[i]->name, actors[i]->avg_aud);
	i++;
	}*/

}
Exemplo n.º 5
0
void main(void){
    int i, j;
    Item input[] = "ALGORITHM";
    Item heap[9];
    for(i=0; i<9; i++){
        printf("Insert %c : ", input[i]);
        insertHeap(heap, i, input[i]);
    for(j = 1; j <= i+1; j++){ printf("%c", heap[j]); }
        printf("\n");
    }
}
Exemplo n.º 6
0
// -------------------------------------------------------------------------
int main (int argc, const char * argv[]) 
{
  int* key;
  char command;
  bool quit=false;
  printf( "Welcome to heap test program\n" );
  Heap h = newHeap( comp );
  do {
    printf( "Command: ('h' for help): " );
    scanf( " %c", &command );
    switch( toupper(command) ) {
    case 'H':			/* help */
      printf( "Commands:\n"
	      "  i num - insert number into heap\n"
	      "  d     - delete minimum element from heap\n"
	      "  p     - print heap\n"
	      "  q     - quit\n" );
      break;
    case 'I':			/* insert */
      key = safe_malloc( sizeof *key );
      scanf( "%d", key );
      insertHeap( h, key );
      break;
    case 'D':			/* delete_min */
      if (isEmptyHeap( h )) {
	printf( "Heap is empty\n" );
      }
      else {
        key = deleteMinHeap( h );
        printf( "%d\n", *key );
        free( key );
      }
      break; 
    case 'P':			/* print */
      mapHeap( print_element, h, NULL );
      printf( "\n" );
      break;
    case 'Q':
      quit=true;
    }
  } while( !quit );
  
  freeHeap( h );
  printf( "Goodbye!\n" );
}
Exemplo n.º 7
0
int main()
{
    int heap[max],n=0,ele=0,choice;
    for(;;)
    {
        printf("Enter 1 to insert 2 to display, 3 to exit");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1: printf("Enter the element to insert\n");
                    scanf("%d",&ele);
                    insertHeap(ele,heap,&n);
                    break;
            case 2: display(heap,n);break;
            case 3: exit(0);
        }
    }
return 0;
}
Exemplo n.º 8
0
void main() {
	int heap[MAX], curr = -1, item;
	char choice;
	printf("\n Enter");
	printf("\n i to Insert");
	printf("\n d to Display");
	do {
		printf("\n >> ");
		scanf("%c", &choice);
		switch(choice) {
			case 'i':
				printf(" Enter element to insert: ");
				scanf(" %d", &item);
				insertHeap(heap, &curr, item);
			case 'd':
				printf("\n");
				displayHeap(heap, curr);
				break;
			default:
				return;
		}
		scanf("%c", &choice);
	} while(1);
}