private string getNodeName(HuffmanNode node) { string str; if (node.IsLeaf) { str = " '" + node.ToString() + "'(" + node.Frequency.ToString() + ")\n" + node.getBit(); } else { str = node.ToString() + "\n" + node.getBit(); } return(str); }
public void AssignCode(string code, HuffmanNode node) { if (node == null) { return; } if (node.Left == null && node.Right == null) { node.Code = code; return; } AssignCode(code + "0", node.Left); AssignCode(code + "1", node.Right); return; }
private void updateFreqs(HuffmanNode root) { if (root.LeftChild != null) { updateFreqs(root.LeftChild); } if (root.RightChild != null) { updateFreqs(root.RightChild); } if (root.LeftChild == null && root.RightChild == null) { return; } root.Frequency = root.LeftChild.Frequency + root.RightChild.Frequency; }
private void CreateStaticHuffmanTree(object sender, EventArgs e) { graph = new Microsoft.Msagl.Drawing.Graph("Huffman Tree"); Dtable.Clear(); map = new Dictionary <char, string>(); String text = textBox1.Text; textBox3.Text = ""; if (text.Length < 1) { UpdateGraph(graph); textBox2.Text = ""; encodedText = ""; MessageBox.Show("Can't encode empty input ", "Encoding Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var count2 = text.ToLower().Select(x => x).GroupBy(c => c).Select(chunk => new { Letter = chunk.Key, Count = chunk.Count() }).OrderByDescending(item => item.Count).ThenBy(item => item.Letter); // Language Integrated Query List <HuffmanNode> list = new List <HuffmanNode>(); foreach (var c1 in count2) { //Console.WriteLine("Alphabet :" + c1.Letter + ", Count :" + c1.Count); list.Add(new HuffmanNode(c1.Count, c1.Letter)); } list.Sort(sorter); while (list.Count > 1) { list.Add(new HuffmanNode(list[0], list[1])); list.RemoveAt(0); list.RemoveAt(0); list.Sort(sorter); } topNode = list[0]; createStaticGraph(topNode); encodedText = Encode(); double compressionRatio = 100.0 - Math.Floor((double)encodedText.Length / (double)(topNode.Frequency * 8) * 100 * 100) / 100; this.label7.Text = "Compression Ratio: " + compressionRatio.ToString() + "%"; updateTextBox(encodedText); dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Ascending); UpdateGraph(graph); }
private string getNodeName2(HuffmanNode node) { string str; if (node.IsLeaf) { str = " '" + node.ToString() + "'(" + node.Depth.ToString() + "," + node.Frequency + ")\n" + node.getBit(); } else if (node.LeftChild == null && node.RightChild == null) { return("ZeroNode(" + node.Depth + ")"); } else { str = node.ToString() + "(" + node.Depth + ")" + "\n" + node.getBit(); } return(str); }
public HuffmanNode(HuffmanNode leftChild, HuffmanNode rightChild, int depth) { if (leftChild.parentNode != null) { this.parentNode = leftChild.parentNode; this.parentNode.leftChild = this; } else { this.parentNode = null; } this.leftChild = leftChild; this.leftChild.parentNode = this; this.rightChild = rightChild; this.rightChild.parentNode = this; this.isLeaf = false; this.freq = this.leftChild.Frequency + this.rightChild.Frequency; this.depth = depth; this.leftChild.depth = this.depth - 2; this.rightChild.depth = this.depth - 1; }
private void updateDepths(HuffmanNode root) { Queue <HuffmanNode> queue = new Queue <HuffmanNode>(); int depthIndex = 256; queue.Enqueue(root); HuffmanNode currNode; while (queue.Count > 0) { currNode = queue.Dequeue(); currNode.Depth = depthIndex; depthIndex--; if (currNode.LeftChild != null && currNode.RightChild != null) { queue.Enqueue(currNode.RightChild); queue.Enqueue(currNode.LeftChild); } } updateFreqs(root); }
private void CreateDynamicHuffmanTree(string text) { graph = new Microsoft.Msagl.Drawing.Graph("Huffman Tree"); foreach (char c in text) { if (!firstReadOfChar.Contains(c)) { encodedText += nodeZero.getBit(); firstReadOfChar.Add(c); HuffmanNode leafNode = new HuffmanNode(1, c, 0); dynamicMap[c] = leafNode; HuffmanNode newNode = new HuffmanNode(nodeZero, leafNode, nodeZero.Depth); EncodeNodeList.Add(leafNode.Depth, leafNode); EncodeNodeList[newNode.Depth] = newNode; EncodeNodeList.Add(nodeZero.Depth, nodeZero); updateGraph(EncodeNodeList); } else { encodedText += dynamicMap[c].getBit(); dynamicMap[c].Frequency++; updateGraph(EncodeNodeList); } } textBox5.Text = encodedText; double compressionRatio = 100.0 - Math.Floor((double)encodedText.Length / (double)(EncodeNodeList[256].Frequency * 8) * 100 * 100) / 100; this.label6.Text = "Compression Rate: " + compressionRatio.ToString() + "%"; Dtable.Rows.Clear(); createDynamicGraph(EncodeNodeList[256]); viewer2.Graph = graph; dataGridView2.Sort(dataGridView2.Columns[3], ListSortDirection.Ascending); dataGridView2.Update(); viewer2.Update(); }
private void TraverseInOrder(HuffmanNode node) { if (node.LeftNode != null) { HuffmanCodeBuilder.Append('0'); TraverseInOrder(node.LeftNode); } if (node.Symbol != HuffmanNode.DEFAULT_SYMBOL) { Leafs.Add(node.Symbol, HuffmanCodeBuilder.ToString()); } if (node.RightNode != null) { HuffmanCodeBuilder.Append('1'); TraverseInOrder(node.RightNode); } if (node != GetRoot()) { HuffmanCodeBuilder.Remove(HuffmanCodeBuilder.Length - 1, 1); } }
public void Enqueue(HuffmanNode n) { nodes.Add(n); heapSize++; BuildHeap(heapSize); }
static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); var _config = builder.Build(); while (true) { string userInput = ""; Console.WriteLine("Enter 1 for compression\nEnter 2 for Decompression\nEnter 'e' to exit"); userInput = Console.ReadLine(); if (!userInput.Equals("1") && !userInput.Equals("2") && !userInput.Equals("e")) { continue; } if (userInput.Equals("e")) { break; } switch (int.Parse(userInput)) { case 1: { string orderLevel = ""; string filename = ""; FileInfo fileToCompress = null; orderLevel = PromptUser_Parameter(); filename = PromptUser_FileName(); filename = Path.Combine(_config.GetSection("DataFileDir").Value, filename); if (File.Exists(filename)) { fileToCompress = new FileInfo(filename); } string _fileText = FileReader.GetCharactersFromFile(fileToCompress.FullName); Dictionary <string, Dictionary <char, int> > contexts = new Dictionary <string, Dictionary <char, int> >(); contexts = FileReader.ConstructFrequencyTable(_fileText, int.Parse(orderLevel)); Dictionary <string, PQueue <HuffmanNode> > q = new Dictionary <string, PQueue <HuffmanNode> >(); Utils.FillContextsPriorityQueues(ref q, contexts); HuffmanNode root = null; Dictionary <string, Dictionary <char, string> > huffmanCodes = new Dictionary <string, Dictionary <char, string> >(); Dictionary <string, HuffmanNode> contextTrees = Utils.CreateHuffmanTreesForContexts(q); Utils.GenerateHuffmanCodeTablesForContexts(ref huffmanCodes, contextTrees); string resultsPath = Path.Combine(_config.GetSection("DataFileDir").Value, fileToCompress.FullName + ".ggjs"); Utils.Compress(huffmanCodes, contexts, int.Parse(orderLevel), _fileText, resultsPath); } break; case 2: { string filename = ""; FileInfo fileToDecompress = null; filename = PromptUser_FileName(); filename = Path.Combine(_config.GetSection("DataFileDir").Value, filename); if (File.Exists(filename)) { fileToDecompress = new FileInfo(filename); } Utils.Decompress(fileToDecompress); var files = Directory.GetFiles(_config.GetSection("OutputDirectory").Value); var results = new FileInfo(files[0]); Console.WriteLine("Reuslts File: {0}; Size: {1}", results.Name, results.Length); } break; default: { } break; } } }
public void Insert(HuffmanNode node) { PriorityQueue.Add(node); }
public static void swap(HuffmanNode node1, HuffmanNode node2) { //Console.WriteLine("TriedToSwap"); HuffmanNode leftParent = node1.parentNode; HuffmanNode rightParent = node2.parentNode; if (leftParent == node2 || rightParent == node1) { return; } if (leftParent == rightParent) { if (leftParent.leftChild == node1) { leftParent.leftChild = node2; leftParent.rightChild = node1; } else { leftParent.leftChild = node1; leftParent.rightChild = node2; } return; } int temp = node1.depth; node1.depth = node2.depth; node2.depth = temp; if (leftParent == null) { node2.unAssignParent(); } else if (leftParent.leftChild == node1) { leftParent.leftChild = node2; } else if (leftParent.rightChild == node1) { leftParent.rightChild = node2; } if (leftParent != null) { node2.parentNode = leftParent; } if (rightParent == null) { node1.unAssignParent(); } else if (rightParent.leftChild == node2) { rightParent.leftChild = node1; } else if (rightParent.rightChild == node2) { rightParent.rightChild = node1; } node1.parentNode = rightParent; }
private void unAssignParent() { this.parentNode = null; }
public void Compress(string text) { HuffmanNode node = new HuffmanNode(); List <HuffmanNode> nodeList = node.GetList(text); PriorityQueue <string> PQ = new PriorityQueue <string>(); foreach (HuffmanNode n in nodeList) { PQ.Enqueue(n); } int size = PQ.Size; PQ.DisplayTree(); PQ.Display(); while (size >= 1) { HuffmanNode hNode = new HuffmanNode(); HuffmanNode a = new HuffmanNode(); HuffmanNode b = new HuffmanNode(); if (!PQ.isEmpty()) { a = PQ.Dequeue(); } if (!PQ.isEmpty()) { b = PQ.Dequeue(); } bool isA = true; bool isB = true; foreach (HuffmanNode n in nList) { if (n == a) { isA = false; } if (n == b) { isB = false; } } if (isA && a.Symbol != null) { nList.Add(a); } if (isB && b.Symbol != null) { nList.Add(b); } hNode.Left = a; hNode.Right = b; hNode.Frequency = a.Frequency + b.Frequency; hNode.Symbol = a.Symbol + b.Symbol; hNode.Left.Parent = hNode.Left.Parent = hNode; PQ.Enqueue(hNode); size--; } AssignCode(nList[0].Code, nList[0]); }
public HuffmanNode(string symbol, int frequency, string code, HuffmanNode left, HuffmanNode right, HuffmanNode parent) { this.Symbol = symbol; this.Frequency = frequency; this.Code = code; this.Left = left; this.Right = right; this.Parent = parent; }