*紧急*为什么我的霍夫曼代码程序不能正常工作? (任何建议?) [英] *URGENT* Why isn't my Huffman Code program working properly? (Any Advice?)

查看:69
本文介绍了*紧急*为什么我的霍夫曼代码程序不能正常工作? (任何建议?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我创建了一个程序,我想输出一个Huffman树的结果,而我实际上并没有得到任何输出(只是一个冒号)。有人可以通过获取我的代码(每个代码块作为不同的代码文件)和
测试它来找到一个可以做它应该做的解决方案的一些帮助吗?任何帮助都会被大大占用。谢谢!

使用System.Collections.Generic; 
使用System.Linq;

名称空间Huffman1
{
内部类PriorityQueue< T>
{
private readonly SortedDictionary< int,Queue< T>> _sortedDictionary = new SortedDictionary< int,Queue< T>>();

public int Count {get;私人集; }

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.cs 
namespace Huffman1
{
内部类HuffmanNode
{
public HuffmanNode Parent {get;组; }
public HuffmanNode Left {get;组; }
public HuffmanNode Right {get;组; }
public char Value {get;组; }
public int Count {get;组; }
}
}




使用System.Collections; 
使用System.Collections.Generic;

名称空间Huffman1
{
内部类HuffmanTree
{
private readonly HuffmanNode _root;
私人IDictionary计数;

public HuffmanTree(IEnumerable< KeyValuePair< char,int>> count)
{
var priorityQueue = new PriorityQueue< HuffmanNode>();

foreach(KeyValuePair< char,int> kvp in counts)
{
priorityQueue.Enqueue(new HuffmanNode {Value = kvp.Key,Count = kvp.Value},kvp 。值);
}

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计数)
{
this.counts = counts;
}

public IDictionary< char,string> CreateEncodings()
{
var encodings = new Dictionary< char,string>();
编码(_root,"",编码);
返回编码;
}

private void Encode(HuffmanNode节点,字符串路径,IDictionary< char,字符串>编码)
{
if(node?.Left!= null)
{
Encode(node.Left,path +" 0",encodings);
Encode(node.Right,path +" 1",encodings);
}
else
{
encodings.Add(node == null?'\ 0':node.Value,path);
}
}
}
}


使用System; 
使用System.Collections;
使用System.Collections.Generic;

名称空间Huffman1
{
class程序
{
static void Main(string [] args)
{
IDictionary计数= 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树=新的HuffmanTree(计数);
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();
}
}
}

解决方案

嗨goofy_goober,


>> 有人可以提供一些吗?通过获取我拥有的代码(每个代码块作为不同的代码文件)并测试它以找到一个能够完成它应该做的解决方案的帮助? 


因为名为  _root的变量为null,所以您无法显示任何内容。请使用另一个构造并修改Main方法,如下所示:

 static void Main(string [] args)
{
List< KeyValuePair< char,int>> counts = new List< KeyValuePair< char,int>>();
// ac bca ba z
counts.Add(new KeyValuePair< char,int>('',3));
counts.Add(new KeyValuePair< char,int>('b',2));
counts.Add(new KeyValuePair< char,int>('a',3));
counts.Add(new KeyValuePair< char,int>('c',2));
counts.Add(new KeyValuePair< char,int>('z',1));
counts.Add(new KeyValuePair< char,int>('\ n',1));

HuffmanTree树=新的HuffmanTree(计数);
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();
}


祝你好运,


章龙


So, I've created a program that I want to output the results of a Huffman tree, and i'm not really getting anything for my output (just a colon). Could someone please offer some help by taking the code I have (each code block as a different code file) and testing it to find a solution that will do what it is supposed to do? Any help would be greatly appropriated. Thank you!

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.cs
namespace 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 == null ? '\0' : 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();
        }
    }
}

解决方案

Hi goofy_goober,

>> Could someone please offer some help by taking the code I have (each code block as a different code file) and testing it to find a solution that will do what it is supposed to do? 

Because the variable named _root is null, so that you could not show anything. please use another construct and modify the Main method like this:

static void Main(string[] args)
{
            List<KeyValuePair<char, int>> counts = new List<KeyValuePair<char, int>>();
            // ac bca ba z
            counts.Add(new KeyValuePair<char, int>(' ', 3));
            counts.Add(new KeyValuePair<char, int>('b', 2));
            counts.Add(new KeyValuePair<char, int>('a', 3));
            counts.Add(new KeyValuePair<char, int>('c', 2));
            counts.Add(new KeyValuePair<char, int>('z', 1));
            counts.Add(new KeyValuePair<char, int>('\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();
}

Best regards,

Zhanglong


这篇关于*紧急*为什么我的霍夫曼代码程序不能正常工作? (任何建议?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