赫夫曼树编码(编译没有错,运行到一半就崩了,麻烦大神帮忙看一下)
我是用VS2010编的,主函数是这样的:程序代码:
#include "stdafx.h" #include"Huffman.h" int _tmain(int argc, _TCHAR* argv[]) { int n,i; //n是赫夫曼树叶子结点数 char flag = 'y'; int* w; //存放权值 char* c; //存放原始码 Huffman HT; cout<<"------------------------本程序将演示赫夫曼树------------------------"<<endl<<endl; cout<<"是否继续?(输入Y or N)"<<endl; cin>>flag; while(flag == 'y'||flag == 'Y') { cout<<"请输入叶子结点数"<<endl; cin>>n; if(n>1) { w=new int[n]; c=new char[n]; cout<<"请输入原始码及权值"<<endl; for(i=0;i<n;i++){ cin>>c[i]; cin>>w[i]; } HT.HuffmanCoding(w,n); cout<<endl; HT.PrintHuffmanCode(w,n); cout<<"赫夫曼树构造完毕,你还想继续吗?"<<endl; cin>>flag; } else{ cout<<"叶子节点个数不能小于2"<<endl; cout<<"你还想继续吗?"<<endl; cin>>flag; } } delete []c; delete []w; return 0; }
我在头文件"stdafx.h"中添加了:
程序代码:
#pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h> #include <iostream> using namespace std; #include <string.h> typedef char **HuffmanCode; typedef struct{ unsigned int weight; int parent,lchild,rchild; char letter; }HTNode; // TODO: 在此处引用程序需要的其他头文件 #include"Huffman.h"
然后添加了一个类Huffman
头文件类定义:
程序代码:
#pragma once class Huffman { public: HTNode *HT; HuffmanCode HC; public: Huffman(void); ~Huffman(void); void HuffmanCoding(int *w,int n); void PrintHuffmanCode(int* w,int n); void Select(int t, int& s1, int& s2); };
实现函数是这样的:
程序代码:
#include "StdAfx.h" #include "Huffman.h" Huffman::Huffman(void) { } Huffman::~Huffman(void) { } void Huffman::HuffmanCoding(int * w,int n){ int m , i; m = 2 * n - 1; HT = new HTNode[m+1]; HTNode *p = HT+1; for( i=1;i<=n;++i,++p,++w) { p->weight = *w; p->parent = 0; p->lchild = 0; p->rchild = 0; } for(;i<=m;++i,++p) { p->weight = 0; p->parent = 0; p->lchild = 0; p->rchild = 0; } int s1,s2; for(i=n+1;i<=m;++i){ //建赫夫曼树 //在HT[1...i-1]选择parent 为0且weight最小的两个结点,其序号为s1和s2 Select(i-1,s1,s2); HT[s1].parent = i; HT[s2].parent = i; HT[s1].lchild = s1; HT[s2].rchild = s2; HT[i].weight = HT[s1].weight + HT[s2].weight; } HC = new char* [n+1]; //分配n个编码的头指针向量 char* cd = new char[n]; //开辟一个求编码的工作空间 cd[n-1] = '\0'; //结束编码符 int c,f,start; for(i=1;i<=n;++i){ start = n-1; for(c=i,f=HT[i].parent ; f!=0 ; c=f,f=HT[f].parent) if(HT[f].lchild == c) cd[--start] = '\0'; //若是左孩子编为'0' else cd[--start] = '1'; //若是右孩子编为'1' HC[i] = new char[n-start]; //为第i个编码分配存储空间 strcpy_s(HC[i],1,&cd[start]); //将编码从cd复制到HC中 } delete []cd; //释放存储空间 } void Huffman::PrintHuffmanCode(int* w,int n){ //显示有n个叶子结点的赫夫曼树的编码表 int i; cout<<"赫夫曼编码如下:"<<endl; cout<<"权值"<<'\t'<<"赫夫曼编码"<<endl; for(i=1;i<=n;i++) cout<<w[i-1]<<'\t'<<HC[i]; cout<<endl; } void Huffman::Select( int t, int& s1, int& s2){ int i,j,k,least,second; for(i = 1;i <=t ;i++) if(HT[i].parent == 0) break; for(j=i+1;j<=t;j++) if(HT[j].parent == 0) break; if(HT[i].weight < HT[j].weight){ least = i; second = j; } else { least = j; second = i; } for(k=j+1;k<=t;k++) if(HT[k].parent == 0) if(HT[k].weight < HT[least].weight) { second = least; least = k; } else if (HT[k].weight >= HT[least].weight && HT[k].weight < HT[second].weight) second=k; s1=least; s2=second; }
编译没有错,但是每次运行把需要输入的原始码和权值输入完了,就不可以运行了,不知道哪里写着有问题,麻烦帮忙看一下,谢谢
[此贴子已经被作者于2016-5-22 22:45编辑过]