private void InitializeCodeTable(int codeTableSize)
        {
            CodeTable.Clear();
            for (var i = 0; i < codeTableSize; i++)
            {
                CodeTable.Add(i, new List <int>()
                {
                    i
                });
            }

            CodeTable[codeTableSize] = new List <int>()
            {
                codeTableSize
            };
            CodeTable[codeTableSize + 1] = new List <int>()
            {
                codeTableSize + 1
            };
        }
Beispiel #2
0
        // tree traversal
        public void StoreCodes(HuffmanTreeNode root, string s)
        {
            // base case; if the left and right are null then its a leaf node and store the code s generated by traversing the tree.
            if (root.Left == null && root.Right == null && Char.IsLetter(root.Char))
            {
                // codetable char
                if (!CodeTable.ContainsKey(root.Char))
                {
                    CodeTable.Add(root.Char, s);
                }
                CodeTable[root.Char] = s;
                Console.WriteLine("\t\t" + root.Char + ":" + s);
                return;
            }

            // if we go to left then add "0" to the code., if we go to the right add "1" to the code.
            // recursive calls for left and right sub-tree of the generated tree.
            StoreCodes(root.Left, s + "0");
            StoreCodes(root.Right, s + "1");
        }