/// <summary>
        /// Create huffman tree
        /// </summary>
        /// <param name="path">Path of the document.</param>
        private void HuffmanTree(string path)
        {
            using (var File = new FileStream(path, FileMode.Open)) {
                var text = new byte[1000000];
                using var readerText = new BinaryReader(File, Encoding.Default, true);
                data = readerText.BaseStream.Length;

                while (readerText.BaseStream.Position != readerText.BaseStream.Length)
                {
                    text = readerText.ReadBytes(1000000);
                    foreach (var item in text)
                    {
                        if (numberChar.Keys.Contains(item))
                        {
                            numberChar[(item)] += 1;
                        }
                        else
                        {
                            numberChar.Add(item, 1);
                        }
                    }
                }
                List <HuffmanNode> nodesHuffman = new List <HuffmanNode>();
                foreach (KeyValuePair <byte, int> chars in numberChar)
                {
                    nodesHuffman.Add(new HuffmanNode(chars.Key, Convert.ToDouble(chars.Value) / data));
                }

                while (nodesHuffman.Count > 1)
                {
                    if (nodesHuffman.Count == 1)
                    {
                        break;
                    }
                    else
                    {
                        nodesHuffman = nodesHuffman.OrderBy(x => x.frequency).ToList();
                        HuffmanNode parent = createParent(nodesHuffman[1], nodesHuffman[0]);
                        nodesHuffman.RemoveAt(0);
                        nodesHuffman.RemoveAt(0);
                        nodesHuffman.Add(parent);
                    }
                }
                root = nodesHuffman[0];
            }
        }
 /// <summary>
 /// Build huffman code
 /// </summary>
 /// <param name="node">Root node of huffman's tree</param>
 /// <param name="route">Initial route</param>
 private static void buildHuffmanCode(HuffmanNode node, string route)
 {
     if (node.isLeafNode())
     {
         huffmanCode.Add(node.symbol, route);
     }
     else
     {
         if (node.leftChild != null)
         {
             buildHuffmanCode(node.leftChild, route + "0");
         }
         if (node.rightChild != null)
         {
             buildHuffmanCode(node.rightChild, route + "1");
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Create Huffman Tree
        /// </summary>
        /// <param name="path"></param>
        private void HuffmanTree(string path)
        {
            using var reader = new StreamReader(path, Encoding.Default, true);
            var pos = reader.ReadLine().Length;

            reader.Close();

            using (var file = new FileStream(path, FileMode.Open)) {
                file.Position = pos + 1;
                int    divisor = 0;
                var    fillingB = new byte[1000000];
                string newData = "", frequency = "", dataM = "";
                int    last = 0;
                byte   bit  = new byte();
                using (var readFile = new BinaryReader(file, Encoding.Default, true)) {
                    while (readFile.BaseStream.Position != readFile.BaseStream.Length)
                    {
                        fillingB = readFile.ReadBytes(1000000);
                        foreach (var item in fillingB)
                        {
                            if (divisor == 0)
                            {
                                if (Convert.ToChar(item) == '|' || Convert.ToChar(item) == 'æ' || Convert.ToChar(item) == 'þ')
                                {
                                    divisor = 1;
                                    if (Convert.ToChar(item) == '|')
                                    {
                                        separator = '|';
                                    }
                                    else if (Convert.ToChar(item) == 'æ')
                                    {
                                        separator = 'æ';
                                    }
                                    else
                                    {
                                        separator = 'þ';
                                    }
                                }
                                else
                                {
                                    newData += Convert.ToChar(item).ToString();
                                }
                            }
                            else if (divisor == 2)
                            {
                                break;
                            }
                            else
                            {
                                if (last == 1 && Convert.ToChar(item) == separator)
                                {
                                    last    = 2;
                                    divisor = 2;
                                }
                                else
                                {
                                    last = 0;
                                }

                                if (dataM == "")
                                {
                                    dataM = Convert.ToChar(item).ToString();
                                    bit   = item;
                                }
                                else if (Convert.ToChar(item) == separator && last == 0)
                                {
                                    numberChar.Add(bit, Convert.ToInt32(frequency));
                                    dataM     = "";
                                    frequency = "";
                                    last      = 1;
                                }
                                else
                                {
                                    frequency += Convert.ToChar(item).ToString();
                                }
                            }
                        }
                    }
                }
                data = Convert.ToDouble(newData);
            }
            List <HuffmanNode> nodesHuffman = new List <HuffmanNode>();

            foreach (KeyValuePair <byte, int> chars in numberChar)
            {
                nodesHuffman.Add(new HuffmanNode(chars.Key, Convert.ToDouble(chars.Value) / data));
            }
            nodesHuffman = nodesHuffman.OrderBy(x => x.frequency).ToList();
            while (nodesHuffman.Count > 1)
            {
                nodesHuffman = nodesHuffman.OrderBy(x => x.frequency).ToList();
                HuffmanNode parent = createParent(nodesHuffman[1], nodesHuffman[0]);
                nodesHuffman.RemoveAt(0);
                nodesHuffman.RemoveAt(0);
                nodesHuffman.Add(parent);
            }
            root = nodesHuffman[0];
        }