霍夫曼加密中的Java至C#代码转换错误

问题描述

好吧,您会看到问题是我正在翻译一个用huffman方法加密文本文件的C#Java代码,在Java中原始程序运行完美,但是这里的问题是我在C#中需要它。

我正在尝试将其转换为C#,但它在setPrefixCodes和buildTree方法内部生成4个错误,我什么都不懂,也不知道我在翻译什么错误,或者我需要实现一些东西其他。

我真的需要知道如何正确翻译

帮助!!!!!!

namespace Huffman_v2 
{
    public partial class Form1: Form 
    {
        public Form1() 
        {
            InitializeComponent();
        }

        String documento;

        private static IDictionary<char,string> charPrefixHashMap = new Dictionary<char,string> ();

        internal static HuffmanNode root;

        private void btnCifrar_Click(object sender,EventArgs e) 
        {
            string txtText = documento;
            IDictionary<char,int> freq = new Dictionary<char,int> ();
            for (int i = 0; i < txtText.Length; i++) 
            {
                if (!freq.ContainsKey(txtText[i])) 
                {
                    freq[txtText[i]] = 0;
                }

                freq[txtText[i]] = freq[txtText[i]] + 1;
            }

            txtFrecuencia.Text = freq.ToString();
            root = buildTree(freq);
            setPrefixCodes(root,new StringBuilder());
            StringBuilder s = new StringBuilder();

            for (int i = 0; i < txtText.Length; i++) 
            {
                char c = txtText[i];
                s.Append(charPrefixHashMap[c]);
            }
            string xs = decode(s.ToString());
            txtPrefijos.Text = charPrefixHashMap.ToString();
            txtCifrado.Text = xs.ToString();
        }

        private static void setPrefixCodes(HuffmanNode node,StringBuilder prefix)
        {
            if (node != null) 
            {
                if (node.left == null && node.right == null)
                {
                    charPrefixHashMap[node.data] = prefix.ToString();
                }
                else 
                {
                    prefix.Append('0');
                    setPrefixCodes(node.left,prefix);
                    prefix.deleteCharat(prefix.Length - 1); //error en .deleteChatAt
                    prefix.Append('1');
                    setPrefixCodes(node.right,prefix);
                    prefix.deleteCharat(prefix.Length - 1); //error en .deleteChatAt
                }
            }
        }

        private static string decode(string s)
        {
            StringBuilder stringBuilder = new StringBuilder();
            HuffmanNode temp = root;
            
            for (int i = 0; i < s.Length; i++) 
            {
                int j = int.Parse(s[i].ToString());
                if (j == 0) 
                {
                    temp = temp.left;
                    if (temp.left == null && temp.right == null) 
                    {
                        stringBuilder.Append(temp.data);
                        temp = root;
                    }
                }
                
                if (j == 1) 
                {
                    temp = temp.right;
                    if (temp.left == null && temp.right == null) 
                    {
                        stringBuilder.Append(temp.data);
                        temp = root;
                    }
                }
            }
            return stringBuilder.ToString();
        }

        private static HuffmanNode buildTree(IDictionary < char,int > freq) 
        {
            PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<HuffmanNode>(); // esta linea tiene el error en PriorityQueue<HuffmanNode> 
            ISet<char> keySet = (ISet<char>) freq.Keys;
            
            foreach(char ? c in keySet) {
                HuffmanNode huffmanNode = new HuffmanNode();
                huffmanNode.data = (char) c;
                huffmanNode.frequency = freq.get(c); //aqui en .get
                huffmanNode.left = null;
                huffmanNode.right = null;
                priorityQueue.offer(huffmanNode);
            }
            
            Debug.Assert(priorityQueue.size() > 0);
            while (priorityQueue.size() > 1) 
            {
                HuffmanNode x = priorityQueue.peek();
                priorityQueue.poll();
                HuffmanNode y = priorityQueue.peek();
                priorityQueue.poll();
                HuffmanNode sum = new HuffmanNode();
                sum.frequency = x.frequency + y.frequency;
                sum.data = '-';
                sum.left = x;
                sum.right = y;
                root = sum;
                priorityQueue.offer(sum);
            }
            return priorityQueue.poll();
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)