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());
        }
Beispiel #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            string          s    = textBox1.Text;
            int             n    = s.Length;
            List <CharFreq> list = new List <CharFreq>();

            textBox2.Text = string.Empty;

            for (int i = 0; i < n; i++)
            {
                bool     found = false;
                char     c     = s[i];
                CharFreq cf    = new CharFreq();

                for (int j = 0; !found && j < list.Count; j++)
                {
                    if (c == list[j].ch)
                    {
                        found   = true;
                        cf.ch   = c;
                        cf.freq = 1 + list[j].freq;
                        list.RemoveAt(j);
                        list.Add(cf);
                    }
                }

                if (!found)
                {
                    cf.ch   = c;
                    cf.freq = 1;
                    list.Add(cf);
                }
            }

            HuffmanTree ht = new HuffmanTree();
            BinaryTreeNode <CharFreq> root = ht.Build(list, list.Count);

            InorderTraversal(root);
            textBox2.Text += "\r\nKarakterler = " + n.ToString() + "\r\n";
            textBox2.Text += "Düğümler = " + leafNodes.ToString() + "\r\n";
            textBox2.Text += "Sıkıştırma oranı %" +
                             (100.0 - 100.0 * ((double)leafNodes) / n).ToString("F2") + "\r\n";
        }
Beispiel #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++;
                    textBox2.Text += "'" + new string(cf.ch, 1) + "' ";
                    textBox2.Text += node.Value.freq.ToString() + "\r\n";
                }

                InorderTraversal(node.Right);
            }
        }