Professional Documents
Culture Documents
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Huffman
{
public class HTreeNode
{
public string Data;
public int Freq;
public HTreeNode Left;
public HTreeNode Right;
class Program
{
//--------------------------------------------------- WriteInt
static void WriteInt(FileStream F, int N)
{
byte B1 = (byte)((N & 0xFF000000) >> 24);
F.WriteByte(B1);
//-------------------====---------------------------- ReadInt
static int ReadInt(FileStream F)
{
byte B1 = (byte)F.ReadByte();
byte B2 = (byte)F.ReadByte();
byte B3 = (byte)F.ReadByte();
byte B4 = (byte)F.ReadByte();
//--------------------------------------------------- Main
static void Main(string[] args)
{
Compress("Test.txt", "CompressedTest.txt");
Decompress("CompressedTest.txt", "DecompressedTest.txt");
}
//---------------------------------------------------- Decompress
static void Decompress(string InputFile, string OutputFile)
{
int[] Freq = new int[256];
HTreeNode H = MakeHuffmanTree(Freq);
if (Bit == 0)
{
N = N.Left;
}
else
{
N = N.Right;
}
}
Fout.WriteByte((byte)(N.Data[0]));
}
Fout.Flush();
Fout.Close();
Fin.Close();
}
//------------------------------------------------ Compress
static void Compress(string InputFile, string OutputFile)
{
int[] Freq = new int[256];
HTreeNode H = MakeHuffmanTree(Freq);
WriteInt(Fout, (int)Fin.Length);
Fout.WriteByte((byte)(H.Data.Length - 1));
Fin.Seek(0, SeekOrigin.Begin);
for (int i = 0; i < Fin.Length; i++)
{
char C = (char) Fin.ReadByte();
HTreeNode N = H;
BitCount++;
if (BitCount == 8)
{
Fout.WriteByte(BitBuffer);
BitBuffer = 0;
BitCount = 0;
}
}
}
Fout.Flush();
Fout.Close();
Fin.Close();
}
//------------------------------------------------- MakeHuffmanTree
public static HTreeNode MakeHuffmanTree(int[] Freq)
{
Heap<HTreeNode> H = new Heap<HTreeNode>(Freq.Length);
H.insert(node, -node.Freq);
}
return H.remove();
}
}
}
Heap.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Huffman
{
class Heap<T>
{
class HeapNode
{
public T Data;
public int Priority;
int i = count - 1;
while (i > 0)
{
int parent = (i - 1) / 2;
i = parent;
}
}
}
public T remove()
{
T RemovedData = HeapArray[0].Data;
//--------------------------------dnHeap
int i = 0;
i = child;
}
}
//-------------------------------
return RemovedData;
}
public int Count()
{
return count;
}
}
}