资源简介

c# 实现哈夫曼编码压缩文件 一些文件操作及哈夫曼的创建,及保存。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Huffmancode
{
    
    struct SmallPair//定义表示最小2个量的结构体SmallPair
    {
        public int S1;
        public int S2;
    }
    struct BufferNode//定义带下表和结点的结构体BufferNode
    {
        public Node node;
        public int index;
    }
    class Huffmantree//哈夫曼树类
    {
        
        public Node[] huff;
        
        public int leafnum;

        private char[] temp = new char[100];
        
        public Huffmantree(int sizeint leafnumValue) //带结点个数的构造函数
        {
            huff = new Node[size];
            leafnum = leafnumValue;
        }
        
        
        //初始化哈弗曼树
        public void InitTree(Node[] huffValue)
        { 
            for (int i = 1; i <= leafnum; i++)
            {
                
                huff[i] = new Node();
                huff[i].weight= huffValue[i - 1].weight ;
                huff[i].lchild=huff[i].rchild=huff[i].parent=-1;
                huff[i].ch=huffValue[i-1].ch;
            }
            for (int i = leafnum + 1; i <= 2 * leafnum - 1; i++) 
            {
                huff[i] = new Node();
                huff[i].weight = -1;
                huff[i].lchild = huff[i].rchild = huff[i].parent = -1; 
            }
        }
        
        //求当前无双亲且权重最小的两个结点下标
        public SmallPair Selectsort( int newnum)
        {
           
         //Huffman树中找出没有父节点并且权值最小的两个节点
        BufferNode[] buffer=new BufferNode[256]; //存放没有父节点的所有节点
        
            int p=0; //统计buffer中节点的个数
        
            for(int m=1;m<=newnum;m++)
        {
        if(huff[m].parent==-1)
        {
                    buffer[p].node = huff[m];
        buffer[p].index = m;
        p++;
        }
        }
        //对buffer进行冒泡排序选出最小的两个权重的结点
        BufferNode temp;
        for(int i=1;i<=p-1;i++)
        {
        for(int j=2;j<=p-i+1;j++)
        {
        if(buffer[j-1].node.weight         {
        temp = buffer[j-1];
        buffer[j-1] = buffer[j-2];
        buffer[j-2] = temp;
        }
        }
         }
         SmallPair smp;
         smp.S1 = buffer[0].index;
         smp.S2 = buffer[1].index;
         return smp;
         }

        //生成leafnum-1个非叶子结点的循环(共leafnum+(leafnum-1)结点)
        public void InsertTree()
        {
            for(int i=1;i<=leafnum-1;i++)
            {
                SmallPair min2=Selectsort(leafnum +i-1);//对数组huff[1....leafnum+i+1]中无双亲的结点权值进行排序,找出最小的两个结点的下标
                
                double sum=huff[min2.S1].weight+huff[min2.S2].weight;//合并结点后的权值sum并修改相应的parentlchildrchild值
                huff[leafnum + i].weight = sum;
                huff[min2.S1].parent = huff[min2.S2].parent = leafnum + i;
                huff[leafnum + i].lchild = min2.S1;
                huff[leafnum + i].rchild = min2.

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        348  2010-04-21 11:06  Huffmancode\Huffmancode\bin\Debug\coded string.txt

     文件       7680  2010-04-21 11:06  Huffmancode\Huffmancode\bin\Debug\Huffmancode.exe

     文件      22016  2010-04-21 11:06  Huffmancode\Huffmancode\bin\Debug\Huffmancode.pdb

     文件      14328  2010-04-21 10:45  Huffmancode\Huffmancode\bin\Debug\Huffmancode.vshost.exe

     文件        490  2007-07-21 01:33  Huffmancode\Huffmancode\bin\Debug\Huffmancode.vshost.exe.manifest

     文件         88  2010-04-10 12:51  Huffmancode\Huffmancode\bin\Debug\source string.txt

     文件       2572  2010-04-08 02:46  Huffmancode\Huffmancode\Huffmancode.csproj

     文件       4475  2010-04-21 11:06  Huffmancode\Huffmancode\Huffmantree.cs

     文件        692  2010-04-21 11:06  Huffmancode\Huffmancode\Node.cs

     文件        650  2010-04-21 11:04  Huffmancode\Huffmancode\obj\Debug\Huffmancode.csproj.FileListAbsolute.txt

     文件       7680  2010-04-21 11:06  Huffmancode\Huffmancode\obj\Debug\Huffmancode.exe

     文件      22016  2010-04-21 11:06  Huffmancode\Huffmancode\obj\Debug\Huffmancode.pdb

     文件        660  2010-04-10 12:26  Huffmancode\Huffmancode\obj\Release\Huffmancode.csproj.FileListAbsolute.txt

     文件       7168  2010-04-10 12:25  Huffmancode\Huffmancode\obj\Release\Huffmancode.exe

     文件      19968  2010-04-10 12:25  Huffmancode\Huffmancode\obj\Release\Huffmancode.pdb

     文件       4654  2010-04-21 10:58  Huffmancode\Huffmancode\Program.cs

     文件       1386  2010-04-07 21:28  Huffmancode\Huffmancode\Properties\AssemblyInfo.cs

     文件       1674  2010-04-08 13:45  Huffmancode\Huffmancode\ve-B2.tmp

     文件        923  2010-04-07 21:28  Huffmancode\Huffmancode.sln

    ..A..H.     19968  2010-04-21 11:08  Huffmancode\Huffmancode.suo

     目录          0  2010-04-07 21:28  Huffmancode\Huffmancode\obj\Debug\TempPE

     目录          0  2010-04-09 13:47  Huffmancode\Huffmancode\obj\Release\TempPE

     目录          0  2010-04-10 18:32  Huffmancode\Huffmancode\bin\Debug

     目录          0  2010-04-21 11:06  Huffmancode\Huffmancode\obj\Debug

     目录          0  2010-04-10 12:25  Huffmancode\Huffmancode\obj\Release

     目录          0  2010-04-10 12:27  Huffmancode\Huffmancode\bin

     目录          0  2010-04-09 13:47  Huffmancode\Huffmancode\obj

     目录          0  2010-04-07 21:28  Huffmancode\Huffmancode\Properties

     目录          0  2010-04-21 11:06  Huffmancode\Huffmancode

     目录          0  2010-04-07 21:28  Huffmancode

............此处省略3个文件信息

评论

共有 条评论