using System;
using System.Collections.Generic;
using System.Linq;
namespace 哈夫曼樹
{
class Program
{
public static List<HuffmanNode> huffmanNodes= new List<HuffmanNode>();//哈夫曼樹節點集合
public static List<Node> nodes = new List<Node>();//基本節點集合 只有數值和權重
static void Main(string[] args)
{
//新增節點(權重,字元)
nodes.Add(new Node(5, "a"));
nodes.Add(new Node(32, "b"));
nodes.Add(new Node(18, "c"));
nodes.Add(new Node(7, "d"));
nodes.Add(new Node(25, "e"));
//將每個節點轉換成哈夫曼樹的節點
foreach (var node in nodes)
{
HuffmanNode huffman = new HuffmanNode();
huffman.value = node.Value;
huffman.weight = node.Weight;
huffmanNodes.Add(huffman);
}
//生成哈夫曼樹
while (huffmanNodes.Count > 1)
{
//將所有節點按權重升序排序
var sort =
from node in huffmanNodes
orderby node.weight
select node;
//選出權重最小的兩個節點
var min = sort.Take(2).ToArray();
//生成一個父節點 權重為兩個權重最小節點的權重和
HuffmanNode huffman = new HuffmanNode();
huffman.weight = min[0].weight + min[1].weight;
huffman.left = min[0];
huffman.right = min[1];
//為兩個權重最小節點新增父節點
min[0].parent = huffman;
min[1].parent = huffman;
//在集合中移除兩個最小節點
huffmanNodes.Remove(min[0]);
huffmanNodes.Remove(min[1]);
//將父節點新增進集合中
huffmanNodes.Add(huffman);
}
visitTree(huffmanNodes[0], " "," " );
Console.ReadKey();
}
//哈夫曼編碼輸出 左子樹為0 右子樹為1
public static void visitTree(HuffmanNode node, String prefixStr, String addStr)
{
if (node != null)
{
if (!string .IsNullOrEmpty(node.value))
Console.WriteLine(node.value.ToString() + ":" + prefixStr + addStr);
visitTree(node.left, prefixStr + addStr, "0");
visitTree(node.right, prefixStr + addStr, "1");
}
}
}
/// <summary>
/// 定義個Node節點類 用於新增操作
/// </summary>
public class Node
{
public int Weight { get; set; }//權重
public String Value { get; set; }//值
public Node(int weight, string value)
{
Weight = weight;
Value = value;
}
}
/// <summary>
/// 哈夫曼樹節點類
/// </summary>
public class HuffmanNode
{
public int weight;//權重
public String value = "";//值
public HuffmanNode parent;//父節點
public HuffmanNode left;//左孩子
public HuffmanNode right;//右孩子
}
}