- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个已排序的链表,但我已经尝试使用以下代码来创建树并打印它。但是我无法获得输出..
NODE insertorder(NODE first,int pixel,int freq, NODE llink,NODE rlink)
{
NODE temp=(NODE)malloc(sizeof(struct node));
NODE cur,prev=NULL;
temp->pix=pixel;
temp->freq=freq;
temp->llink=llink;
temp->rlink=rlink;
temp->link=NULL;
if(first==NULL)
return temp;
cur=first;
while(cur!=NULL&&freq>cur->freq)
{
prev=cur;
cur=cur->link;
}
temp->link=cur;
if(prev!=NULL)
prev->link=temp;
return first;
}
void roots(NODE first)
{
NODE t1=first,t2=first->link;//taking first two elements in the list everytime
if(t2!=NULL)
createtree(t1,t2);
}
void createtree(NODE first,NODE nxt)
{
NODE temp=(NODE)malloc(sizeof(struct node));
temp->pix=0;
temp->freq=first->freq+nxt->freq;
temp->llink=first;
temp->rlink=nxt;
temp->link=NULL;
first=deletefirst(first);
first=deletefirst(first);
//inserting back the new sum of both elements into the same list
first=insertorder(first, temp->pix, temp->freq, temp->llink, temp->rlink);
roots(first);//calling root back
}
printleaf(NODE first,int a[500],int current)
{
if(first->llink!=NULL)
{
a[current]=0;
printleaf(first->llink,a,current+1);
}
if(first->rlink!=NULL)
{
a[current]=1;
printleaf(first->rlink,a,current+1);
}
if(first->llink==NULL&&first->rlink==NULL)
for(int i=0;a[i]!='\0';i++)
printf("%d",a[i]);
}
我的想法是将树根存储回之前存在的同一个列表中,但是当我执行它时我没有得到输出。
最佳答案
在链表中如果有rlink和link则为双链表,从代码看不清楚为什么需要link。插入时,rlink 和 link 应该被使用,在插入点前一个节点和下一个节点都必须安排它们的 rlink 和 link。
关于c - 如何从 C 中的链表构建霍夫曼树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53263923/
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我这样定义了一个二叉树: struct btree { int x; btree* left_child = nullptr; btree* right_child = nul
我有这个霍夫曼代码,旨在返回数组中每个字母的霍夫曼代码并按字母顺序打印它们。问题是它不生成任何输出,而是继续处理,直到我手动退出它。谁能帮我找出错误吗?我认为我的代码是正确的,但我不知道无限循环从何而
动机 想象一下一个哈夫曼压缩文件被部分下载,就像在p2p软件中一样,所以我们首先为整个文件分配磁盘空间,然后开始随机下载文件块。其中一个哈夫曼密码(但我们不知道是哪一个)是一个结束密码,所以如果这个密
以下 block 由霍夫曼 block 标记嵌套 -HUFF---------------------------------------------------------------------0
我是一名优秀的程序员,十分优秀!