Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace hafman
  6. {
  7.     public class HaffmanTree
  8.     {
  9.         public char C { get; private set; }
  10.         public bool IsChar { get; private set; }
  11.         public int Count { get; set; }
  12.         public string Text { get; set; }
  13.  
  14.         public HaffmanTree LeftChild;
  15.         public HaffmanTree RightChild;
  16.  
  17.         public HaffmanTree(char c, bool isChar, int count)
  18.         {
  19.             C = c;
  20.             IsChar = isChar;
  21.             Count = count;
  22.         }
  23.     }
  24.  
  25.     class Hafman
  26.     {
  27.         public HaffmanTree HaffmanTree;
  28.  
  29.         private string[] BuildTree(IEnumerable<char> s)
  30.         {
  31.             var charListIn      = new int[65536];
  32.             var charListOut     = new string[65536];
  33.             var listHaffmanTree = new List<HaffmanTree>();
  34.  
  35.             foreach (var t in s)
  36.             {
  37.                 charListIn[t]++;
  38.             }
  39.  
  40.             for (var i = 0; i < charListIn.Length; i++)
  41.             {
  42.                 if (charListIn[i] > 0)
  43.                     listHaffmanTree.Add(new HaffmanTree((char)i, true, charListIn[i]));
  44.             }
  45.  
  46.             if (listHaffmanTree.Count <= 1)
  47.             {
  48.                 if (listHaffmanTree.Count ==1)
  49.                 {
  50.                     HaffmanTree = listHaffmanTree[0];
  51.                     HaffmanTree.Text = "0";
  52.                 }
  53.                 return charListOut;
  54.             }
  55.  
  56.             while (listHaffmanTree.Count > 1)
  57.             {
  58.                 listHaffmanTree.Sort(new Comparison<HaffmanTree>((a, b) => a.Count - b.Count));
  59.                 listHaffmanTree.Add(Merge(listHaffmanTree[0], listHaffmanTree[1]));
  60.                 listHaffmanTree.RemoveAt(0);
  61.                 listHaffmanTree.RemoveAt(0);
  62.             }
  63.  
  64.             HaffmanTree = listHaffmanTree[0];
  65.             SetText(HaffmanTree, "");
  66.             GetText(HaffmanTree, charListOut);
  67.             return charListOut;
  68.         }
  69.  
  70.         public string Encode(string s)
  71.         {
  72.             var temp = BuildTree(s);
  73.             var sb = new StringBuilder();
  74.             foreach (var a in s)
  75.             {
  76.                 sb.Append(temp[a] + " ");
  77.             }
  78.             return sb.ToString();
  79.         }
  80.  
  81.         private static void SetText(HaffmanTree ht, string s)
  82.         {
  83.             ht.Text = s;
  84.             if(ht.LeftChild!=null)
  85.             {
  86.                 SetText(ht.LeftChild,s+"0");
  87.             }
  88.             if (ht.RightChild != null)
  89.             {
  90.                 SetText(ht.RightChild, s + "1");
  91.             }
  92.         }
  93.  
  94.         private static void GetText(HaffmanTree ht, IList<string> charListOut)
  95.         {
  96.             if(ht.IsChar)
  97.             {
  98.                 charListOut[ht.C] = ht.Text;
  99.             }
  100.             if (ht.LeftChild != null)
  101.             {
  102.                 GetText(ht.LeftChild, charListOut);
  103.             }
  104.             if (ht.RightChild != null)
  105.             {
  106.                 GetText(ht.RightChild, charListOut);
  107.             }
  108.         }
  109.  
  110.         private static HaffmanTree Merge(HaffmanTree a,HaffmanTree b)
  111.         {
  112.             return new HaffmanTree('\0',false,a.Count+b.Count)
  113.                        {
  114.                            LeftChild  = a,
  115.                            RightChild = b
  116.                        };
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement