Пример #1
0
 private void createStaticGraph(HuffmanNode node)
 {
     graph.AddNode(getNodeName(node));
     if (node.IsLeaf)
     {
         graph.FindNode(getNodeName(node)).Attr.Color = new Microsoft.Msagl.Drawing.Color(255, 0, 0);
         DataRow row = Dtable.NewRow();
         row[0] = "'" + node.Char.ToString() + "'";
         row[1] = node.Frequency;
         row[2] = node.getBit();
         row[3] = node.getBit().Length.ToString();
         Dtable.Rows.Add(row);
         map.Add(node.Char, node.getBit());
     }
     if (node.RightChild != null)
     {
         graph.AddEdge(getNodeName(node), getNodeName(node.RightChild));
         createStaticGraph(node.RightChild);
     }
     if (node.LeftChild != null)
     {
         graph.AddEdge(getNodeName(node), getNodeName(node.LeftChild));
         createStaticGraph(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);
        }
        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() + 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);
                }
            }

            textBox1.Text = encodedText;
            double compressionRatio = 100.0 - Math.Floor(((double)encodedText.Length) / (double)(EncodeNodeList[256].Frequency * 8) * 100 * 100) / 100;

            this.label1.Text = "Compression Ratio: " + compressionRatio.ToString() + "%";


            if (!checkBox3.Checked)
            {
                Dtable.Rows.Clear();
                createDynamicGraph(EncodeNodeList[256]);
                viewer.Graph = graph;
                dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Ascending);
                dataGridView1.Update();
                viewer.Update();
            }
        }
        private string getNodeName(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);
        }
Пример #5
0
 public string getBit()
 {
     return(parentNode == null ? "" : (parentNode.leftChild == this ? parentNode.getBit() + "0" : parentNode.getBit() + "1"));
 }