基于树的c语言哈夫曼译码

基于树的c语言哈夫曼译码,c,tree,decoding,prefix,huffman-code,C,Tree,Decoding,Prefix,Huffman Code,我必须解压缩一个用哈夫曼树编码的字符串,但是代码长度可变,并且不是所有的输入都在前缀中,在这种情况下,我应该打印“invalid”并完成执行。输入包括:不同字符的数量;字符及其代码;编码消息的长度;编码信息 如果可以的话,我会问一个更具体的问题,但我真的不知道怎么了,因为我觉得一切都错了 示例输入为: 6. e 11 m 101 x100 L011 第010页 00 18 111001110101001100 它的输出是: “范例” #包括 #包括 #包括 类型定义结构节点 { 结构节点_t*左

我必须解压缩一个用哈夫曼树编码的字符串,但是代码长度可变,并且不是所有的输入都在前缀中,在这种情况下,我应该打印“invalid”并完成执行。输入包括:不同字符的数量;字符及其代码;编码消息的长度;编码信息

如果可以的话,我会问一个更具体的问题,但我真的不知道怎么了,因为我觉得一切都错了

示例输入为: 6. e 11 m 101 x100 L011 第010页 00 18 111001110101001100

它的输出是: “范例”

#包括
#包括
#包括
类型定义结构节点
{
结构节点_t*左,*右;
char*codigo;
无符号字符c;
}*节点;
无效解码(常量字符*s,节点t)
{
节点q=t;
而(*s)
{
如果(*s++='0')q=q->左;
else q=q->right;
如果(q->c)putchar(q->c),q=t;
}
putchar('\n');
}
结构节点插入(结构节点根,字符x,字符h,整数a)
{
如果(!root)
{
root=(结构节点)malloc(sizeof(结构节点));
根->c=x;
根->codigo=h;
根->左=空;
root->right=NULL;
返回(根);
}
其他的
{
如果(h[a]=='0')
{
如果(根->左!=NULL)
{
printf(“无效\n”);
出口(0);
}
根->左=插入(根->左,x,h,a+1);
}
其他的
{
如果(h[a]=='1')
{
如果(根->左!=NULL)
{
printf(“无效\n”);
出口(0);
}
根->右=插入(根->右,x,h,a+1);
}
}
}
返回(根);
}
无效索引(结构节点*根)
{
if(root!=NULL)
{
顺序(根->左);
自由根;
顺序(根->右);
}
返回;
}
内部主(空)
{
结构节点*root;
root=NULL;
int i,N,M,k;
scanf(“%d”和“&N”);
字符项,num[2*N];
对于(i=0;i#包括
#包括
类型定义结构节点{
结构节点_t*左,*右;
煤焦codigo;
char ch;
}*节点;
字符解码(常量字符**s,节点np){
if(!np){
fprintf(stderr,“无效\n”);
出口(0);
}
如果(np->ch)
返回np->ch;
如果(**s=='\0')
返回“\0”;
如果(*(*s)+=='0')
返回解码辅助(s,np->左);
else//if(**s==“1”)
返回解码辅助(s,np->右);
}
无效解码(字符*out,常量字符*s,节点根){
常量char*p=s;
while(*out++=decode_aux(&p,root))
;
}
虚空插入(节点*np,字符ch,字符*代码){
如果(!*np){
*np=malloc(sizeof(**np));
(*np)->左=(*np)->右=空;
(*np)->codigo=*代码;
(*np)->ch=0;
}
如果(*++code=='\0')
(*np)->ch=ch;
如果(*代码=='0',则为else
插图(&(*np)->左,ch,代码);
else//if(*code==“1”)
插图(&(*np)->右侧,ch,代码);
}
无效索引(节点根){
if(root!=NULL){
顺序(根->左);
顺序(根->右);
自由根;
}
返回;
}
内部主(空){
node root=calloc(1,sizeof(*root));
int i,N,M;
scanf(“%d”和“&N”);
字符项,num[2*N+1];
num[0]=“”;//根的伪
对于(i=0;i
无效解码\u huff(节点*根,字符串s){
int len=s.length();
节点*我的根=根;
对于(int i=0;iright;
其他的
我的根=我的根->左;
如果(我的根->数据){

请问错误是什么?分段错误?解析错误?输出错误?
#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;
      }
 }  
}