簡體   English   中英

使用樹在C中進行霍夫曼解碼

[英]Huffman decoding in c using tree

我必須解壓縮用霍夫曼樹編碼的字符串,但是代碼具有可變長度,並且並非所有輸入都在前綴中,在這種情況下,我應該打印“無效”並完成執行。 輸入內容包括:不同字符數; 字符及其代碼; 編碼消息的長度; 編碼的消息。

如果可以的話,我會問一個更具體的問題,但是我真的不知道出什么問題了,因為我覺得一切都錯了。

樣本輸入為:6 e 11 m 101 x 100 l 011 p 010 o 00 18 111001110101001100

它的輸出將是:“示例”

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct node_t
{
    struct node_t *left, *right;
    char* codigo;
    unsigned char c;
} *node;

void decode(const char *s, node t)
{
    node q = t;
    while (*s)
    {
        if (*s++ == '0') q = q->left;
        else q = q->right;

        if (q->c) putchar(q->c), q = t;
    }
    putchar('\n');
}

struct node_t *insere(struct node_t *root, char x, char* h, int a)
{
    if(!root)
    {
        root=(struct node_t*)malloc(sizeof(struct node_t));
        root->c = x;
        root->codigo=h;
        root->left = NULL;
        root->right = NULL;
        return(root);
    }
    else
    {
        if(h[a]=='0')
        {
            if(root->left!=NULL)
            {
                printf("invalid\n");
                exit(0);
             }
            root->left = insere(root->left,x, h, a+1);
        }
        else
        {
            if(h[a]=='1')
            {
                if(root->left!=NULL)
                {
                    printf("invalid\n");
                    exit(0);
                }
                root->right = insere(root->right,x, h, a+1);
            }
        }
    }
    return(root);
}

void inorder(struct node_t *root)

{
    if(root != NULL)
    {
        inorder(root->left);
        free(root);
        inorder(root->right);
    }
    return;
}

int main(void)
{
    struct node_t *root;
    root = NULL;
    int i, N, M, k;
    scanf("%d", &N);
    char item, num[2*N];
    for (i = 0; i < N; i++)
    {
        scanf("%c %s\n", &item, num);
        root= insere(root, item, num, 0);
    }
    scanf("%d\n", &M);
    char buf[M];
    for (k=0; k<M; k++)
        scanf ("%c", &buf[k]);
    decode (buf, root);
    inorder(root);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

typedef struct node_t {
    struct node_t *left, *right;
    char codigo;
    char ch;
} *node;

char decode_aux(const char **s, node np){
    if(!np){
        fprintf(stderr, "invalid\n");
        exit(0);
    }
    if(np->ch)
        return np->ch;
    if(**s=='\0')
        return '\0';

    if(*(*s)++ =='0')
        return decode_aux(s, np->left);
    else //if(**s=='1')
        return decode_aux(s, np->right);
}

void decode(char *out, const char *s, node root){
    const char *p = s;
    while(*out++ = decode_aux(&p, root))
        ;
}

void insere(node *np, char ch, char *code){
    if(!*np){
        *np = malloc(sizeof(**np));
        (*np)->left = (*np)->right = NULL;
        (*np)->codigo = *code;
        (*np)->ch = 0;
    }
    if(*++code == '\0')
        (*np)->ch = ch;
    else if(*code == '0')
        insere(&(*np)->left, ch, code);
    else // if(*code == '1')
        insere(&(*np)->right, ch, code);
}

void inorder(node root){
    if(root != NULL){
        inorder(root->left);
        inorder(root->right);
        free(root);
    }
    return;
}

int main(void){
    node root = calloc(1, sizeof(*root));
    int i, N, M;
    scanf("%d", &N);
    char item, num[2*N+1];
    num[0] = ' ';//dummy for root
    for (i = 0; i < N; i++){
        scanf(" %c %s", &item, num+1);
        insere(&root, item, num);
    }
    scanf("%d", &M);
    char buf[M+1], out[M];
    scanf("%s", buf);
    decode(out, buf, root);
    printf("%s\n", out);
    inorder(root);
    return 0;
}

#if 0
         root (root is special node)
      /         \
     0           1
    / \         / \
   0   1       0   1
  (o) / \     / \ (e)
     0   1   0   1
    (p) (l) (x) (m)
#endif
void decode_huff(node * root , string s){
 int len = s.length();
 node *my_root = root;
 for(int i = 0 ; i<len  ; i++){
      if(s[i] == '1')
           my_root=my_root->right;        
      else
           my_root = my_root->left;
      if(my_root->data){
           cout<<my_root->data;
           my_root=root;
      }
 }  
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM