public Huffman(IEnumerable<KeyValuePair<char, int>> kvps) { //保存原始数据 var tmpOriginalNodes = from kvp in kvps select new HuffmanNode(kvp.Key, kvp.Value); //创建最小优先队列,并输入数据 MinPriorityQueue<HuffmanNode> minQueue = new MinPriorityQueue<HuffmanNode>(); originalNodes = new List<HuffmanNode>(); foreach (var node in tmpOriginalNodes) { originalNodes.Add(node); minQueue.Insert(node); } //建造编码树,并取得编码树的根节点 while (!minQueue.IsEmpty) { HuffmanNode left = minQueue.ExtractMin(); if (minQueue.IsEmpty) { rootNode = left; break; } HuffmanNode right = minQueue.ExtractMin(); HuffmanNode newNode = new HuffmanNode(null, left.Value + right.Value, left, right); left.Parent = newNode; right.Parent = newNode; minQueue.Insert(newNode); } }
public Huffman(IEnumerable <KeyValuePair <char, int> > kvps) { //保存原始数据 var tmpOriginalNodes = from kvp in kvps select new HuffmanNode(kvp.Key, kvp.Value); //创建最小优先队列,并输入数据 MinPriorityQueue <HuffmanNode> minQueue = new MinPriorityQueue <HuffmanNode>(); originalNodes = new List <HuffmanNode>(); foreach (var node in tmpOriginalNodes) { originalNodes.Add(node); minQueue.Insert(node); } //建造编码树,并取得编码树的根节点 while (!minQueue.IsEmpty) { HuffmanNode left = minQueue.ExtractMin(); if (minQueue.IsEmpty) { rootNode = left; break; } HuffmanNode right = minQueue.ExtractMin(); HuffmanNode newNode = new HuffmanNode(null, left.Value + right.Value, left, right); left.Parent = newNode; right.Parent = newNode; minQueue.Insert(newNode); } }
static void Main(string[] args) { MaxPriorityQueue <IHeapValue> maxheap = new MaxPriorityQueue <IHeapValue>(); MinPriorityQueue <IHeapValue> minheap = new MinPriorityQueue <IHeapValue>(); List <IHeapValue> list = new List <IHeapValue>(); Random rnd = new Random(DateTime.Now.Millisecond); for (int i = 0; i < 10; i++) { HeapNode hn = new HeapNode() { Value = rnd.Next(0, 100) }; list.Add(hn); maxheap.Insert(hn); minheap.Insert(hn); } Console.WriteLine("RandomData:"); list.ForEach(n => Console.Write("{0},", n.Value)); Console.WriteLine(Environment.NewLine + "MaxHeapOutput:"); while (!maxheap.IsEmpty) { Console.Write("{0},", maxheap.ExtractMax().Value); } Console.WriteLine(Environment.NewLine + "MinHeapOutput:"); while (!minheap.IsEmpty) { Console.Write("{0},", minheap.ExtractMin().Value); } Console.ReadKey(); }
static void Main(string[] args) { MaxPriorityQueue<IHeapValue> maxheap = new MaxPriorityQueue<IHeapValue>(); MinPriorityQueue<IHeapValue> minheap = new MinPriorityQueue<IHeapValue>(); List<IHeapValue> list = new List<IHeapValue>(); Random rnd = new Random(DateTime.Now.Millisecond); for (int i = 0; i < 10; i++) { HeapNode hn = new HeapNode() { Value = rnd.Next(0, 100) }; list.Add(hn); maxheap.Insert(hn); minheap.Insert(hn); } Console.WriteLine("RandomData:"); list.ForEach(n => Console.Write("{0},", n.Value)); Console.WriteLine(Environment.NewLine + "MaxHeapOutput:"); while (!maxheap.IsEmpty) { Console.Write("{0},", maxheap.ExtractMax().Value); } Console.WriteLine(Environment.NewLine + "MinHeapOutput:"); while (!minheap.IsEmpty) { Console.Write("{0},", minheap.ExtractMin().Value); } Console.ReadKey(); }