数据结构第七次试验

一、实验题目

设计一个程序exp7-6.cpp,构造一棵哈夫曼树,输出对应的哈夫曼编码和平均查找长度。并用表7.8所示的数据进行验证。

表7.8 单词及出现的频度

单词

The

of

a

to

and

in

that

he

is

at

on

for

His

are

be

出现频度

1192

677

541

518

462

450

242

195

190

181

174

157

138

124

123

 

二、实验目的

掌握哈夫曼树的构造过程和哈夫曼编码的产生方法;灵活运用二叉树这种数据结构解决一些综合应用问题。

三、实验要求

针对程序exp7-6.cpp,输出结果如下:

#include 
#include 
#include 
#include 
#define N 50
#define M 2*N-1
using namespace std;
typedef struct
{
    string data;
    double weight;
    int parent;
    int lchild;
    int rchild;
}HTNode;
typedef struct
{
    char cd[N];
    int start;
}HCode;
void CreateHT(HTNode ht[],int n)
{
    int i,k,lnode,rnode;
    double min1,min2;
    for(i=0;i<2*n-1;i++)
    {
        ht[i].parent=ht[i].lchild=ht[i].rchild=-1;
    }
    for(i=n;i<2*n-1;i++)
    {
        min1=min2=32767;
        lnode=rnode=-1;
        for(k=0;k<=i-1;k++)
        {
            if(ht[k].parent==-1)
            {
                if(ht[k].weight


你可能感兴趣的:(数据结构)