本文介绍了* URGENT *节点是空的错误??? (如何修复第3代码文件第54行的错误?(第3代码块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述using System.Collections.Generic;using System.Linq;namespace Huffman1{ internal class PriorityQueue<T> { private readonly SortedDictionary<int, Queue<T>> _sortedDictionary = new SortedDictionary<int, Queue<T>>(); public int Count { get; private set; } public void Enqueue(T item, int priority) { ++Count; if (!_sortedDictionary.ContainsKey(priority)) _sortedDictionary[priority] = new Queue<T>(); _sortedDictionary[priority].Enqueue(item); } public T Dequeue() { --Count; var item = _sortedDictionary.First(); if (item.Value.Count == 1) _sortedDictionary.Remove(item.Key); return item.Value.Dequeue(); } }}// HuffmanNode.csnamespace Huffman1{ internal class HuffmanNode { public HuffmanNode Parent { get; set; } public HuffmanNode Left { get; set; } public HuffmanNode Right { get; set; } public char Value { get; set; } public int Count { get; set; } }}using System.Collections;using System.Collections.Generic;namespace Huffman1{ internal class HuffmanTree { private readonly HuffmanNode _root; private IDictionary counts; public HuffmanTree(IEnumerable<KeyValuePair<char, int>> counts) { var priorityQueue = new PriorityQueue<HuffmanNode>(); foreach (KeyValuePair<char, int> kvp in counts) { priorityQueue.Enqueue(new HuffmanNode { Value = kvp.Key, Count = kvp.Value }, kvp.Value); } while (priorityQueue.Count > 1) { HuffmanNode n1 = priorityQueue.Dequeue(); HuffmanNode n2 = priorityQueue.Dequeue(); var n3 = new HuffmanNode { Left = n1, Right = n2, Count = n1.Count + n2.Count }; n1.Parent = n3; n2.Parent = n3; priorityQueue.Enqueue(n3, n3.Count); } _root = priorityQueue.Dequeue(); } public HuffmanTree(IDictionary counts) { this.counts = counts; } public IDictionary<char, string> CreateEncodings() { var encodings = new Dictionary<char, string>(); Encode(_root, "", encodings); return encodings; } private void Encode(HuffmanNode node, string path, IDictionary<char, string> encodings) { if (node?.Left != null) { Encode(node.Left, path + "0", encodings); Encode(node.Right, path + "1", encodings); } else { encodings.Add(node.Value,path); } } }}using System;using System.Collections;using System.Collections.Generic;namespace Huffman1{ class Program { static void Main(string[] args) { IDictionary counts = new Dictionary<char, int>(); // ac bca ba z counts.Add(' ', 3); counts.Add('b', 2); counts.Add('a', 3); counts.Add('c', 2); counts.Add('z', 1); counts.Add('\n', 1); HuffmanTree tree = new HuffmanTree(counts); IDictionary<char, string> encodings = tree.CreateEncodings(); foreach (KeyValuePair<char, string> kvp in encodings) { Console.WriteLine((kvp.Key == '\n' ? "EOF" : kvp.Key.ToString()) + ":\t" + kvp.Value); } Console.ReadLine(); } }}推荐答案不应该吗?代替?在前面的if语句中,节点本身可以为空。Shouldn't it be ?. instead? In the previous if statement, the node itself can be null.编码。 添加 ( 节点encodings.Add(node?.Value,path); 这篇关于* URGENT *节点是空的错误??? (如何修复第3代码文件第54行的错误?(第3代码块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-12 20:35