C#哈夫曼树构造字符串编码

构建abbcccdddd的哈夫曼树,并写出最后编码的二进制串。web

1.统计字符的频次svg

a:1  b:2  c :3  d:4

2.根据权重实现报文以下转换编码

a 100
 b 101
 c 11
 d 0

3.构建字典spa

字符 	a 	 b    c  d
编码 	100  101  11 0

4.C#代码
结果为 1101111111010100000code

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{ 
 
  
    public class tree
    { 
 
  
        public int value;
        public string name;
        public tree left;
        public tree right;
    }

    class Program
    { 
 
  
        static void Main(string[] args)
        { 
 
  
            string lie = "abbcccdddd";
            Hashtable hs = new Hashtable();
            hs.Add("*", "");

            char[] names = lie.ToCharArray();

            Dictionary<string, int> hs_names = new Dictionary<string, int>();
            foreach (char name in names)
            { 
 
  
                if (!hs_names.ContainsKey(name.ToString()))
                { 
 
  
                    hs_names.Add(name.ToString(), 1);
                }
                else
                { 
 
  
                    hs_names[name.ToString()]++;
                }
            }
            hs_names.OrderByDescending(v => v.Value).OrderByDescending(t => t.Key).ToDictionary(t => t.Key, v => v.Value);

            string streat = "left";
            tree tree = new tree();
            int i = 0;
            foreach (KeyValuePair<string, int> hs_name in hs_names)
            { 
 
  
                AddTree(ref tree, hs_name.Key, hs_name.Value, ref streat,i);
                i++;
            }

            TraaAll(tree, ref hs);

            string write = "";
            foreach (char name in names) { 
 
  
                write = write + hs[name.ToString()].ToString();
            }
            Console.WriteLine(write);
            Console.ReadLine();
        }

        public static void AddTree(ref tree tree, string name, int nub, ref string streat,int i)
        { 
 
  
            if (tree.name == null)
            { 
 
  
                if (streat == "left")
                { 
 
  
                    tree _tree = new tree();
                    _tree.name = name;
                    _tree.value = nub;
                    tree.left = _tree;
                    streat = "right";
                }
                else
                { 
 
  
                    tree _tree = new tree();
                    _tree.name = name;
                    _tree.value = nub;
                    tree.right = _tree;
                    tree.value = tree.left.value + tree.right.value;
                    tree.name = "*"+ i;
                    streat = "left";
                }
            }
            else
            { 
 
  
                tree _tree = new tree();
                if (tree.value >= nub)
                { 
 
  
                    _tree.left = new tree();
                    _tree.left.name = name;
                    _tree.left.value = nub;
                    _tree.right = tree;
                    _tree.name = "*"+ i;
                    _tree.value = _tree.left.value + _tree.right.value;
                    tree = _tree;
                }
                else
                { 
 
  
                    _tree.right = new tree();
                    _tree.right.name = name;
                    _tree.right.value = nub;
                    _tree.left = tree;
                    _tree.name = "*"+ i;
                    _tree.value = _tree.left.value + _tree.right.value;
                    tree = _tree;
                }
            }
        }

        public static void TraaAll(tree tree,  ref Hashtable hs)
        { 
 
  
            if (tree.left != null)
            { 
 
  
                if (hs.ContainsKey(tree.left.name))
                { 
 
  
                    hs[tree.left.name] = hs[tree.name] + "0";
                }
                else
                { 
 
  
                    hs.Add(tree.left.name, hs[tree.name] + "0");
                }
                TraaAll(tree.left, ref hs);
            }
            if (tree.right != null)
            { 
 
  
                 if (hs.ContainsKey(tree.right.name))
                { 
 
  
                    hs[tree.left.name] = hs[tree.name] + "1";
                }
                else
                { 
 
  
                    hs.Add(tree.right.name, hs[tree.name] + "1");
                }
                TraaAll(tree.right, ref hs);
            }
        }
    }
}
相关文章
相关标签/搜索