数据结构实验报告三——哈夫曼文件压缩实验题目:哈夫曼文件压缩实验目标:输入一个有 10k 单词得英文文档
输出压缩后得二进制文件,并计算压缩比
数据结构:栈与哈夫曼树
定义栈()t y ped ef s t ruc t{ﻩchar *el e m;ﻩint s tack s ize;in t t op;}STAC K;2
定义哈夫曼树()typede f struc t{int we ig ht;ﻩint le f t,r i ght;i nt parent;}HTNod e;需要得操作有:1、初始化栈(I n its t a c k)vo id Initst ac k(ST A CK *s){ﻩs->el em=(ch ar *)ma ll o c(s izeof(int)*100 0);s->s t a c ks i ze=1 0 00;ﻩs->to p=-1;}2、压栈(p u sh)vo id pu s h(STAC K *s,int e){ﻩs->e l em[++s->t op]=e;}3、弹栈(pop)void pop(STACK *s,i n t *e){ﻩif(s->top
=-1)ﻩ*e=s->e le m[s->t o p--];}4、构造哈夫曼树(Inith u ffman)v oid In ithuff m an(in t wset[n],int k,HuffTree HT[]){ //构造哈夫曼树i n t i,m;int s1,s2;m=k*2-1;f or(i=0;iweig ht=(ip arent=-1;ﻩﻩHT[i]->l ef t=HT[i]->r i ght=-1;}ﻩf or(i=k;ileft=s1;ﻩﻩHT[i]->r i gh t=s2;ﻩHT[i]->weight=HT[s 1]->w e ig h t+H T[s2]