Advertisement
VART1X

Untitled

Mar 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.83 KB | None | 0 0
  1. //Program.cs
  2. using System;
  3. using System.Collections;
  4.  
  5. namespace TaskIV
  6. {
  7.     class Programm
  8.     {
  9.         static void Main()
  10.         {
  11.             string input = "foo bar";
  12.             HuffmanTree huffmanTree = new HuffmanTree();
  13.  
  14.             // Build the Huffman tree
  15.             huffmanTree.Build(input);
  16.  
  17.             // Encoding
  18.             BitArray encoded = huffmanTree.Encode(input);
  19.  
  20.             Console.Write("Encoded: ");
  21.             foreach (bool bit in encoded)
  22.             {
  23.                 Console.Write((bit ? 1 : 0) + "");
  24.             }
  25.             Console.WriteLine();
  26.  
  27.             // Decoding
  28.             string decoded = huffmanTree.Decode(encoded);
  29.  
  30.             Console.WriteLine("Decoded: " + decoded);
  31.  
  32.             Console.ReadLine();
  33.         }
  34.     }
  35. }
  36.  
  37. //HuffmanTree.cs
  38. using System.Collections.Generic;
  39. using System.Linq;
  40. using System.Collections;
  41.  
  42.  
  43. namespace TaskIV
  44. {
  45.         public class HuffmanTree
  46.         {
  47.             private List<Node> nodes = new List<Node>();
  48.             public Node Root { get; set; }
  49.             public Dictionary<char, int> Frequencies = new Dictionary<char, int>();
  50.  
  51.             public void Build(string source)
  52.             {
  53.                 for (int i = 0; i < source.Length; i++)
  54.                 {
  55.                     if (!Frequencies.ContainsKey(source[i]))
  56.                     {
  57.                         Frequencies.Add(source[i], 0);
  58.                     }
  59.  
  60.                     Frequencies[source[i]]++;
  61.                 }
  62.  
  63.                 foreach (KeyValuePair<char, int> symbol in Frequencies)
  64.                 {
  65.                     nodes.Add(new Node() { Symbol = symbol.Key, Frequency = symbol.Value });
  66.                 }
  67.  
  68.                 while (nodes.Count > 1)
  69.                 {
  70.                     List<Node> orderedNodes = nodes.OrderBy(node => node.Frequency).ToList<Node>();
  71.  
  72.                     if (orderedNodes.Count >= 2)
  73.                     {
  74.                         // Take first two items
  75.                         List<Node> taken = orderedNodes.Take(2).ToList<Node>();
  76.  
  77.                         // Create a parent node by combining the frequencies
  78.                         Node parent = new Node()
  79.                         {
  80.                             Symbol = '*',
  81.                             Frequency = taken[0].Frequency + taken[1].Frequency,
  82.                             Left = taken[0],
  83.                             Right = taken[1]
  84.                         };
  85.  
  86.                         nodes.Remove(taken[0]);
  87.                         nodes.Remove(taken[1]);
  88.                         nodes.Add(parent);
  89.                     }
  90.  
  91.                     this.Root = nodes.FirstOrDefault();
  92.  
  93.                 }
  94.  
  95.             }
  96.  
  97.             public BitArray Encode(string source)
  98.             {
  99.                 List<bool> encodedSource = new List<bool>();
  100.  
  101.                 for (int i = 0; i < source.Length; i++)
  102.                 {
  103.                     List<bool> encodedSymbol = this.Root.Traverse(source[i], new List<bool>());
  104.                     encodedSource.AddRange(encodedSymbol);
  105.                 }
  106.  
  107.                 BitArray bits = new BitArray(encodedSource.ToArray());
  108.  
  109.                 return bits;
  110.             }
  111.  
  112.             public string Decode(BitArray bits)
  113.             {
  114.                 Node current = this.Root;
  115.                 string decoded = "";
  116.  
  117.                 foreach (bool bit in bits)
  118.                 {
  119.                     if (bit)
  120.                     {
  121.                         if (current.Right != null)
  122.                         {
  123.                             current = current.Right;
  124.                         }
  125.                     }
  126.                     else
  127.                     {
  128.                         if (current.Left != null)
  129.                         {
  130.                             current = current.Left;
  131.                         }
  132.                     }
  133.  
  134.                     if (IsLeaf(current))
  135.                     {
  136.                         decoded += current.Symbol;
  137.                         current = this.Root;
  138.                     }
  139.                 }
  140.  
  141.                 return decoded;
  142.             }
  143.  
  144.             public bool IsLeaf(Node node)
  145.             {
  146.                 return (node.Left == null && node.Right == null);
  147.             }
  148.  
  149.         }
  150.     }
  151.  
  152. // Node.cs
  153. using System.Collections.Generic;
  154.  
  155. namespace TaskIV
  156. {
  157.    public class Node
  158.     {
  159.         public char Symbol { get; set; }
  160.         public int Frequency { get; set; }
  161.         public Node Right { get; set; }
  162.         public Node Left { get; set; }
  163.  
  164.         public List<bool> Traverse(char symbol, List<bool> data)
  165.         {
  166.             // Leaf
  167.             if (Right == null && Left == null)
  168.             {
  169.                 if (symbol.Equals(Symbol))
  170.                 {
  171.                     return data;
  172.                 }
  173.                 else
  174.                 {
  175.                     return null;
  176.                 }
  177.             }
  178.             else
  179.             {
  180.                 List<bool> left = null;
  181.                 List<bool> right = null;
  182.  
  183.                 if (Left != null)
  184.                 {
  185.                     List<bool> leftPath = new List<bool>();
  186.                     leftPath.AddRange(data);
  187.                     leftPath.Add(false);
  188.  
  189.                     left = Left.Traverse(symbol, leftPath);
  190.                 }
  191.  
  192.                 if (Right != null)
  193.                 {
  194.                     List<bool> rightPath = new List<bool>();
  195.                     rightPath.AddRange(data);
  196.                     rightPath.Add(true);
  197.                     right = Right.Traverse(symbol, rightPath);
  198.                 }
  199.  
  200.                 if (left != null)
  201.                 {
  202.                     return left;
  203.                 }
  204.                 else
  205.                 {
  206.                     return right;
  207.                 }
  208.             }
  209.         }
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement