jkjsha ahsa

jkjsha ahsa

  • NA
  • 24
  • 26.1k

Problem with Huffman code

Dec 19 2009 2:17 PM
Huffman code Program
facing a trouble with this code

i found it in book called data structures and algorithm in c#
i wanna to make program to compress text with Huffman code algorithm  
and there is many errors in the code at the main of project and the methods

first class
 public class Node {
HuffmanTree data;
Node link;
public Node(HuffmanTreenewData) {
data=newData;
}
}
2nd class
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class TreeList
{
public int count = 0;
Node first;
public void AddLetter(
)
{
HuffmanTree hTemp = new HuffmanTree(letter);
Node eTemp = new Node(hTemp);
if (first == null)
first = eTemp;
else
{
eTemp.link = first;
first = eTemp;
}
count++;
}
public void SortTree()
{
TreeList otherList = new TreeList();
HuffmanTree aTemp;
while (!(this.first == null))
{
aTemp = this.RemoveTree();
otherList.InsertTree(aTemp);
}
this.first = otherList.first;
}
public void MergeTree()
{
if (!(first == null))
if (!(first.link == null))
{
HuffmanTree aTemp = RemoveTree();
HuffmanTree bTemp = RemoveTree();
HuffmanTree sumTemp = new HuffmanTree(null);
sumTemp.SetLeftChild(aTemp);
sumTemp.SetRightChild(bTemp);
sumTemp.SetFreq(aTemp.GetFreq() +
bTemp.GetFreq());
InsertTree(sumTemp);
}
}
public HuffmanTree RemoveTree()
{
if (!(first == null))
{
HuffmanTree hTemp;
hTemp = first.data;
first = first.link;
count--;
return hTemp;
}
return null;
}
public void InsertTree(HuffmanTree hTemp)
{
Node eTemp = new Node(hTemp);
if (first == null)
first = eTemp;
else
{
Node p = first;
while (!(p.link == null))
{
if ((p.data.GetFreq() <= hTemp.GetFreq()) && (p.link.data.GetFreq() >= hTemp.GetFreq()))
break;
p = p.link;
}
eTemp.link = p.link;
p.link = eTemp;
}
count++;
}
public int Length()
{
return count;
}
}
}

3rd class
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class HuffmanTree
{
private HuffmanTree leftChild;
private HuffmanTree rightChild;
private string letter;
private int freq;
public HuffmanTree(string letter)
{
this.letter = letter;
}
public void SetLeftChild(HuffmanTree newChild)
{
leftChild = newChild;
}
public void SetRightChild(HuffmanTree newChild)
{
rightChild = newChild;
}
public void SetLetter(string newLetter)
{
letter = newLetter;
}
public void IncFreq()
{
freq++;
}
public void SetFreq(int newFreq)
{
freq = newFreq;
}
public HuffmanTree GetLeftChild()
{
return leftChild;
}
public HuffmanTree GetRightChild()
{
return rightChild;
}
public int GetFreq()
{
return freq;
}
}
}

the main
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input;
            Console.Write("Enter a string to encode: ");
            input = Console.ReadLine();
            TreeList treeList = new TreeList();
            foreach (int v in input)
            { treeList.AddLetter(input); }
            treeList.SortTree();
            int[] signTable = new int[input.Length];
            int[] keyTable = new int[input.Length];
            while (treeList.length > 1)
                treeList.MergeTree();
            MakeKey(treeList.RemoveTree(), "");
            string newStr = translate(input);
            for (int i = 0; i <= signTable.Length - 1; i++)
                Console.WriteLine(signTable[i] + ": " +
                keyTable[i]);
            Console.WriteLine("The original string is " + input.
            Length * 16 + " bits long.");
            Console.WriteLine("The new string is " + newStr.Length + " bits long.");
            Console.WriteLine("The coded string looks like this:" + newStr);
            translate(input, signTable);

        }
 
        static string translate(string original,int []signTable)
        {
            
            string newStr = "";
            for (int i = 0; i <= original.Length - 1; i++)
                for (int j = 0; j <= signTable.Length - 1; j++)
                    if (original[i] == signTable[j])
                        newStr += keyTable[j];
            return newStr;
        }
        static void MakeKey(HuffmanTree tree, string code)
        {
            int pos = 0;
            if (tree.GetLeftChild == null)
            {
                signTable[pos] = tree.GetSign();
                keyTable[pos] = code;
                pos++;
            }
            else
            {
                MakeKey(tree.GetLeftChild, code + "0");
                MakeKey(tree.GetRightChild, code + "1");
            }
        }
    }
}


Answers (1)