Advertisement
Infiniti_Inter

Huffman v 0.1

Mar 11th, 2020
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7.  
  8.  
  9. public class BinaryTree
  10. {
  11.  
  12.  
  13.     Node tree;
  14.     public BinaryTree() { tree = null; }
  15.  
  16.  
  17.     public void Solve(string s)
  18.     {
  19.         List<Node> tree = new List<Node>();
  20.         SortedDictionary<char, int> cnt = new SortedDictionary<char, int>();
  21.         for (int i = 0; i < s.Length; ++i)
  22.         {
  23.             if (cnt.ContainsKey(s[i]))
  24.                 cnt[s[i]]++;
  25.             else
  26.                 cnt.Add(s[i], 1);
  27.         }
  28.         foreach (var v in cnt)
  29.         {
  30.             Node cur = new Node(v.Value, v.Key);
  31.             tree.Add(cur);
  32.         }
  33.  
  34.  
  35.         while (tree.Count != 1)
  36.         {
  37.             tree.Sort((a, b) => a.inf.CompareTo(b.inf));
  38.  
  39.             Node l = tree[0];
  40.             tree.RemoveAt(0);
  41.  
  42.             Node r = tree[0];
  43.             tree.RemoveAt(0);
  44.  
  45.             Node parent = new Node(ref l, ref r);
  46.             tree.Add(parent);
  47.         }
  48.         Node root = tree[0];
  49.         Node.Print(ref root);
  50.  
  51.         Node.Build(ref root);
  52.         List<int> path = new List<int>();
  53.         Node.dfs(root, path);
  54.  
  55.  
  56.  
  57.     }
  58.  
  59.     public static SortedDictionary<string, char> table = new SortedDictionary<string, char>();
  60.  
  61.     public class Node
  62.     {
  63.  
  64.         public int inf;
  65.         public Node left;
  66.         public Node right;
  67.         char c;
  68.         int code = -1;
  69.  
  70.         public static void dfs(Node root, List<int> path)
  71.         {
  72.             if (root.c != '~')
  73.             {
  74.                 StringBuilder code = new StringBuilder(path.Count + 1);
  75.  
  76.  
  77.                 Console.Write($"{root.c} = ");
  78.                 for (int i = 0; i < path.Count; ++i)
  79.                 {
  80.                     Console.Write(path[i]);
  81.                     code.Append(path[i]);
  82.                 }
  83.                 Console.WriteLine(root.code);
  84.                 code.Append(root.code);
  85.  
  86.                 table.Add(code.ToString(), root.c);
  87.  
  88.  
  89.             }
  90.             if (root.code != -1)
  91.                 path.Add(root.code);
  92.             if (root.left != null)
  93.                 dfs(root.left, path);
  94.             if (root.right != null)
  95.                 dfs(root.right, path);
  96.             if (path.Count > 0)
  97.                 path.RemoveAt(path.Count - 1);
  98.         }
  99.  
  100.         static public void Print(ref Node root, int depth = 0)
  101.         {
  102.             if (root == null)
  103.                 return;
  104.  
  105.             if (root.c != '~')
  106.             {
  107.                 for (int i = 0; i < depth; i++)
  108.                     Console.Write(".");
  109.                 Console.WriteLine(root.c);
  110.  
  111.             }
  112.             else depth++;
  113.             Print(ref root.left, depth);
  114.             Print(ref root.right, depth);
  115.         }
  116.  
  117.  
  118.         public Node(int nodeInf, char nodeChar)
  119.         {
  120.             inf = nodeInf;
  121.             c = nodeChar;
  122.             left = null;
  123.             right = null;
  124.         }
  125.  
  126.         public Node() { left = right = null; }
  127.         public Node(ref Node L, ref Node R)
  128.         {
  129.  
  130.             left = L;
  131.             right = R;
  132.             inf = L.inf + R.inf;
  133.             c = '~';
  134.         }
  135.  
  136.         public static void Build(ref Node root)
  137.         {
  138.  
  139.  
  140.             if (root.left != null)
  141.             {
  142.                 root.left.code = 0;
  143.                 Build(ref root.left);
  144.  
  145.             }
  146.             if (root.right != null)
  147.             {
  148.                 root.right.code = 1;
  149.                 Build(ref root.right);
  150.  
  151.             }
  152.         }
  153.  
  154.  
  155.  
  156.     }
  157.  
  158. }
  159.  
  160.  
  161.  
  162. class Program
  163. {
  164.     static void Main(string[] args)
  165.     {
  166.  
  167.  
  168.         string s = "abbccc";
  169.         BinaryTree tree = new BinaryTree();
  170.         tree.Solve(s);
  171.  
  172.  
  173.         string rev_s = "101111000";
  174.         StringBuilder t = new StringBuilder();
  175.         ICollection<string> a = BinaryTree.table.Keys;
  176.  
  177.         for (int i = 0; i < rev_s.Length; i++)
  178.         {
  179.             t.Append(rev_s[i]);
  180.  
  181.  
  182.             if (a.Contains(t.ToString()))
  183.             {
  184.                 Console.Write(BinaryTree.table[t.ToString()]);
  185.                 t.Clear();
  186.             }
  187.            
  188.         }
  189.         Console.WriteLine();
  190.  
  191.  
  192.  
  193.     }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement