Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace AIKD_01
- {
- class Program
- {
- static void Main()
- {
- var huffman = new Huffman("02.txt");
- huffman.Run();
- huffman.PrintPretty();
- Console.ReadKey();
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- namespace AIKD_01
- {
- internal class Node
- {
- public char? Char { get; set; }
- public Node Left { get; set; }
- public Node Right { get; set; }
- public string Code { get; set; }
- public string Dir { get; set; }
- private int _Count;
- public int Count
- {
- get
- {
- if (Left != null || Right != null)
- {
- _Count = 0;
- if (Left != null)
- _Count += Left.Count;
- if (Right != null)
- _Count += Right.Count;
- }
- return _Count;
- }
- }
- public Node(char? newChar)
- {
- Code = "";
- Char = newChar;
- _Count = 1;
- }
- public void CountAdd()
- {
- _Count++;
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace AIKD_01
- {
- internal class Huffman
- {
- private string FileName, FileText;
- private List<Node> AllNodes;
- private List<Node> CodedList;
- private List<Node> Tree;
- public Huffman(string fileName)
- {
- AllNodes = new List<Node>();
- Tree = new List<Node>();
- CodedList = new List<Node>();
- FileName = fileName;
- }
- public void Run()
- {
- if (LoadFile())
- {
- GenerateNodes();
- Tree = AllNodes;
- GenerateTree();
- GenerateCode();
- }
- foreach (var letter in FileText)
- {
- Console.Write(Convert.ToString(letter, 2) + " ");
- }
- Console.WriteLine("\n");
- foreach (var letter in FileText)
- {
- Console.Write(CodedList.FirstOrDefault(a=>a.Char == letter).Code + " ");
- }
- Console.WriteLine("\n\n");
- }
- private void GenerateCode(Node node = null, string code = "")
- {
- if (node == null)
- node = Tree.First();
- if (node.Char != '+')
- {
- node.Code = code;
- CodedList.Add(node);
- }
- if (node.Left != null)
- GenerateCode(node.Left, code + "0");
- if (node.Right != null)
- GenerateCode(node.Right, code + "1");
- }
- private void GenerateTree()
- {
- while (Tree.Count() != 1)
- {
- Tree = Tree.OrderBy(t => t.Count).ToList();
- Tree[0].Dir = "LEWO";
- Tree[1].Dir = "PRAWO";
- var newNode = new Node(null)
- {
- Left = Tree[0],
- Right = Tree[1]
- };
- Tree.Add(newNode);
- Tree.RemoveAt(0);
- Tree.RemoveAt(0);
- }
- }
- private void GenerateNodes()
- {
- foreach (var Char in FileText)
- {
- if (AllNodes.All(c => c.Char != Char))
- AllNodes.Add(new Node(Char));
- else
- AllNodes.First(a => a.Char == Char).CountAdd();
- }
- }
- private bool LoadFile()
- {
- var textLetters = "";
- try
- {
- using (var sr = new StreamReader(FileName))
- {
- string line;
- while ((line = sr.ReadLine()) != null)
- {
- textLetters += line.ToUpper();
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine($"Nie znaleziono pliku ({e.Message})");
- return false;
- }
- //var pattern = new Regex("[^A-Za-z]|[ ]");
- //textLetters = pattern.Replace(textLetters, "");
- FileText = textLetters;
- return true;
- }
- public void PrintPretty(int layer = 0, Node node = null)
- {
- if (node == null && layer == 0)
- node = Tree.First();
- for (var i = 0; i < layer; i++)
- Console.Write(" ");
- if (node.Char != null)
- Console.WriteLine($"|-'{node.Char}':{node.Count} ({node.Code} : {node.Dir})");
- else
- Console.WriteLine($"|- {node.Count} : {node.Dir}");
- if (node.Left != null)
- PrintPretty(layer + 2, node.Left);
- if (node.Right != null)
- PrintPretty(layer + 2, node.Right);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement