Example #1
0
        public BinaryTreeNode <CharFreq> Build(List <CharFreq> charFreq, int n)
        {
            PriorityQueue Q = new PriorityQueue();

            for (int i = 0; i < n; i++)
            {
                BinaryTreeNode <CharFreq> z = new BinaryTreeNode <CharFreq>(charFreq[i]);

                Q.insert(z);
            }

            Q.buildHeap();

            for (int i = 0; i < n - 1; i++)
            {
                BinaryTreeNode <CharFreq> x = Q.extractMin();
                BinaryTreeNode <CharFreq> y = Q.extractMin();
                CharFreq chFreq             = new CharFreq();

                chFreq.ch   = (char)((int)x.Value.ch + (int)y.Value.ch);
                chFreq.freq = x.Value.freq + y.Value.freq;

                BinaryTreeNode <CharFreq> z = new BinaryTreeNode <CharFreq>(chFreq);

                z.Left  = x;
                z.Right = y;
                Q.insert(z);
            }

            return(Q.extractMin());
        }
Example #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                saveFileDialog1.Filter = "Huffman Files (*.huf)|*.huf";
                openFileDialog1.Filter = "Text Files (*.txt)|*.txt";

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    int[]        freq = new int[128];
                    StreamReader sr   = new StreamReader(openFileDialog1.FileName);

                    iCount = 0;

                    while (!sr.EndOfStream)
                    {
                        int ch = sr.Read();

                        if (ch >= 0 && ch <= 127)
                        {
                            freq[ch]++;
                        }
                        else
                        {
                            MessageBox.Show("File is not ASCII", "Warning",
                                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }

                        iCount++;
                    }

                    sr.Close();
                    charFreq = new List <CharFreq>(128);

                    for (int i = 0; i < 128; i++)
                    {
                        if (freq[i] != 0)
                        {
                            CharFreq cf = new CharFreq();

                            cf.ch   = (char)i;
                            cf.freq = freq[i];
                            charFreq.Add(cf);
                        }
                    }

                    Compress();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Warning",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Example #3
0
        private void InorderTraversal(BinaryTreeNode <CharFreq> node)
        {
            if (node != null)
            {
                InorderTraversal(node.Left);

                CharFreq cf  = node.Value;
                int      ord = (int)cf.ch;

                if (node.Left == null && node.Right == null)
                {
                    leafNodes++;
                    textBox1.Text += ord.ToString("D3") + "\t";
                    textBox1.Text += cf.freq.ToString() + "\r\n";
                }

                InorderTraversal(node.Right);
            }
        }