private void huffmancode()
 {
     for (int i = 0; i < list2.Count; i++)
     {
         nodex  curnode  = list2[i];
         nodex  prevnode = null;
         string str1     = "";
         while (curnode != null)
         {
             prevnode = curnode;
             curnode  = curnode.parent;
             if (curnode != null)
             {
                 if (curnode.lc == prevnode)
                 {
                     str1 = str1 + "0";
                 }
                 else
                 {
                     str1 = str1 + "1";
                 }
             }
         }
         str1          = ReverseStringDirect(str1);
         list2[i].code = str1;
     }
 }
        private void button1_Click_1(object sender, EventArgs e)
        {
            string[] a = textBox1.Text.Split(';');
            int      n = a.Length;

            list1 = new List <nodex>();
            list2 = new List <nodex>();
            for (int i = 0; i < n; i++)
            {
                string[] b       = a[i].Split(' ');
                nodex    curnode = new nodex(Convert.ToChar(b[0]), Convert.ToDouble(b[1]));
                list1.Add(curnode);
                list2.Add(curnode);
            }
            list1.Sort(delegate(nodex x, nodex y)
            {
                return(x.val.CompareTo(y.val));
            });
            huffmantree();
            huffmancode();
            showhuffmancode();
        }
 private void huffmantree()
 {
     while (list1.Count > 1)
     {
         nodex cur1;
         nodex cur2;
         cur1 = list1[0];
         cur2 = list1[1];
         nodex root = new nodex(' ', cur1.val + cur2.val);
         root.lc     = cur1;
         root.rc     = cur2;
         cur1.parent = root;
         cur2.parent = root;
         list1.RemoveAt(0);
         list1.RemoveAt(0);
         list1.Add(root);
         list1.Sort(delegate(nodex x, nodex y)
         {
             return(x.val.CompareTo(y.val));
         });
     }
 }