コード例 #1
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);
        }
コード例 #2
0
        private void updateFreqs(HuffmanNode root)
        {
            if (root.LeftChild != null)
            {
                updateFreqs(root.LeftChild);
            }
            if (root.RightChild != null)
            {
                updateFreqs(root.RightChild);
            }

            if (root.LeftChild == null && root.RightChild == null)
            {
                return;
            }
            root.Frequency = root.LeftChild.Frequency + root.RightChild.Frequency;
        }
コード例 #3
0
        private void CreateStaticHuffmanTree(object sender, EventArgs e)
        {
            graph = new Microsoft.Msagl.Drawing.Graph("Huffman Tree");
            Dtable.Clear();
            map = new Dictionary <char, string>();
            String text = textBox3.Text;

            textBox2.Text = "";
            if (text.Length < 1)
            {
                UpdateGraph(graph);
                textBox1.Text = "";
                encodedText   = "";
                label1.Text   = "Compression Ratio:";
                MessageBox.Show("Can't encode empty input ", "Encoding Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var count2 = text.ToLower().Select(x => x).GroupBy(c => c).Select(chunk => new { Letter = chunk.Key, Count = chunk.Count() }).OrderByDescending(item => item.Count).ThenBy(item => item.Letter); // Language Integrated Query

            List <HuffmanNode> list = new List <HuffmanNode>();

            foreach (var c1 in count2)
            {
                //Console.WriteLine("Alphabet :" + c1.Letter + ", Count :" + c1.Count);
                list.Add(new HuffmanNode(c1.Count, c1.Letter));
            }
            list.Sort(sorter);

            while (list.Count > 1)
            {
                list.Add(new HuffmanNode(list[0], list[1]));
                list.RemoveAt(0);
                list.RemoveAt(0);
                list.Sort(sorter);
            }
            topNode = list[0];
            createStaticGraph(topNode);
            encodedText = Encode();
            double compressionRatio = 100.0 - Math.Floor((double)encodedText.Length / (double)(topNode.Frequency * 8) * 100 * 100) / 100;

            this.label1.Text = "Compression Ratio: " + compressionRatio.ToString() + "%";
            updateTextBox(encodedText);
            dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Ascending);
            UpdateGraph(graph);
        }
コード例 #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() + 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();
            }
        }
コード例 #5
0
        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);
        }
コード例 #6
0
 public void cleanInit()
 {
     graph        = new Microsoft.Msagl.Drawing.Graph("Huffman Tree");
     viewer.Graph = graph;
     viewer.Update();
     Dtable.Clear();
     dataGridView1.Update();
     textBox1.Text  = "";
     textBox2.Text  = "";
     textBox3.Text  = "";
     mainInput      = "";
     encodedText    = "";
     decodedText    = "";
     label1.Text    = "Compression Ratio:";
     nodeZero       = new HuffmanNode();
     EncodeNodeList = new SortedDictionary <int, HuffmanNode>();
     EncodeNodeList.Add(nodeZero.Depth, nodeZero);
     firstReadOfChar = new List <char>();
     dynamicMap      = new Dictionary <char, HuffmanNode>();
 }
コード例 #7
0
 public HuffmanNode(HuffmanNode leftChild, HuffmanNode rightChild, int depth)
 {
     if (leftChild.parentNode != null)
     {
         this.parentNode           = leftChild.parentNode;
         this.parentNode.leftChild = this;
     }
     else
     {
         this.parentNode = null;
     }
     this.leftChild             = leftChild;
     this.leftChild.parentNode  = this;
     this.rightChild            = rightChild;
     this.rightChild.parentNode = this;
     this.isLeaf           = false;
     this.freq             = this.leftChild.Frequency + this.rightChild.Frequency;
     this.depth            = depth;
     this.leftChild.depth  = this.depth - 2;
     this.rightChild.depth = this.depth - 1;
 }
コード例 #8
0
        private void updateDepths(HuffmanNode root)
        {
            Queue <HuffmanNode> queue = new Queue <HuffmanNode>();
            int depthIndex            = 256;

            queue.Enqueue(root);
            HuffmanNode currNode;

            while (queue.Count > 0)
            {
                currNode       = queue.Dequeue();
                currNode.Depth = depthIndex;
                depthIndex--;
                if (currNode.LeftChild != null && currNode.RightChild != null)
                {
                    queue.Enqueue(currNode.RightChild);
                    queue.Enqueue(currNode.LeftChild);
                }
            }
            updateFreqs(root);
        }
コード例 #9
0
        public static bool swap(HuffmanNode node1, HuffmanNode node2)
        {
            //Console.WriteLine("TriedToSwap");
            HuffmanNode leftParent  = node1.parentNode;
            HuffmanNode rightParent = node2.parentNode;

            if (leftParent == node2 || rightParent == node1)
            {
                return(false);
            }
            if (leftParent == rightParent)
            {
                if (leftParent.leftChild == node1)
                {
                    leftParent.leftChild  = node2;
                    leftParent.rightChild = node1;
                }
                else
                {
                    leftParent.leftChild  = node1;
                    leftParent.rightChild = node2;
                }
                return(true);
            }

            int temp = node1.depth;

            node1.depth = node2.depth;
            node2.depth = temp;
            if (leftParent == null)
            {
                node2.unAssignParent();
            }
            else if (leftParent.leftChild == node1)
            {
                leftParent.leftChild = node2;
            }
            else if (leftParent.rightChild == node1)
            {
                leftParent.rightChild = node2;
            }

            if (leftParent != null)
            {
                node2.parentNode = leftParent;
            }

            if (rightParent == null)
            {
                node1.unAssignParent();
            }
            else if (rightParent.leftChild == node2)
            {
                rightParent.leftChild = node1;
            }
            else if (rightParent.rightChild == node2)
            {
                rightParent.rightChild = node1;
            }

            node1.parentNode = rightParent;
            return(true);
        }
コード例 #10
0
 private void unAssignParent()
 {
     this.parentNode = null;
 }