//entering values in map inside nodes and arranging those nodes in Priority queue with smallest frequencies in front public PQueue.PriorityQueue nodesinQueue(Dictionary <char, int> Dic) { PQueue.cNode node = new PQueue.cNode(); //node created PQueue.PriorityQueue pQueue = new PQueue.PriorityQueue(); //queue created foreach (KeyValuePair <char, int> kvp in Dic) //reading from frequency dictionary { node.value = kvp.Key; node.frequency = kvp.Value; //setting the values of node pQueue.insertWithPriority(node); //entering the node node = new PQueue.cNode(); } return(pQueue); }
//calculating codes making tree public PQueue.cNode HuffmanEncoding(PQueue.PriorityQueue pQueue) { int n = pQueue.count; while (n != 1) { PQueue.cNode node = new PQueue.cNode(); node.leftZero = pQueue.remove(); node.rightOne = pQueue.remove(); node.frequency = node.leftZero.frequency + node.rightOne.frequency; node.value = '~'; //just adding a temp value to middle nodes pQueue.insertWithPriority(node); n = pQueue.count; } return(pQueue.Top()); }
private void Compression_Click(object sender, RoutedEventArgs e) { string filename = fileName; //myObj.comleteFlag = 0; if (fileExt == ".pdf" || fileExt == ".docx") { if (fileExt == ".docx") { try { // Open a doc file. Application app = new Application(); Microsoft.Office.Interop.Word.Document document = app.Documents.Open(fileName); // Select all words from word file int total = document.Words.Count; for (int i = 1; i <= total; i++) { // Reading from word document into a string content += document.Words[i].Text; } // Close word. Console.WriteLine(content); app.Quit(); } catch { MessageBox.Show("File is used by another Process"); throw new ArgumentException("File is used by another Process."); } } if (fileExt == ".pdf") { try { StringBuilder fileContent = new StringBuilder(); using (PdfReader pdfReader = new PdfReader(filename)) { for (int k = 1; k <= pdfReader.NumberOfPages; k++) { fileContent.Append(PdfTextExtractor.GetTextFromPage(pdfReader, k)); } } content = fileContent.ToString(); } catch { MessageBox.Show("File is used by another Process"); throw new ArgumentException("File is used by another Process."); } } //string content; Dictionary <char, int> Dic = new Dictionary <char, int>(); Dic = myObj.frequencyPDF(content); // myObj.printMap(Dic); //getting frequency //entering data in nodes then storing them in queue PQueue.PriorityQueue pQueue = new PQueue.PriorityQueue(); pQueue = myObj.nodesinQueue(Dic); // pQueue.print(); //creating encooding tree myObj.root = myObj.HuffmanEncoding(pQueue); PQueue.cNode top = myObj.root; //temporary storing the value // Console.WriteLine(top.getValue()); myObj.HuffCode(top, ""); // myObj.printCodes(myObj.HuffmanCode); } else { FileStream fs; try { fs = File.OpenRead(fileName); } catch { MessageBox.Show("No File has been Uploaded"); throw new ArgumentException("A file should be uploaded."); } //String fs = File.ReadAllText(fileName); Dictionary <char, int> Dic = new Dictionary <char, int>(); //Dic = myObj.frequency(fs); Dic = myObj.frequency(fs); // myObj.printMap(Dic); //getting frequency //entering data in nodes then storing them in queue PQueue.PriorityQueue pQueue = new PQueue.PriorityQueue(); pQueue = myObj.nodesinQueue(Dic); // pQueue.print(); //creating encooding tree myObj.root = myObj.HuffmanEncoding(pQueue); PQueue.cNode top = myObj.root; //temporary storing the value // Console.WriteLine(top.getValue()); myObj.HuffCode(top, ""); // myObj.printCodes(myObj.HuffmanCode); top = myObj.root; //temporary storing the value } MessageBox.Show("Compress Done Save File."); }