哈夫曼树课程设计

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

哈弗曼编码程序说明书一、使用手册:

1、在字符里面输入字符(单个字符)以空格隔开,输入最大字符数量100;

2、输入完成后点击哈弗曼编码按钮,然后再右边的列表控件中显示出字符、权值以及相应的哈弗曼代码。

3、运行完一次后点击清空按钮。清空右边列表控件的内容,然后再测试下一组值。可以用delete键删除

二.可行性分析

通过对输入编辑框中的字符进行分型,通过最这个字符进行遍历,获得每个字母的重复次数。以此作为该字符的权值存入数组weight[]中,并与字符数组相对应,并将权值作为参数传入Status CHafumanDlg::HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n)中,利用对应的处理函数获得哈弗曼编码,最后输出在列表控件中。

二、概要设计

在vc++6.0中生成的改程序

1、建立基于对话框的程序,在类CHafumanDlg中添加构造哈弗曼树的代码;

2、向对话框中添加静态文本控件(caption改为字符);接着添加编辑框并关联String类型的关联变量m_zifu,还有两个按钮(caption 分别改为清空和哈弗曼代码)以及一个列表控件,并关联一个CLisrCtrl类型的变量m_list;然后分别为清空和哈弗曼编码按钮添加相应的响应函数

三、源代码

1构造哈弗曼树的代码

(1)// hafumanDlg.h : header file//头文件

//

#if !defined(AFX_HAFUMANDLG_H__ACA655FF_81FF_452A_855C_32381C74 3BB5__INCLUDED_)

#define

AFX_HAFUMANDLG_H__ACA655FF_81FF_452A_855C_32381C743BB5__INC LUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////

// CHafumanDlg dialog

#define ok 1

#define error 0

typedef int Status;

typedef struct

{

unsigned int weight;

unsigned int parent,lchild,rchild;

}HTNode, *HuffmanTree;//动态分配数组存储赫夫曼树

typedef char * *HuffmanCode;//动态分配数组存储赫夫曼编码表

class CHafumanDlg : public CDialog

{

// Construction

public:

Status HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n);

Status Select(HuffmanTree HT,int n,int &n1,int &n2);

CHafumanDlg(CWnd* pParent = NULL); // standard constructor

};

(2)// hafumanDlg.cpp : implementation file

//

#include "stdafx.h"

#include "hafuman.h"

#include "hafumanDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CAboutDlg dialog used for App About

Status CHafumanDlg::Select(HuffmanTree HT, int n, int &n1, int &n2)

{

// 在HT[i-1]中选择parent为0求weight最小的两个节点

int i;

for (i=1;i<=n && HT[i].parent!=0 ;i++);

n1=i;

for (i=1;i<=n;i++)

if (HT[i].parent==0 && HT[i].weight

for (i=1; i<=n ; i++)

if (HT[i].parent==0 && i!=n1) break;

n2=i;

for (i=1;i<=n;i++)

if ( HT[i].parent==0 && i!=n1 && HT[i].weight

return ok;

(3)// hafumanDlg.cpp : implementation file

//

#include "stdafx.h"

#include "hafuman.h"

#include "hafumanDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CAboutDlg dialog used for App About

Status CHafumanDlg::HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int

相关文档
最新文档