//entering values in map inside nodes and arranging those nodes in Priority queue with smallest frequencies in front 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); }
static void Main(string[] args) { Program myComp = new Program(); //creating instance of the main file //string S; //Enter string to find the huffman code //S = Console.ReadLine(); // Console.WriteLine("String Entered: " + S); Console.WriteLine("Enter File Name: "); var fileName = Console.ReadLine(); Console.WriteLine(fileName); Dictionary <char, int> Dic = new Dictionary <char, int>(); Dic = myComp.frequency(fileName); //frequency calculated myComp.printMap(Dic); //entering data in nodes then storing them in queue PQueue.PriorityQueue pQueue = new PQueue.PriorityQueue(); pQueue = myComp.nodesinQueue(Dic); //creating encooding tree myComp.root = myComp.HuffmanEncoding(pQueue); PQueue.cNode top = myComp.root; //temporary storing the value Console.WriteLine(top.getValue()); myComp.HuffCode(top, ""); myComp.printCodes(myComp.HuffmanCode); //writing tree in file // top = myComp.root; //temporary storing the value //myComp.EncodingTreeWrite(top); //compressing file FileStream fs = File.OpenRead(fileName); myComp.WriteCompressFile(fs); Console.ReadKey(); }
//building the tree 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 = 'a'; pQueue.insertWithPriority(node); pQueue.print(); Console.WriteLine("Inserted"); n = pQueue.count; } return(pQueue.Top()); }