expression and implementation of Huffman tree coding--writing data structure by itself

Source: Internet
Author: User

Header file Huffman.h

#ifndef _huffman_h_#define _huffman_h_#define max_weight 10000typedef struct _htnode{    int WEIGHT;    int parent,lchild,rchild;    char data;} Htnode,*phtnode;typedef char** huffmancode;void select_min_weight (htnode* btree,int mn,int* s1,int* S2);//void Read_ Huffman_code (Huffmancode code,htnode** tree,int n); void Creat_huffman_tree (Huffmancode code,htnode** tree,char* pdata , int* pweight,int N); #endif


Related data Structure HUFFMAN.C

/*************************** Time: 2014.12.17xiao_ping_ping compilation environment: dev-c++ 4.9.9.2 Content: Huffman tree encoding Representation and implementation functions: Learning to write data structures *********** /#include <string.h> #include <stdlib.h> #include "huffman.h" void Select_min_weight (         htnode* btree,int mn,int* s1,int* s2) {int mweight1,mweight2,i;     mweight1 = Max_weight;           for (i = 0;i < mn;i++) {if ((((btree+i)->weight < mweight1) && (0 = = (btree+i)->parent))             {*s1 = i;    mweight1 = (btree+i)->weight;     *S1 = (btree+i)->weight;                               Nmin = i;    }} mweight2 = Max_weight; for (i = 0;i < mn;i++) {if ((((btree+i)->weight < mweight2) && (0 = = (btree+i)->parent) &&am             p; (*s1! = i)) {*s2 = i;                             Mweight2 = (btree+i)->weight; }}}/*void Read_huffman_code (Huffmancode code,htnode** tree,int N) {int i;     Char *chs;     int start;          int c,f;     Code = (huffmancode) malloc (n * sizeof (char *));     CHS = (char *) malloc (n * sizeof (char *));          Chs[n-1] = ' + ';         for (i = 0;i < n;i++) {start = n-1;              for (c = i,f = *tree[i].parent;f! = 0;c = F,f = *tree[f].parent) {if (*tree[f].lchild = = c)             {Chs[--start] = ' 0 ';                 } else {Chs[--start] = ' 1 ';         }} Code[i] = (char *) malloc ((N-start) *sizeof (char));                  strcpy (Code[i],&chs[start]);     printf ("%c:%s", *tree[i].data,code[i]); } free (CD);}     */void Creat_huffman_tree (huffmancode code,htnode** tree,char* pdata,int* pweight,int n) {int i,m;     int *order1,*order2;     Phtnode p;     Order1 = (int *) malloc (sizeof (int));          Order2 = (int *) malloc (sizeof (int));     m = n*2-1;     *tree = (Htnode *) malloc (M * sizeof (Htnode)); p = *Tree         for (i = 0;i < n;i++,p++) {p->weight = * (pweight+i);         p->parent = 0;         P->lchild = 0;         P->rchild = 0;         P->data = * (pdata+i);          } for (i = N;i < m;i++,p++) {p->parent = 0;         } for (i = N;i < m;i++) {select_min_weight (*tree,i,order1,order2);         (*tree + i)->weight = (*tree + *order1)->weight + (*tree + *order2)->weight;         (*tree + i)->lchild = *order1;          (*tree + i)->rchild = *order2;         (*tree + *order1)->parent = i;                  (*tree + *order2)->parent = i;  printf ("Order1.weight and Order2.weight in round%d:%d,%d\n", I, (*tree + *order1)->weight, (*tree +                   *ORDER2)->weight);     /* For testing */printf ("\ n");     }//int i;     Char *chs;     int start;          int c,f;     Code = (huffmancode) malloc (n * sizeof (char *)); CHS = (char *) malloc (n * sizeof (char *));          Chs[n-1] = ' + ';         for (i = 0;i < n;i++) {start = n-1; for (c = i,f = (*tree + i)->parent;f! = 0;c = F,f = (*tree + f)->parent) {if ((*tree + f)->lch             ILD = = c) {Chs[--start] = ' 0 ';                 } else {Chs[--start] = ' 1 ';         }} Code[i] = (char *) malloc ((N-start) *sizeof (char));                  strcpy (Code[i],&chs[start]);     printf ("%c:%s\n", (*tree + i + 1)->data,code[i]); } free (CHS);}


The test file test.c as follows

#include <string.h> #include <stdlib.h> #include "huffman.h" int main () {    int n,i;    char NCR = 0;    int* weights;    char* datas;    Phtnode Phash;        Huffmancode Pcode;    printf ("n =");     scanf ("%d", &n);    Datas = (char *) malloc ((N + 2) * sizeof (char));    weights = (int *) malloc (N * sizeof (int));        printf ("Please enter encoded characters:");    for (i = 0;i < (n+2); i++)    {              scanf ("%c", datas+i);  ' #结束 '        //scanf ("%c", &NCR);         while (' \ n ' = = NCR);        NCR = 0;         }    printf ("Please enter the corresponding weight: \ n");    for (i = 0;i < n;i++)    {              scanf ("%d", weights+i);          }    Creat_huffman_tree (pcode,&phash,datas,weights,n);        Getch ();    return 0;       }


The results of the operation are as follows:

expression and implementation of Huffman tree coding--writing data structure by itself

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.