以{8,5,3,2,9,11,2}为叶子结点的权值构造哈夫曼树,并求其带权路径长度.

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/08 00:38:34
以{8,5,3,2,9,11,2}为叶子结点的权值构造哈夫曼树,并求其带权路径长度.

以{8,5,3,2,9,11,2}为叶子结点的权值构造哈夫曼树,并求其带权路径长度.
以{8,5,3,2,9,11,2}为叶子结点的权值构造哈夫曼树,并求其带权路径长度.

以{8,5,3,2,9,11,2}为叶子结点的权值构造哈夫曼树,并求其带权路径长度.
1.struct_tree.h
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
typedef struct tree
{
char data;
int weight;
struct tree *parent;
struct tree *Lchild;
struct tree *Rchild;
}bitnode,*bitree;
typedef struct node
{
char ch;
struct node *next;
}linknode,*linklist;
2.stack.h
typedef struct stack
{
bitree ch;
struct stack *next;
}linkstacknode,*linkstack;
linkstack initstack()
{
linkstack s;
s=(linkstack)malloc(sizeof(linkstacknode));
s->next=NULL;
return s;
}
linkstack clearstack(linkstack s)
{
s->next=NULL;
return s;
}
int isempty(linkstack s)
{
if(s->next==NULL)
return 1;
return 0;
}
int push(linkstack s,bitree ch)
{
linkstacknode *temp;
temp=(linkstacknode *)malloc(sizeof(linkstacknode));
if(temp==NULL)
return 0;
temp->ch=ch;
temp->next=s->next;
s->next=temp;
return 1;
}
int pop(linkstack s,bitree *ch)
{
linkstacknode *temp;
temp=s->next;
if(temp==NULL)
return 0;
s->next=temp->next;
*ch=temp->ch;
free(temp);
return 1;
}
int gettop(linkstack s,bitree *ch)
{
linkstacknode *temp;
temp=s->next;
if(temp==NULL)
return 0;
*ch=temp->ch;
return 1;
}
3.queue.h
typedef struct queue
{
bitree ch;
struct queue *next;
}linkqueuenode;
typedef struct
{
linkqueuenode *front;
linkqueuenode *rear;
}linkqueue;
linkqueue* initqueue()
{
linkqueue *q;
q=(linkqueue *)malloc(sizeof(linkqueue));
q->front=(linkqueuenode *)malloc(sizeof(linkqueuenode));
if(q->front!=NULL)
{
q->rear=q->front;
q->front->next=NULL;
return q;
}
return NULL;
}
int enterqueue(linkqueue *q,bitree ch)
{
linkqueuenode *newnode;
newnode=(linkqueuenode *)malloc(sizeof(linkqueuenode));
if(newnode!=NULL)
{
newnode->ch=ch;
newnode->next=NULL;
q->rear->next=newnode;
q->rear=newnode;
return 1;
}
return 0;
}
int deletequeue(linkqueue *q,bitree *ch)
{
linkqueuenode *temp;
if(q->front==q->rear)
return 0;
temp=q->front->next;
q->front->next=temp->next;
if(q->rear==temp)
q->rear=q->front;
*ch=temp->ch;
free(temp);
return 1;
}
int getqueue(linkqueue *q,bitree *ch)
{
linkqueuenode *temp;
if(q->front==q->rear)
return 0;
temp=q->front->next;
*ch=temp->ch;
return 1;
}
4.主程序
typedef struct queue
{
bitree ch;
struct queue *next;
}linkqueuenode;
typedef struct
{
linkqueuenode *front;
linkqueuenode *rear;
}linkqueue;
linkqueue* initqueue()
{
linkqueue *q;
q=(linkqueue *)malloc(sizeof(linkqueue));
q->front=(linkqueuenode *)malloc(sizeof(linkqueuenode));
if(q->front!=NULL)
{
q->rear=q->front;
q->front->next=NULL;
return q;
}
return NULL;
}
int enterqueue(linkqueue *q,bitree ch)
{
linkqueuenode *newnode;
newnode=(linkqueuenode *)malloc(sizeof(linkqueuenode));
if(newnode!=NULL)
{
newnode->ch=ch;
newnode->next=NULL;
q->rear->next=newnode;
q->rear=newnode;
return 1;
}
return 0;
}
int deletequeue(linkqueue *q,bitree *ch)
{
linkqueuenode *temp;
if(q->front==q->rear)
return 0;
temp=q->front->next;
q->front->next=temp->next;
if(q->rear==temp)
q->rear=q->front;
*ch=temp->ch;
free(temp);
return 1;
}
int getqueue(linkqueue *q,bitree *ch)
{
linkqueuenode *temp;
if(q->front==q->rear)
return 0;
temp=q->front->next;
*ch=temp->ch;
return 1;
}

