Example #1
0
// The main function that builds Huffman tree
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size)
{
    struct MinHeapNode *left, *right, *top;
 
    // Step 1: Create a min heap of capacity equal to size.  Initially, there are
    // modes equal to size.
    struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);
 
    // Iterate while size of heap doesn't become 1
    while (!isSizeOne(minHeap))
    {
        // Step 2: Extract the two minimum freq items from min heap
        left = extractMin(minHeap);
        right = extractMin(minHeap);
 
        // 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;
        insertMinHeap(minHeap, top);
    }
 
    // Step 4: The remaining node is the root node and the tree is complete.
    return extractMin(minHeap);
}
Example #2
0
void ptest(void)
{
    int i,n;
    struct Data d[MaxHeapSize-1];
    struct Data tempD;
    int A[]={5,2,8,6,1,9,21,42,0};
    struct Heap * mH=initMinHeap();
    for (i=0; i<MaxHeapSize-1; i++) {
        d[i].data=A[i];
    }
    for (i=0; i<MaxHeapSize-1; i++) {
        insertMinHeap(mH,d[i]);
    }
    printf("count: %d \n", mH->count);
    for (i=0; i<MaxHeapSize-1; i++){
        printf("popped data: %d ", (removeMinHeap(mH)).data);
        printf("\n");
        for(n=0; n<MaxHeapSize; n++){
            printf("%d \n", mH->heapArray[n].data.data);
        }
        printf("------------------------------");
        printf("\n");
    }
    free(mH);
}
Example #3
0
void p1(void)
{
    int i,n;
    struct Data d[MaxHeapSize-1];
    struct Data tempD;
    int A[]={5,2,8,6,1,9,21,42,0};
    struct Queue * pQ;
    struct Heap * mH=initMinHeap();
    initQueue(&pQ);

    for (i=0; i<MaxHeapSize-1; i++) {
        d[i].data=A[i];
    }
    for (i=0; i<MaxHeapSize-1; i++) {
        insertMinHeap(mH,d[i]);
    }
    for (i=0; i<MaxHeapSize-1; i++) {
        tempD=removeMinHeap(mH);
        enqueue(pQ, tempD);
    }
    printf("Elements in the priority queue are:");
    while (isEmpty(pQ)!=1) {
        dequeue(pQ, &tempD);
        printf(" %d ", tempD.data);
    }
    printf("\n");
    freeQueue(pQ);
    freeMinHeap(mH);
}
/** Inserts a num into the data structure. left of median=maxheap right of median=minheap*/
void addNum(struct MedianFinder* mf, int num) {
  if(!mf)
    return;
  int i = compare(mf->maxheap->count, mf->minheap->count);
  printf("\n median=%lf, num=%d",mf->median,num);
  switch(i)
  {
    case -1:
      if(num<mf->median)
        insertMaxHeap(mf->maxheap, num);
      else{
        int e = mf->minheap->arr[0];
        insertMaxHeap(mf->maxheap,e);
        mf->minheap->arr[0]=num;
        minHeapify(mf->minheap, 0);
      }
      mf->median=(double)((mf->maxheap->arr[0]+mf->minheap->arr[0])/2);
      break;
    case 0:
      if(num<mf->median)
      {
        insertMaxHeap(mf->maxheap, num);
        mf->median= mf->maxheap->arr[0];
      }
      else{
        insertMinHeap(mf->minheap, num);
        mf->median= mf->minheap->arr[0];
      }
      break;
    case 1:
      if(num<mf->median){
        int e = mf->maxheap->arr[0];
        insertMinHeap(mf->minheap,e);
        mf->maxheap->arr[0]=num;
        maxHeapify(mf->maxheap, 0);
      }
      else{
        insertMinHeap(mf->minheap, num);
      }
      mf->median=(mf->maxheap->arr[0]+mf->minheap->arr[0])/2;
      break;
    default:
      break;
  }
}
void insertMedianOnline(MedianOnline *S, long int key)
{
	long int temp, max, min;

	insertMaxHeap(&(S->Max), key);
	if((S->Max).A.heapSize > (S->Min).heapSize)
	{
		temp = extractMaxHeap(&(S->Max));
		insertMinHeap(&(S->Min), temp);
	}
	if((S->Max).A.heapSize == (S->Min).heapSize)
	{
		if(peekMaxHeap(&(S->Max)) > peekMinHeap(&(S->Min)))
		{
			max = extractMaxHeap(&(S->Max));
			min = extractMinHeap(&(S->Min));
			insertMinHeap(&(S->Min), max);
			insertMaxHeap(&(S->Max), min);
		}
	}
	(S->size)++;
}
MinHeapNode* buildHuffmanTree (char data[], int freq[], int size)
{
	MinHeapNode* left; MinHeapNode* right; MinHeapNode* top;
	MinHeap* minHeap = createAndBuildMinHeap(data,freq,size);
	while (!isSizeOne(minHeap))
	{
		left = extractMin(minHeap);
		right = extractMin(minHeap);
		top = newNode('$', left->freq + right->freq);
		top->left = left;
		top->right = right;
		insertMinHeap(minHeap,top);
	}
	return extractMin(minHeap);
}
Example #7
0
void mergeKSortedArrays(int arr1[][n], int k)
{
    maxheap *h = createHeap(13);
    int *output = (int *)malloc(sizeof(int)*n*k);
    int i=0, j=0;
    for(i=0;i<n;i++)
    {
      for(j=0;j<k;j++)
      {
	  insertMinHeap(h,arr1[j][i]); 
      }
    }
    for(i=0;i<n*k;i++)
    {
      output[i]=extractMin(h);
      printf("%d ,", output[i]);
    }
}
int main(){

	int v;
	int i;
	int element;
	int priority;

	scanf ( "%d", &v);
	struct MinHeap * minHeap = createMinHeap(v);

	for ( i = 0; i < v; i++ ) {
		scanf ( "%d", &element);
		scanf ( "%d", &priority);
		insertMinHeap( minHeap, element, priority);
	}
	printHeap ( minHeap );
	printSortedElements ( minHeap );
	
return 0;
}
Example #9
0
// The main function that builds Huffman tree
struct MinHeapNode* buildHuffmanTree(uint16 freq[])
{
    struct MinHeapNode *left, *right, *top, *ret_node;

    // Step 1: Create a min heap of capacity equal to size.  Initially, there are
    // modes equal to size.
    struct MinHeap* minHeap = createAndBuildMinHeap(freq, HUFFMAN_STEPS);
    if (minHeap == NULL) {
        hError |= (1 << 5);
        return NULL;
    }

    // Iterate while size of heap doesn't become 1
    while (!isSizeOne(minHeap))
    {
        // Step 2: Extract the two minimum freq items from min heap
        left = extractMin(minHeap);
        right = extractMin(minHeap);

        // 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);
        if (top == NULL) {
            hError |= (1 << 6);
            return NULL;
        }
        top->left = left;
        top->right = right;
        insertMinHeap(minHeap, top);
    }

    // Step 4: The remaining node is the root node and the tree is complete.
    ret_node = extractMin(minHeap);
    osal_mem_free(minHeap);

    return ret_node;
}
void insertMaxHeap(MaxHeap *M, long int key)
{
	insertMinHeap(&(M->A), -key);
}