Beispiel #1
0
        f_node generate_Tree_for_huffman(List <f_node> list)
        {
            if (list.Count <= 1)
            {
                list[0].sympol = list[0].prob.ToString();
                return(list[0]);
            }
            if (list.Count == 2)
            {
                f_node node_left  = new f_node();
                f_node node_right = new f_node();

                node_left  = list[0];
                node_right = list[1];

                f_node final = new f_node();
                final.Left   = node_left;
                final.Right  = node_right;
                final.prob   = node_right.prob + node_left.prob;
                final.sympol = final.prob.ToString();
                return(final);
            }

            List <f_node> temp_lest = new List <f_node>();

            temp_lest = list;

            f_node node_left1  = new f_node();
            f_node node_right1 = new f_node();

            node_left1 = list[0];

            node_right1 = list[1];

            f_node left_or_right = new f_node();

            left_or_right.Left   = node_left1;
            left_or_right.Right  = node_right1;
            left_or_right.prob   = node_right1.prob + node_left1.prob;
            left_or_right.sympol = left_or_right.prob.ToString();

            temp_lest.RemoveAt(0);
            temp_lest.RemoveAt(0);



            temp_lest.Add(left_or_right);
            //temp_lest.Sort((x, y) => x.Item1.CompareTo(y.Item1));

            temp_lest.Sort((x, y) => x.prob.CompareTo(y.prob));


            f_node last_final = new f_node();

            last_final = generate_Tree_for_huffman(temp_lest);

            return(last_final);
        }
Beispiel #2
0
 private void InorderTraversal(f_node t, int depth)
 {
     if (t != null)
     {
         InorderTraversal(t.Left, depth + 1); //add 1 to depth (y coordinate)
         t.Xpos = totalNodes++ + 1;           //x coord is node number in inorder traversal
         t.Ypos = depth - 1;                  // mark y coord as depth
         InorderTraversal(t.Right, depth + 1);
     }
 }
Beispiel #3
0
 private int TreeHeight(f_node t)
 {
     if (t == null)
     {
         return(-1);
     }
     else
     {
         return(1 + Math.Max(TreeHeight(t.Left), TreeHeight(t.Right)));
     }
 }
Beispiel #4
0
        f_node generate_Tree(List <Tuple <int, char> > list)
        {
            if (list.Count <= 1)
            {
                f_node ret = new f_node();
                ret.Left   = null;
                ret.Right  = null;
                ret.sympol = list[0].Item2.ToString();
                return(ret);
            }

            List <Tuple <int, char> > temp_list = new List <Tuple <int, char> >();
            int all_sympol_count = 0;

            temp_list = list;
            for (int i = 0; i < temp_list.Count; i++)
            {
                all_sympol_count += temp_list[i].Item1;
            }
            List <Tuple <int, char> > right_list = new List <Tuple <int, char> >();
            List <Tuple <int, char> > left_list  = new List <Tuple <int, char> >();

            int sum_to_hulf = 0;

            for (int i = 0; i < temp_list.Count; i++)
            {
                if (sum_to_hulf < all_sympol_count / 2)
                {
                    right_list.Add(temp_list[i]);
                    sum_to_hulf += temp_list[i].Item1;
                }
                else
                {
                    left_list.Add(temp_list[i]);
                }
            }
            f_node right_node = new f_node();
            f_node left_node  = new f_node();

            right_node = generate_Tree(right_list);
            left_node  = generate_Tree(left_list);

            right_node.bit = "1";
            left_node.bit  = "0";

            f_node nnn = new f_node();

            nnn.Left  = left_node;
            nnn.Right = right_node;
            return(nnn);
        }
