コード例 #1
0
 public char_freq(char_freq leftnode, char_freq rightnode, List <char_freq> frequency_list)
 {
     this.left_node_index  = frequency_list.FindIndex(f => f == leftnode);
     this.right_node_index = frequency_list.FindIndex(f => f == rightnode);
     frequency_list[left_node_index].parent_node_index  = frequency_list.FindIndex(f => f == this);
     frequency_list[right_node_index].parent_node_index = frequency_list.FindIndex(f => f == this);
     this.count = leftnode.Count + rightnode.Count;
 }
コード例 #2
0
        /****This method will be called by compress_with_Huffman to create the huffman tree with the given character frequencies****/
        private char_freq create_huffman_tree()
        {
            frequency_list = frequency_list.OrderBy(f => f.Count).ToList();//list the frequencies by frequency
            char_freq root        = new char_freq();
            int       index       = 0;
            int       Num_entries = frequency_list.Count();

            //keep on creating a parent node with the two lowest frequency entries that we haven't visited.
            while (index + 1 < Num_entries)
            {
                char_freq newNode = new char_freq(frequency_list[index], frequency_list[index + 1], frequency_list);
                frequency_list.Add(newNode);//add to correct place
                root        = newNode;
                index      += 2;
                Num_entries = frequency_list.Count();
            }
            return(root);
        }
コード例 #3
0
 /****This method will be called by compress_with_Huffman to create ****/
 private void create_char_freq(string fileString)
 {
     foreach (char c in fileString)
     {
         bool found = false;                         //saves whether we've seen this character
         foreach (char_freq entry in frequency_list) //look through the list - if char found, add 1 to count.
         {
             if (entry.ID == c)
             {
                 found = true;
                 entry.Count++;
             }
         }
         if (!found)//If not, create new entry with that char and count of 1
         {
             char_freq newEntry = new char_freq(c);
             frequency_list.Add(newEntry);
         }
     }
 }
コード例 #4
0
 private List <char_freq> compress(string coded_string, char last_char, List <char_freq> counted_char)
 {
     if (0 == coded_string.Length)
     {
         return(counted_char);
     }
     else
     {
         if (last_char == coded_string[0])
         {
             counted_char[counted_char.Count - 1].Count += 1;
         }
         else
         {
             char_freq newEntry = new char_freq(coded_string[0], 1);
             counted_char.Add(newEntry);
         }
         return(compress(coded_string.Remove(0, 1), last_char, counted_char));
     }
 }
コード例 #5
0
 private void create_char_freq(string fileString)
 {
     foreach (char c in fileString)
     {
         bool found = false;
         foreach (char_freq entry in frequency_list)
         {
             if (entry.ID == c)
             {
                 found = true;
                 entry.Count++;
             }
         }
         if (!found)
         {
             char_freq newEntry = new char_freq(c);
             frequency_list.Add(newEntry);
         }
     }
 }
コード例 #6
0
        private void Uncompress_with_Huffman(string InputFilename, string InputTreeFilename, string OutputFilename)
        {
            byte[]   CompressedFileByte = File.ReadAllBytes(InputFilename);
            string[] Huffman_tree       = File.ReadAllLines(InputTreeFilename);

            frequency_list = new List <char_freq>();
            foreach (string Huffman_node in Huffman_tree)
            {
                string[]  node_info = Huffman_node.Split(',');
                char_freq newNode   = new char_freq();//add correctly
                frequency_list.Add(newNode);
            }
            int           index = 0;
            StringBuilder CompressedByteString = new StringBuilder();

            foreach (byte b in CompressedFileByte)
            {
                CompressedByteString.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
            }
            StringBuilder Uncompressed_fileString = new StringBuilder();
            char_freq     traversal =;//find root - make variable so that we know the type of node it is - add node, char node, or root node

            while (index < CompressedByteString.Length)
            {
                if ((traversal.LeftNodeIndex ==) && (traversal.RightNodeIndex ==))  //if this is a edge node
                {
                    Uncompressed_fileString.Append(traversal.ID);
                }
                if (CompressedByteString[index] == '1')
                {
                    traversal = frequency_list[traversal.RightNodeIndex];
                    index++;
                }
                else
                {
                    traversal = frequency_list[traversal.LeftNodeIndex];
                    index++;
                }
            }
        }
コード例 #7
0
 private void compress(string fileString, char_freq root)
 {
     compressed_binary_string = new StringBuilder();
     foreach (char c in fileString)
     {
         char_freq node       = frequency_list.Find(f => f.ID == c);
         string    tempString = "";
         while (node != root)
         {
             char_freq tempNode = frequency_list[node.ParentNodeIndex];
             if (frequency_list[tempNode.LeftNodeIndex] == node)
             {
                 tempString = '1' + tempString;
             }
             if (frequency_list[tempNode.RightNodeIndex] == node)
             {
                 tempString = '0' + tempString;
             }
             node = tempNode;
         }
         compressed_binary_string.Append(tempString);
     }
 }
コード例 #8
0
        /****This method will be called by the form to compress the input file into huffman compressed output file****/
        public void compress_with_Huffman(string InputFilename, string OutputFilename)
        {
            //we will first count the number of times each character appears in this file.
            frequency_list = new List <char_freq>();
            string fileString = File.ReadAllText(InputFilename);

            create_char_freq(fileString);

            char_freq root = create_huffman_tree();         //create a huffman tree based off of the frequency of each char in the file

            compress(fileString, root);                     //convert the file into binary using the huffman tree
            StringBuilder String_rep_tree = convert_tree(); //convert the tree into a savable format
            //write the converted binary and huffman tree to appropriate file
            int numBytes = compressed_binary_string.ToString().Length / 8;

            compressed_fileByte = new byte[numBytes];
            for (int i = 0; i < numBytes; ++i)
            {
                compressed_fileByte[i] = Convert.ToByte(compressed_binary_string.ToString().Substring(8 * i, 8), 2);
            }
            File.WriteAllBytes(OutputFilename, compressed_fileByte);
            File.WriteAllText("HuffmanTreeData_" + OutputFilename, String_rep_tree.ToString());
        }