Beispiel #1
0
        //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);
        }
Beispiel #2
0
 //writing tree at the top of decoding file
 void Write_tree(PQueue.cNode r)
 {
     if (r.leftZero == null && r.rightOne == null)
     {
         treeFile.Write("0");
         treeFile.Write(r.value);
     }
     else
     {
         treeFile.Write("1");
         Write_tree(r.leftZero);
         Write_tree(r.rightOne);
     }
 }
Beispiel #3
0
        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();
        }
Beispiel #4
0
        //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());
        }
Beispiel #5
0
        public void printPreorder(PQueue.cNode node)
        {
            if (node == null)
            {
                return;
            }

            /* first print data of node */
            if (node.leftZero == null && node.rightOne == null)
            {
                Console.Write(node.value + " ");
            }

            /* then recur on left sutree */
            printPreorder(node.leftZero);

            /* now recur on right subtree */
            printPreorder(node.rightOne);
        }
Beispiel #6
0
        //decompress the file

        public void decompress(string fileName, FileStream fs2)
        {
            readBitByBit bit = new readBitByBit(fileName);
            FileTreeroot = ReadTreeHeader(bit);
            Console.WriteLine("insode decompress");
            program.printPreorder(FileTreeroot);
            var output = new StreamWriter(fs2);
            int returnbit = -1;
            char leaf = '1'; //checking if we reached the end of file

            // PQueue.cNode top = root;
            PQueue.cNode top = FileTreeroot;

            while (true)  //will run until we found the pseduo_EOF
            {
                if (top.leftZero == null && top.rightOne == null)  //if leaf node is reached
                {
                    leaf = top.value;
                    if (leaf == (char)program.Pseudo_EOF)   //if it is last letter close the file
                    {
                        output.Close();
                        break;
                    }
                    else
                    {
                        output.Write(leaf);  //else write in file
                        top = FileTreeroot;   //again start from root
                    }
                }
                returnbit = bit.bitRead();
                if (returnbit == 0)  //if not leaf keep on reading the file
                {
                    top = top.leftZero;
                }
                else if (returnbit == 1)
                {
                    top = top.rightOne;
                }
            }
            output.Close();
            bit.close();

        }
Beispiel #7
0
        //decompress the file

    



        public PQueue.cNode ReadTreeHeader(readBitByBit bit)
        {
            char c = bit.();
            PQueue.cNode node = new PQueue.cNode();

            if (c == '1')
            {
                Console.WriteLine("inside 1 : ");
                node = new PQueue.cNode();
                node.value = bit.ByteRead();
                Console.WriteLine("inside 1 value: " + node.value);
                return node;
            }

            else
            {
                Console.WriteLine("inside 0");
                PQueue.cNode leftChild = ReadTreeHeader(bit);
                PQueue.cNode rightChild = ReadTreeHeader(bit);
                node = new PQueue.cNode('0', leftChild, rightChild);
                return node;
            }
        }
Beispiel #8
0
        void HuffCode(PQueue.cNode root, string str)
        {
            if (root == null)   //base case
            {
                return;
            }

            if (root.leftZero == null && root.rightOne == null)    //if we reached at leaf node
            {
                try
                {
                    HuffmanCode.Add(root.value, str);     //if it is not already in the Dictionary
                }
                catch
                {
                    HuffmanCode[root.value] = str;   //if it is in the dictionary just edit 2nd value
                }
            }
            else
            {
                HuffCode(root.leftZero, str + "0");   //concatination to form the code
                HuffCode(root.rightOne, str + "1");
            }
        }
Beispiel #9
0
        //Writng decoding tree
        void EncodingTreeWrite(PQueue.cNode r)
        {
            string fileName = "tree.cmu";

            try
            {
                // Check if file already exists. If yes, delete it.

                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                // Create a new file
                treeFile = new StreamWriter(fileName, true, Encoding.ASCII);
                Write_tree(r);
                treeFile.Write(254);
                treeFile.Close();
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.ToString());
            }
        }