以{8,5,3,2,9,11,2}为叶子结点的权值构造哈夫曼树,并求其带权路径长度. 2.有7个带权结点,其权值分别为4,7,8,2,5,16,30,试以它们为叶子结点构造一棵哈夫曼树(要求按每个 有七个带权节点,其权值分别是3 7 8 2 6 10 14,以他们的叶子为结点构造哈夫曼树,计算带权路径长度 关于哈夫曼树的问题由权值分别为3,8,6,2,5的叶子结点生成一棵哈夫曼树,它的带权路径长度为多少? 有一棵树,度数为3的结点数N1,度数为2的结点数N2,其余为叶子,有几片叶子?最好有具体过程 由权值分别为11、8、、6、2、5的叶子结点生成一棵哈夫曼树,它的带权路径长度是多少? 一只青蛙沿着一连串的睡莲叶子跳跃.在每片叶子上,它通过掷硬币决定是向前跳两个睡莲叶子还是向后跳一个睡莲叶子.则它跳过的睡莲叶子与总睡莲叶子的比值是多少?A.1/3B._____(√5-1)/2C.____(3 设树T的度为4,其中度为1,2,3,4的结点个数分别为4,2,1,1.则T中的叶子节点数为:A 5B 6C 7D 8 设树T的度为4,其中度为1,2,3,4的结点个数分别为4,2,1,1.则T中的叶子节点数为()A.8 B.7 C.6 D.5 选择题( )能使叶子变出各种颜色. 1、太阳 2、太阳光3、叶子的色素 1片叶子代表什么?2片叶子代表什么?3片叶子代表什么?4片叶子又代表什么?1片叶子代表信仰,2片叶子代表希望,3片叶子代表爱情,而4片叶子就代表幸运. 英语翻译:鸵鸟以草、叶子和昆虫为食. 对数运算!以4为底以9为真数的对数×以25为底以8真数的对数×以27为底以125的对数1.以4为底以9为真数的对数×以25为底以8真数的对数×以2为底7以125为真数的对数=?2.(以5为底以√2为真数的对数 A,B,C,D,E,F 使用频率比为2﹕9﹕5﹕7﹕8 :14 画出构造过程并输出六个字符的哈夫曼编码假定编码系统中有六个字符A,B,C,D,E,F,它们的使用频率比为2﹕9﹕5﹕7﹕8 :14,以这些频率值作叶子的权构造哈 一到noip的模拟题给出一组顶点(顶点值用A,B,C,D,E,F表示),其对应权值分别为2,3,1,7,8,4.请以A,B,C,D,E,F为叶子顶点构造一棵哈夫曼树,并求出它的最小带权路径长度WPL的值.为什么答案是61,而我总 由五个带权值为9,2,3,5,14的叶子结点构成哈夫曼树,带权路径长度为:()我做的结果是 1*14+2*9+3*5+4*(2+3)=67 对不对 某二叉树有5个度为2的结点,则叶子接点数为__? 设树T的度为4,其中度为1,2,3,4,的结点个数分别是4,2,1,1,则T中的叶子结点为 A.8 B.7 C,6 D.5答案是8我是根据 2(k-1)算出来的2的3次方就是8但是有一点疑惑的是 叶子结点是不是就是度为0的那个 那不