哈夫曼编码 C#
来源:互联网 发布:淘宝小帮手 编辑:程序博客网 时间:2024/06/05 22:29
我是从《数据结构与算法 C#语言描述》这本书里面抄的 , 程序可以正常运行
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 哈夫曼编码1{ public class HuffmanTree { private HuffmanTree lChild; private HuffmanTree rChild; private string letter; private int freq; public HuffmanTree(string letter) { this.letter = letter; this.freq = 1; } public HuffmanTree LChild { get { return lChild; } set { lChild = value; } } public HuffmanTree RChild { get { return rChild; } set { rChild = value; } } public string Letter { get { return letter; } set { letter = value; } } public int Freq { get { return freq; } set { freq = value; } } public void IncFreq() { freq++; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 哈夫曼编码1{ public class Node { public HuffmanTree data; public Node link; public Node(HuffmanTree newData) { data = newData; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 哈夫曼编码1{ public class TreeList { private int count = 0; private Node first = null; private static string[] signTable = null; private static string[] keyTable = null; public TreeList(string input) { List<char> list = new List<char>(); for (int i = 0; i < input.Length; i++) { if (!list.Contains(input[i])) { list.Add(input[i]); //如果不包含string里的字符,就加上去 } } signTable = new string[list.Count]; keyTable = new string[list.Count]; } public string[] GetSignTable() { return signTable; } public string[] GetKeyTable() { return keyTable; } public void AddLetter(string letter) { HuffmanTree hTemp = new HuffmanTree(letter); Node eTemp = new Node(hTemp); if (first == null) { first = eTemp; } else { eTemp.link = first; first = eTemp; } count++; } public void SortTree() { if (first != null && first.link != null) { Node tmp1; Node tmp2; for (tmp1 = first; tmp1 != null; tmp1 = tmp1.link) { for (tmp2 = tmp1.link; tmp2 != null; tmp2 = tmp2.link) { if (tmp1.data.Freq > tmp2.data.Freq) { HuffmanTree tmpHT = tmp1.data; tmp1.data = tmp2.data; tmp2.data = tmpHT; } } }//按照权重(freq)升序排列 } } /// <summary> /// 去掉结点 /// </summary> /// <returns>去掉的结点</returns> public HuffmanTree RemoveTree() { if (first != null) { HuffmanTree hTmp; hTmp = first.data; first = first.link; count--; return hTmp; } return null; } /// <summary> /// 插入结点 /// </summary> /// <param name="hTmp">等待插入的结点</param> public void InsertTree(HuffmanTree hTmp) { Node eTmp = new Node(hTmp); if (first == null) { first = eTmp; } else { Node p = first; while (p.link != null) { if (p.data.Freq <= hTmp.Freq && p.link.data.Freq >= hTmp.Freq) { break; } p = p.link; } eTmp.link = p.link; p.link = eTmp; } count++; } /// <summary> /// 合并成一棵树 /// </summary> public void MergeTree() { if (first != null) { if (first.link != null) { HuffmanTree aTmp = RemoveTree(); HuffmanTree bTmp = RemoveTree(); HuffmanTree sumTmp = new HuffmanTree("x"); sumTmp.LChild = aTmp; sumTmp.RChild = bTmp; sumTmp.Freq = aTmp.Freq + bTmp.Freq; InsertTree(sumTmp); } } } public int Lenght() { return count; } /// <summary> /// 增加字符 /// </summary> /// <param name="str">等待增加的字符</param> public void AddSign(string str) { if (first == null) { AddLetter(str); return; } Node tmp = first; while (tmp != null) { if (tmp.data.Letter == str) { tmp.data.IncFreq(); return; } tmp = tmp.link; } AddLetter(str); } static public string Translate(string original) { string newStr = ""; for (int i = 0; i < original.Length; i++) { for (int j = 0; j < signTable.Length; j++) { if (original[i].ToString() == signTable[j]) { newStr += keyTable[j]; } } } return newStr; } static int pos = 0; static public void MakeKey(HuffmanTree tree, string code) { if (tree.LChild == null) { signTable[pos] = tree.Letter; keyTable[pos] = code; pos++; } else { MakeKey(tree.LChild, code + "0"); MakeKey(tree.RChild, code + "1"); } } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 哈夫曼编码1{ class Program { static void Main(string[] args) { string input; Console.Write("Enter a string to encode : "); input = Console.ReadLine(); TreeList treeList = new TreeList(input); for (int i = 0; i < input.Length; i++) { treeList.AddSign(input[i].ToString()); } treeList.SortTree(); while (treeList.Lenght() > 1) { treeList.MergeTree(); } TreeList.MakeKey(treeList.RemoveTree(), ""); string newStr = TreeList.Translate(input); string[] signTable = treeList.GetSignTable(); string[] keyTable = treeList.GetKeyTable(); for (int i = 0; i < signTable.Length; i++) { Console.WriteLine(signTable[i] + ": " + keyTable[i]); } Console.WriteLine("The original string is " + input.Length * 16 + "bits long . "); Console.WriteLine("the new string is " + newStr.Length + "bits long . "); Console.WriteLine("The coded string looks like this : " + newStr); Console.Read(); } }}
- 哈夫曼编码 C#
- c# 编码
- C#编码 .
- C# 编码规范
- C#编码规范
- C#编码规范
- C#编码规范
- C#程序编码规范
- C#程序编码规范
- C# 编码规范 zz
- C# 编码规则
- C# 编码规则
- C# 编码规范
- C# 编码规则
- C#程序编码规范
- C#程序编码规范
- C#编码规范
- C# 编码规范
- 查看HP UNIX小型机的信息的命令集
- Apache Http Server和Tomcat 之区别
- linux关键系统服务详解
- ExtJs树的递归算法(Java),Json格式
- unix常用命令
- 哈夫曼编码 C#
- 关于用PIO读取Excel2003和2007的不同版本处理
- nysit 251—AMAZING AUCTION
- tomca下的web工程打包成war
- linux指令大全
- Android图像开源视图:SmartImageView
- something about IBM DB2
- C++备忘录
- 华为QINQ配置