コード例 #1
0
ファイル: HTree.cs プロジェクト: Benji1685/HuffmanEncoding
        //Decoding the file
        public void Decode(StreamReader input, StreamWriter output)
        {
            HNode CurrentNode = this.RootNode;


            while (!input.EndOfStream)
            {
                bool b = input.Read() == '1';
                CurrentNode = b ?
                              CurrentNode.LeftNode : CurrentNode.RightNode;

                if (!CurrentNode.isBranch())
                {
                    output.Write(CurrentNode.Character);
                    CurrentNode = this.RootNode;
                }
            }
        }
コード例 #2
0
ファイル: HTree.cs プロジェクト: Benji1685/HuffmanEncoding
        public void BuildHTree()
        {
            // Counts the Frequencies


            //creating the Array of Huffman Leafs
            for (int i = 0; i < 256; i++)
            {
                if (Frequencies[i] != 0)
                {
                    HuffmanNodes.Add(new HNode()
                    {
                        Character     = (char)i,
                        CharFrequency = Frequencies[i]
                    });
                }
            }
            //Actally building the tree and taking the smallest frequencies and adding them together
            //then back into the tree
            while (HuffmanNodes.Count > 1)
            {
                HuffmanNodes.Sort();

                HNode first  = HuffmanNodes[0];
                HNode second = HuffmanNodes[1];

                HuffmanNodes.Remove(first);
                HuffmanNodes.Remove(second);

                HNode Branch = new HNode();
                Branch.Character     = '\0';
                Branch.CharFrequency = first.CharFrequency + second.CharFrequency;
                Branch.LeftNode      = first;
                Branch.RightNode     = second;
                HuffmanNodes.Add(Branch);
            }
            this.RootNode = HuffmanNodes[0];
        }