예제 #1
0
        private void createDynamicGraph(HuffmanNode node)
        {
            map = new Dictionary <char, string>();

            graph.AddNode(getNodeName2(node));
            if (node.IsLeaf)
            {
                graph.FindNode(getNodeName2(node)).Attr.Color = new Microsoft.Msagl.Drawing.Color(255, 0, 0);
                DataRow row = Dtable2.NewRow();
                row[0] = "'" + node.Char.ToString() + "'";
                row[1] = node.Frequency;
                row[2] = node.getBit();
                row[3] = node.getBit().Length.ToString();
                Dtable2.Rows.Add(row);
                map.Add(node.Char, node.getBit());
            }
            if (node.RightChild != null)
            {
                graph.AddEdge(getNodeName2(node), getNodeName2(node.RightChild));
                createDynamicGraph(node.RightChild);
            }
            if (node.LeftChild != null)
            {
                graph.AddEdge(getNodeName2(node), getNodeName2(node.LeftChild));
                createDynamicGraph(node.LeftChild);
            }
        }
예제 #2
0
        private string getNodeName(HuffmanNode node)
        {
            string str;

            if (node.IsLeaf)
            {
                str = " '" + node.ToString() + "'(" + node.Frequency.ToString() + ")\n" + node.getBit();
            }
            else
            {
                str = node.ToString() + "\n" + node.getBit();
            }

            return(str);
        }
예제 #3
0
        private string getNodeName2(HuffmanNode node)
        {
            string str;

            if (node.IsLeaf)
            {
                str = " '" + node.ToString() + "'(" + node.Depth.ToString() + "," + node.Frequency + ")\n" + node.getBit();
            }
            else if (node.LeftChild == null && node.RightChild == null)
            {
                return("ZeroNode(" + node.Depth + ")");
            }
            else
            {
                str = node.ToString() + "(" + node.Depth + ")" + "\n" + node.getBit();
            }

            return(str);
        }
예제 #4
0
        private void CreateDynamicHuffmanTree(string text)
        {
            graph = new Microsoft.Msagl.Drawing.Graph("Huffman Tree");

            foreach (char c in text)
            {
                if (!firstReadOfChar.Contains(c))
                {
                    encodedText += nodeZero.getBit();
                    firstReadOfChar.Add(c);
                    HuffmanNode leafNode = new HuffmanNode(1, c, 0);
                    dynamicMap[c] = leafNode;
                    HuffmanNode newNode = new HuffmanNode(nodeZero, leafNode, nodeZero.Depth);
                    EncodeNodeList.Add(leafNode.Depth, leafNode);
                    EncodeNodeList[newNode.Depth] = newNode;
                    EncodeNodeList.Add(nodeZero.Depth, nodeZero);

                    updateGraph(EncodeNodeList);
                }
                else
                {
                    encodedText += dynamicMap[c].getBit();
                    dynamicMap[c].Frequency++;
                    updateGraph(EncodeNodeList);
                }
            }
            textBox5.Text = encodedText;
            double compressionRatio = 100.0 - Math.Floor((double)encodedText.Length / (double)(EncodeNodeList[256].Frequency * 8) * 100 * 100) / 100;

            this.label6.Text = "Compression Rate: " + compressionRatio.ToString() + "%";
            Dtable.Rows.Clear();
            createDynamicGraph(EncodeNodeList[256]);
            viewer2.Graph = graph;
            dataGridView2.Sort(dataGridView2.Columns[3], ListSortDirection.Ascending);
            dataGridView2.Update();
            viewer2.Update();
        }
예제 #5
0
 public string getBit()
 {
     return(parentNode == null ? "" : (parentNode.leftChild == this ? parentNode.getBit() + "0" : parentNode.getBit() + "1"));
 }