Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define bytes 256
  5.  
  6. //Huffman tree node
  7. struct treeNode{
  8.     char data;  //Character
  9.     int frequency;  // Frequency of the character
  10.     struct treeNode *left, *right; // Left and right child of this node
  11. };
  12.  
  13. //Huffman tree nodes !
  14. struct MinHeap
  15. {
  16.     int currentSize;    //Current size of min heap
  17.     int totalSize;   //Total Size of min heap
  18.     struct treeNode **array;  // Array of minheap node pointers
  19. };
  20.  
  21. //Allocate a new min heap node with given character and frequency of this character
  22. struct treeNode* newNode(char data,int frequency)
  23. {
  24.     struct treeNode* temp = (struct treeNode*) malloc(sizeof(struct treeNode));
  25.     temp->left = temp->right = NULL;
  26.     temp->data = data;
  27.     temp->frequency = frequency;
  28.     return temp;
  29. }
  30.  
  31. //Get each character and it's frequency
  32. int get_frequency(char* str,int len,struct treeNode *chars)
  33. {
  34.     int i,no_chars;
  35.     int freq[26];
  36.     //Initialize frequency of each character to 0
  37.     for(i=0; i<26; i++)
  38.     {
  39.         freq[i] = 0;
  40.     }
  41.      //Find total number of occurrences of each character
  42.     for(i=0; i<len; i++)
  43.     {
  44.         //If the current character is lowercase
  45.         if(str[i]>='a' && str[i]<='z')
  46.         {
  47.             freq[str[i] - 97]++;
  48.         }
  49.         //If the current character is uppercase
  50.         else if(str[i]>='A' && str[i]<='Z')
  51.         {
  52.             freq[str[i] - 65]++;
  53.         }
  54.     }
  55.  
  56.     for(i=0; i<26; i++)
  57.     {
  58.         /* If current character exists in given string */
  59.         if(freq[i] != 0)
  60.         {
  61.             chars[no_chars].frequency=freq[i];
  62.             chars[no_chars].data= (i+97);
  63.             no_chars++;
  64.         }
  65.     }
  66. return no_chars;
  67. }
  68.  
  69. //Create MinHeap of given Size(N)
  70. struct MinHeap* createMinHeap(int totalSize)
  71. {
  72.     struct MinHeap* minHeap = (struct MinHeap*) malloc(sizeof(struct MinHeap));
  73.     minHeap->currentSize = 0;  // current size is 0
  74.     minHeap->totalSize = totalSize;
  75.     minHeap->array =(struct treeNode**)malloc(minHeap->totalSize * sizeof(struct treeNode*));
  76.     return minHeap;
  77. }
  78.  
  79. //Swap two nodes with each others
  80. void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
  81. {
  82.     struct MinHeapNode* t = *a;
  83.     *a = *b;
  84.     *b = t;
  85. }
  86.  
  87. //MIN_HEAPIFY
  88. void MIN_HEAPIFY(struct MinHeap* minHeap, int index)
  89. {
  90.     printf("Minheapify reached\n");
  91.     int smallest = index; //Smallest index
  92.     int left = 2 * index + 1; //Left Child of smallest
  93.     int right = 2 * index + 2; //Right Child of smallest
  94.  
  95.     struct treeNode* temp = (struct treeNode*) malloc(sizeof(struct treeNode));
  96.  
  97.     //Frequency of left < frequency of samllest
  98.     if (left < minHeap->currentSize && minHeap->array[left]->frequency < minHeap->array[smallest]->frequency)
  99.       {smallest = left;}
  100.  
  101.     //Frequency of right < frequency of samllest
  102.     if (right < minHeap->currentSize && minHeap->array[right]->frequency < minHeap->array[smallest]->frequency)
  103.       {smallest = right;}
  104.  
  105.     //If index smallest != index then swap the samllest with it's right place in the heap
  106.     if (smallest != index)
  107.     {
  108.         swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[index]);
  109.         //After swap call min heapify again to check every one is in it's right place after swapping
  110.         MIN_HEAPIFY(minHeap, smallest);
  111.     }
  112. }
  113.  
  114. //Check if size is one
  115. int isSizeOne(struct MinHeap* minHeap)
  116. {
  117.     return (minHeap->currentSize == 1);
  118. }
  119.  
  120. //Check if this node is leaf
  121. int isLeaf(struct treeNode* root)
  122. {
  123.     return !(root->left) && !(root->right) ;
  124. }
  125.  
  126. //Extract minimum node
  127. struct treeNode* extractMin(struct MinHeap* minHeap)
  128. {
  129.     //Assuming that the smallest element of the heap is always in the first place of the array of pointers of the heap
  130.     struct treeNode* temp = minHeap->array[0];
  131.     //Putting the last element in the array of heaps in the first place of the heap after getting the smallest element
  132.     minHeap->array[0] = minHeap->array[minHeap->currentSize - 1];
  133.     --minHeap->currentSize;
  134.     MIN_HEAPIFY(minHeap, 0);
  135.     printf("\nsize= %d\n",minHeap->currentSize);
  136.     return temp;
  137. }
  138.  
  139. //Insert minimum into heap after
  140. void insertMinHeap(struct MinHeap* minHeap, struct treeNode* treeNode)
  141. {
  142.     ++minHeap->currentSize;
  143.     int i = minHeap->currentSize - 1;
  144.     while (i && treeNode->frequency < minHeap->array[(i - 1)/2]->frequency)
  145.     {
  146.         minHeap->array[i] = minHeap->array[(i - 1)/2];
  147.         i = (i - 1)/2;
  148.     }
  149.     minHeap->array[i] = treeNode;
  150. }
  151.  
  152. //Build the heap b2a :O :O
  153. void buildMinHeap(struct MinHeap* minHeap)
  154. {
  155.     int n = minHeap->currentSizesize - 1;
  156.     int i;
  157.     /* for(i=0;i<=n;i++)
  158.         printf("char: %s & frequency: %d\n",freq[i]->x,freq[i]->frequency);*/
  159.     for (i = (n - 1) / 2; i >= 0; --i)
  160.         MIN_HEAPIFY(minHeap,i);
  161. }
  162.  
  163.  
  164. int main()
  165.  
  166. {
  167.     FILE *fp;
  168.     int c;
  169.  
  170.     fp = fopen("t.txt", "r");
  171.     char *code;
  172.     size_t n = 0;
  173.  
  174.     if (fp == NULL) {
  175.     printf("ERROR!");
  176.     return NULL;
  177.     } //could not open file
  178.     fseek(fp, 0, SEEK_END);
  179.     long f_size = ftell(fp);
  180.     fseek(fp, 0, SEEK_SET);
  181.     code = malloc(f_size);
  182.  
  183.  
  184.     while((c = fgetc(fp)) != EOF) {
  185.             printf("%c\n",(char)c); //Feh moshkela hena lw shelna el printf by7sal moshkela fl malloc try to check it.
  186.             code[n] = (char)c;
  187.             n++;
  188.             //putchar(c);
  189.     }
  190.     fclose(fp);
  191.  
  192.     struct treeNode* temp = (struct treeNode*) malloc(sizeof(struct treeNode));
  193.     //Construct structuraya
  194.     int no_chars=0;
  195.     no_chars = get_frequency(code,n,temp);
  196.     //BUILD_MIN_HEAP(no_chars);
  197.     return 0;
  198. }
  199.  
  200.  
  201. /* int i=0;
  202.     while (no_chars>0)
  203.     {
  204.         printf("char: %c & freq: %d\n",temp[i].x,temp[i].frequency);
  205.         no_chars--;
  206.         i++;
  207.     }
  208.     */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement