以{8,5,3,2,9,11,2}为叶子结点的权值构造哈夫曼树,并求其带权路径长度.
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/09/20 16:39:30
以{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;
}
#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;
}
构造哈夫曼树:以数据集(3,4,5,8,11,18,20,30)为结点,构造一棵哈夫曼数,并求其带权路径长度.
以{5,6,7,8,9,10,15,18,22}作为叶子结点的权值构造一颗Huffman树,计算带权路径长度
数据结构中的一道题由权值为9,2,5,7的四个叶子结点构造一棵哈夫曼树,该树的带权路径长度为__(50)__.供选择的答
2.有7个带权结点,其权值分别为4,7,8,2,5,16,30,试以它们为叶子结点构造一棵哈夫曼树(要求按每个
2.设给定一个权值集合W=(3,5,7,9,11),要求根据给定的权值集合构造一棵哈夫曼树并计算哈夫曼树的带权路径长度W
有七个带权结点,其权值分别为3,7,8,2,6,10,14,试以它们为叶结点构造一棵哈夫曼树(请按照每个结点的左子树根结
由分别带权为9,2,5,7的4个叶节点构造一棵哈夫曼树,该树的带权路径长度为()?
数据结构,构造哈夫曼树,求树的带权路径长度
画出以3,4,6,8,12,13,15,18,25,40为结点权值所构造的Huffman树,并对各结点编码
给定权值〔3,9,13,5,7〕,构造相应的哈夫曼树,并计算其大带权路径长度,求发图
数据结构问题:给定一组数据{6,2,7,10,3,12}以它构造一棵哈夫曼树,则树高为5,带权路径96,但是
6、求java算法 已知四个带权的结点:(A,1),(B,2),(C,2),(D,3),构造Huffman数,并给出每个