哈弗曼树实现及用途.doc_第1页
哈弗曼树实现及用途.doc_第2页
哈弗曼树实现及用途.doc_第3页
哈弗曼树实现及用途.doc_第4页
哈弗曼树实现及用途.doc_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

哈弗曼树哈夫曼树2009年04月30日 星期四 19:20/vc+6.0下通过测试#include#includeint MAX=65000;struct HtNodeint ww; /权值int llink; /左子节点下标int rlink; /右子节点下标int parent; /父节点下标;/typedef struct HtNode PHtNode;struct HtTreeint m;int root;HtNode *ht;typedef struct HtTree *PHtTree;PHtTree huffman(int m,int *w)PHtTree pht;int i,j;int x1,x2;int m1,m2;pht=(PHtTree)malloc(sizeof (struct HtTree);if(pht=NULL)printf(Out of space !n);return NULL;pht-ht=(struct HtNode *)malloc(sizeof (struct HtNode) * (2*m-1);if(pht-ht=NULL)printf(Out of space !n);return 0;for(i=0;ihti.llink=-1;pht-hti.rlink=-1;pht-hti.parent=-1;if(ihti.ww=wi;else pht-hti.ww=-1;for(i=0;im-1;+i)m1=MAX;m2=MAX;x1=-1;x2=-1;for(j=0;jhtj.wwhtj.parent=-1) m2=m1; x2=x1; m1=pht-htj.ww; x1=j; else if (pht-htj.wwhtj.parent=-1) m2=pht-htj.ww; x2=j; pht-htx1.parent=m+i; pht-htx2.parent=m+i; pht-htm+i.ww=m1+m2; pht-htj.llink=x1; pht-htj.rlink=x2;pht-root=2*m-2;return pht;int main()int w=2,3,5,7,11,13,17,19,23,29,31,37,41;PHtTree pht;pht=huffman(13,w);for(int i=0;ihti.ww);printf(%dt,pht-hti.parent);printf(%dt,pht-hti.llink);printf(%dt,pht-hti.rlink);printf(n);return 0;哈夫曼树2009年04月30日 星期四 19:20/vc+6.0下通过测试#include#includeint MAX=65000;struct HtNodeint ww; /权值int llink; /左子节点下标int rlink; /右子节点下标int parent; /父节点下标;/typedef struct HtNode PHtNode;struct HtTreeint m;int root;HtNode *ht;typedef struct HtTree *PHtTree;PHtTree huffman(int m,int *w)PHtTree pht;int i,j;int x1,x2;int m1,m2;pht=(PHtTree)malloc(sizeof (struct HtTree);if(pht=NULL)printf(Out of space !n);return NULL;pht-ht=(struct HtNode *)malloc(sizeof (struct HtNode) * (2*m-1);if(pht-ht=NULL)printf(Out of space !n);return 0;for(i=0;ihti.llink=-1;pht-hti.rlink=-1;pht-hti.parent=-1;if(ihti.ww=wi;else pht-hti.ww=-1;for(i=0;im-1;+i)m1=MAX;m2=MAX;x1=-1;x2=-1;for(j=0;jhtj.wwhtj.parent=-1) m2=m1; x2=x1; m1=pht-htj.ww; x1=j; else if (pht-htj.wwhtj.parent=-1) m2=pht-htj.ww; x2=j; pht-htx1.parent=m+i; pht-htx2.parent=m+i; pht-htm+i.ww=m1+m2; pht-htj.llink=x1; pht-htj.rlink=x2;pht-root=2*m-2;return pht;int main()int w=2,3,5,7,11,13,17,19,23,29,31,37,41;PHtTree pht;pht=huffman(13,w);for(int i=0;ihti.ww);printf(%dt,pht-hti.parent);printf(%dt,pht-hti.llink);printf(%dt,pht-hti.rlink);printf(n);return 0;java 哈夫曼算法 实现简易的文件压缩和解压(by looking)题目:程序可以将指定的文件用哈夫曼算法压缩为一个新文件,也可以将一个压缩后的文件还原。思路:对指定文件做统计分析,确定各字节的出现频率,以字节频率为权值构造哈夫曼编码,并对原文件逐字节编码: 原文件-位流-字节流实现:(整理完了上载过来)import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import weiss.util.PriorityQueue;interface BitUtils public static final int BITS_PER_BYTES = 8; public static final int DIFF_BYTES = 256; public static final int EOF = 256; / BitInputStream class: Bit-input stream wrapper class./ CONSTRUCTION: with an open InputStream./ *PUBLIC OPERATIONS*/ int readBit( ) - Read one bit as a 0 or 1/ void close( ) - Close underlying streamclass BitInputStream public BitInputStream( InputStream is ) in = is; bufferPos = BitUtils.BITS_PER_BYTES; public int readBit( ) throws IOException if ( bufferPos = BitUtils.BITS_PER_BYTES ) buffer = in.read( ); if( buffer = -1 ) return -1; bufferPos = 0; return getBit( buffer, bufferPos+ ); public void close( ) throws IOException in.close( ); private static int getBit( int pack, int pos ) return ( pack & ( 1 Write one bit (0 or 1)/ void writeBits( vald ) - Write array of bits/ void flush( ) - Flush buffered bits/ void close( ) - Close underlying streamclass BitOutputStream public BitOutputStream( OutputStream os ) bufferPos = 0; buffer = 0; out = os; public void writeBit( int val ) throws IOException buffer = setBit( buffer, bufferPos+, val ); if( bufferPos = BitUtils.BITS_PER_BYTES ) flush( ); public void writeBits( int val ) throws IOException for( int v : val ) writeBit( v ); public void flush( ) throws IOException if( bufferPos = 0 ) return; out.write( buffer ); bufferPos = 0; buffer = 0; public void close( ) throws IOException flush( ); out.close( ); private int setBit( int pack, int pos, int val ) if( val = 1 ) pack |= ( val Return # occurrences of ch/ void setCount( ch, count ) - Set # occurrences of ch/ *ERRORS*/ No error checks.class CharCounter public CharCounter( ) public CharCounter( InputStream input ) throws IOException int ch; while( ( ch = input.read( ) ) != -1 ) theCounts ch +; public int getCount( int ch ) return theCounts ch & 0xff ; public void setCount( int ch, int count ) theCounts ch & 0xff = count; private int theCounts = new int BitUtils.DIFF_BYTES + 1 ;/ Basic node in a Huffman coding tree.class HuffNode implements Comparable public int value; public int weight; public int compareTo( HuffNode rhs ) return weight - rhs.weight; HuffNode left; HuffNode right; HuffNode parent; HuffNode( int v, int w, HuffNode lt, HuffNode rt, HuffNode pt ) value = v; weight = w; left = lt; right = rt; parent = pt; / Huffman tree class interface: manipulate huffman coding tree./ CONSTRUCTION: with no parameters or a CharCounter object./ *PUBLIC OPERATIONS*/ int getCode( ch ) - Return code given character/ int getChar( code ) - Return character given code/ void writeEncodingTable( out ) - Write coding table to out/ void readEncodingTable( in ) - Read encoding table from in/ *ERRORS*/ Error check for illegal code.class HuffmanTree public HuffmanTree( ) theCounts = new CharCounter( ); root = null; public HuffmanTree( CharCounter cc ) theCounts = cc; root = null; createTree( ); public static final int ERROR = -3; public static final int INCOMPLETE_CODE = -2; public static final int END = BitUtils.DIFF_BYTES; /* * Return the code corresponding to character ch. * (The parameter is an int to accomodate EOF). * If code is not found, return an array of length 0. */ public int getCode( int ch ) HuffNode current = theNodes ch ; if( current = null ) return null; String v = ; HuffNode par = current.parent; while ( par != null ) if( par.left = current ) v = 0 + v; else v = 1 + v; current = current.parent; par = current.parent; int result = new int v.length( ) ; for( int i = 0; i result.length; i+ ) result i = v.charAt( i ) = 0 ? 0 : 1; return result; /* * Get the character corresponding to code. */ public int getChar( String code ) HuffNode p = root; for( int i = 0; p != null & i code.length( ); i+ ) if( code.charAt( i ) = 0 ) p = p.left; else p = p.right; if( p = null ) return ERROR; return p.value; / Write the encoding table using character counts /* * Writes an encoding table to an output stream. * Format is character, count (as bytes). * A zero count terminates the encoding table. */ public void writeEncodingTable( DataOutputStream out ) throws IOException for( int i = 0; i 0 ) out.writeByte( i ); out.writeInt( theCounts.getCount( i ) ); out.writeByte( 0 ); out.writeInt( 0 ); /* * Read the encoding table from an input stream in format * given above and then construct the Huffman tree. * Stream will then be positioned to read compressed data. */ public void readEncodingTable( DataInputStream in ) throws IOException for( int i = 0; i BitUtils.DIFF_BYTES; i+ ) theCounts.setCount( i, 0 ); int ch; int num; for( ; ; ) ch = in.readByte( ); num = in.readInt( ); if( num = 0 ) break; theCounts.setCount( ch, num ); createTree( ); /* * Construct the Huffman coding tree. */ private void createTree( ) PriorityQueue pq = new PriorityQueue( ); for( int i = 0; i 0 ) HuffNode newNode = new HuffNode( i, theCounts.getCount( i ), null, null, null ); theNodes i = newNode; pq.add( newNode ); theNodes END = new HuffNode( END, 1, null, null, null ); pq.add( theNodes END ); while( pq.size( ) 1 ) HuffNode n1 = pq.remove( ); HuffNode n2 = pq.remove( ); HuffNode result = new HuffNode( INCOMPLETE_CODE, n1.weight + n2.weight, n1, n2, null ); n1.parent = n2.parent = result; pq.add( result ); root = pq.element( ); private CharCounter theCounts; private HuffNode theNodes = new HuffNode BitUtils.DIFF_BYTES + 1 ; private HuffNode root;public class Hzip public static void compress( String inFile ) throws IOException String compressedFile = inFile + .huf; InputStream in = new BufferedInputStream( new FileInputStream( inFile ) ); OutputStream fout = new BufferedOutputStream( new FileOutputStream( compressedFile ) ); HZIPOutputStream hzout = new HZIPOutputStream( fout ); int ch; while( ( ch = in.read( ) ) != -1 ) hzout.write( ch ); in.close( ); hzout.close( ); public static void uncompress( String compressedFile ) throws IOException String inFile; String extension; inFile = compressedFile.substring( 0, compressedFile.length( ) - 4 ); extension = compressedFile.substring( compressedFile.length( ) - 4 ); if( !extension.equals( .huf ) ) System.out.println( Not a compressed file! ); return; inFile += .uc; /

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论