By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

getting segmentation fault (core dumped)

P: 48
Hii,
I was trying to implement hoffman codes generating program but i was getting runtime error due to function "printcodes" my problem was not to know reason for error but to write printcodes function to print the leaf nodes of the tree created by me with *root as the root node.I don't know if there is any error in huffman tree created by me.Someone please correct my code or atleast provide an easy code for huffman coding as the code i got online is heavy i can't even understand it.Thank you.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. struct node{
  5.     int fr;
  6.     int value;
  7.     struct node *left;
  8.     struct node *right;
  9. };
  10.  
  11.  
  12. void printcodes(struct node *p,int top,int arr[]){
  13.  
  14.     if(p->left!=NULL){
  15.         arr[top]=0;
  16.         printcodes(p->left,top+1,arr);
  17.     }
  18.     if(p->right!=NULL){
  19.         arr[top]=1;
  20.         printcodes(p->right,top+1,arr);
  21.     }
  22.  
  23.     if(p->left==NULL && p->right==NULL){
  24.         for(int i=0;i<top;i++)
  25.             printf("%d",arr[i]);
  26.         printf("\n");
  27.  
  28.     }
  29.  
  30.     top--;
  31. }
  32.  
  33.  
  34. void heapify(struct node *a[],int n){
  35.     int i;
  36.     for(i=n-1;i>0;i--){
  37.         if((a[((i+1)/2)-1]->fr)>(a[i]->fr))
  38.         {
  39.             struct node *t;
  40.             t = a[((i+1)/2)-1];
  41.             a[((i+1)/2)-1]=a[i];
  42.             a[i]=t;
  43.         }
  44.     }
  45. }
  46.  
  47. void heapsort(struct node *a[],int n){
  48.     int i;
  49.     for(i=0;i<2;i++){
  50.         heapify(a,n-i);
  51.         struct node *t;
  52.         t = a[0];
  53.         a[0]=a[n-(i+1)];
  54.         a[n-(i+1)]=t;
  55.     }
  56. }
  57.  
  58.  
  59. int main(){
  60.     int i;
  61.     FILE *fp;
  62.     char ch;
  63.     struct node *a[26],*root;
  64.     int f[26][2]={0};
  65.     for(i=0;i<26;i++){
  66.         f[i][0]=i;
  67.     }
  68.     fp = fopen("input","r");
  69.     while((ch=fgetc(fp))!='\n'){
  70.         f[ch-'a'][1]++;
  71.     }
  72.     for(i=0;i<26;i++){
  73.         printf("%d %d\n",f[i][0],f[i][1]);
  74.     }
  75.  
  76.     int count=0;
  77.     for(i=0;i<26;i++){
  78.         if(f[i][1]!=0){
  79.             struct node *p;
  80.             p = (struct node *)malloc(sizeof(struct node));
  81.             p->fr = f[i][1];
  82.             p->value = f[i][0];
  83.             p->left = NULL;
  84.             p->right = NULL;
  85.             a[count]=p;
  86.             count++;
  87.         }
  88.     }
  89.     int send=count;
  90.     for(i=0;i<count;i++){
  91.         heapsort(a,send);
  92.         struct node *p;
  93.         p = (struct node *)malloc(sizeof(struct node));
  94.         p->fr = a[send-1]->fr + a[send-2]->fr;
  95.         p->value = -1;
  96.         p->left = a[send-1];
  97.         p->right = a[send-2];
  98.         a[send-2]=p;
  99.         send--;
  100.         if(i==count-1)
  101.             root = p;
  102.     }
  103.     int arr[26];
  104.     printcodes(root,0,arr);
  105. }
  106.  
  107.  
Feb 7 '16 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
This code:
Expand|Select|Wrap|Line Numbers
  1.         root = p;
  2.     }
  3.     int arr[26];    <<<<<<<<<<<<<<<<<<<<<
  4.     printcodes(root,0,arr);
  5. }
  6.  
  7.  
The array arr is not the array he rest of the code uses.
Feb 7 '16 #2

P: 48
why i am using the array arr only what is the problem
Feb 8 '16 #3

Post your reply

Sign in to post your reply or Sign up for a free account.