Beispiel #5
0
        public void DrawTree(f_node root, Graphics g)
        {
            try
            {
                panel1.Width  = ClientSize.Width - 20;
                panel1.Height = ClientSize.Height - 20;

                int Width = panel1.Width;
                int Height = panel1.Height;
                int dx = 0, dy = 0, dx2 = 0, dy2 = 0, ys = 20;
                int XSCALE, YSCALE;
                int treeHeight = TreeHeight(root);

                XSCALE = (int)(Width / totalNodes);                  //scale x by total nodes in tree
                YSCALE = (int)((Height - ys) / (maxTreeHeight + 1)); //scale y by tree height

                if (root != null)
                {
                    // inorder traversal to draw each node
                    DrawTree(root.Left, g);            // do left side of inorder traversal
                    dx = root.Xpos * XSCALE;           // get x,y coords., and scale them
                    dy = root.Ypos * YSCALE;
                    string s = root.sympol.ToString(); //get the word at this node
                    s += "(" + root.prob.ToString() + ")";
                    g.DrawString(s, panel1.Font, blackBrush, new PointF(dx - ys, dy));
                    // this draws the lines from a node to its children, if any
                    if (root.Left != null)
                    {
                        //draws the line to left child if it exists
                        dx2 = root.Left.Xpos * XSCALE;
                        dy2 = root.Left.Ypos * YSCALE;
                        g.DrawLine(blackPen, dx, dy, dx2, dy2);
                    }

                    if (root.Right != null)
                    {
                        //draws the line to right child if it exists
                        dx2 = root.Right.Xpos * XSCALE;//get right child x,y scaled position
                        dy2 = root.Right.Ypos * YSCALE;
                        g.DrawLine(blackPen, dx, dy, dx2, dy2);
                    }

                    DrawTree(root.Right, g); //now do right side of inorder traversal
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString());
            }
        }
Beispiel #6
0
        public string calculate_bits(f_node root, string code)
        {
            if (root.Left == null && root.Right == null)
            {
                List <String> temp = new List <string>();
                temp.Add(root.sympol);
                temp.Add(code);
                temp.Add(root.prob.ToString());
                Encode_Bits.Add(temp);
                return("");
            }

            calculate_bits(root.Left, code + "0");
            calculate_bits(root.Right, code + "1");


            return("");
        }
Beispiel #7
0
        public int set_prob(f_node tree)
        {
            if (tree == null)
            {
                return(0);
            }
            else if (tree.Left == null && tree.Right == null)
            {
                tree.prob = 1;
                return(1);
            }
            int left  = set_prob(tree.Left);
            int right = set_prob(tree.Right);

            tree.prob = left + right;
            the_tree  = tree;
            return(1);
        }
Beispiel #8
0
        public String rename_nodes(f_node tree)
        {
            if (tree == null)
            {
                return("");
            }
            if (tree.Left == null && tree.Right == null)
            {
                return(tree.sympol);
            }

            String right_name = rename_nodes(tree.Right);
            String left_name  = rename_nodes(tree.Left);

            if (tree.sympol == "#")
            {
                tree.sympol = right_name + " , " + left_name;
            }
            return(tree.sympol);
        }
Beispiel #9
0
        private void button1_Click(object sender, EventArgs e)
        {
            repetation_chars.Clear();
            if (textBox1.Text == "")
            {
                return;
            }
            String code = textBox1.Text;

            for (int i = 0; i < code.Length; i++)
            {
                if (repetation_chars.ContainsKey(code[i]))
                {
                    repetation_chars[code[i]]++;
                }
                else
                {
                    repetation_chars[code[i]] = 1;
                }
            }
            List <Tuple <int, char> > rep = new List <Tuple <int, char> >();

            var arrayOfAllKeys = repetation_chars.Keys.ToArray();

            //var arrayOfAllValues = repetation_chars.Values.ToArray();

            for (int i = 0; i < arrayOfAllKeys.Length; i++)
            {
                Tuple <int, char> ttt = Tuple.Create(repetation_chars[arrayOfAllKeys[i]], arrayOfAllKeys[i]);

                rep.Add(ttt);
            }
            rep.Sort((x, y) => y.Item1.CompareTo(x.Item1));
            f_node c = new f_node();

            //MessageBox.Show(c.ToString());
            //int g = 3;

            the_tree = generate_Tree(rep);

            Encode_Bits.Clear();

            List <String> temp1 = new List <string>();

            temp1.Add("Sympol");
            temp1.Add("bits");
            temp1.Add("counter");
            Encode_Bits.Add(temp1);

            calculate_bits(the_tree, "");


            int depth = 1;

            totalNodes = 0;
            InorderTraversal(the_tree, depth);
            maxTreeHeight = TreeHeight(the_tree);
            rename_nodes(the_tree);
            set_prob(the_tree);
            panel1.Invalidate();
        }