Ejemplo n.º 1
0
        nodes generate_Tree_for_huffman(List <nodes> list)
        {
            if (list.Count <= 1)
            {
                list[0].sympol = list[0].prob.ToString();
                return(list[0]);
            }
            if (list.Count == 2)
            {
                nodes node_left  = new nodes();
                nodes node_right = new nodes();

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

                nodes final = new nodes();
                final.Left   = node_left;
                final.Right  = node_right;
                final.prob   = node_right.prob + node_left.prob;
                final.sympol = final.prob.ToString();
                return(final);
            }
            List <nodes> temp_lest = new List <nodes>();

            temp_lest = list;

            nodes node_left1  = new nodes();
            nodes node_right1 = new nodes();

            node_left1 = list[0];

            node_right1 = list[1];

            nodes left_or_right = new nodes();

            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));


            nodes last_final = new nodes();

            last_final = generate_Tree_for_huffman(temp_lest);

            return(last_final);
        }
Ejemplo n.º 2
0
 private void InorderTraversal(nodes 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);
     }
 }
Ejemplo n.º 3
0
 private int TreeHeight(nodes t)
 {
     if (t == null)
     {
         return(-1);
     }
     else
     {
         return(1 + Math.Max(TreeHeight(t.Left), TreeHeight(t.Right)));
     }
 }
Ejemplo n.º 4
0
        nodes generate_Tree(List <Tuple <int, char> > list)
        {
            if (list.Count <= 1)
            {
                nodes ret = new nodes();
                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]);
                }
            }
            nodes right_node = new nodes();
            nodes left_node  = new nodes();

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

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

            nodes nnn = new nodes();

            nnn.Left  = left_node;
            nnn.Right = right_node;
            return(nnn);
        }
Ejemplo n.º 5
0
        public void DrawTree(nodes 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());
            }
        }
Ejemplo n.º 6
0
        public string calculate_bits(nodes 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("");
        }
Ejemplo n.º 7
0
        public int set_prob(nodes 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);
        }
Ejemplo n.º 8
0
        public String rename_nodes(nodes 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);
        }
Ejemplo n.º 9
0
        private void button2_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));
            nodes c = new nodes();

            //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();
        }