字符怎么转化数字 就是a(字符型)转化为数字1(整型) 然后依次排下去 26个英文字母  有没有强制执行a(字符型)=1(整型)的语句
  java的哈弗曼树编码译码 已经把哈弗曼树写出来了 然后要输入字符串 编码 只要能让字符串里的字母对号入座  就行了  但是我不会弄 望各位大大指点一下

解决方案 »

  1.   

    这是我的哈弗曼树 上网找的 直接加入数据变成了我要的哈弗曼树....class Code       //哈夫曼编码类    
    {
        int[] bit;  //数组    
        int start;  //编码的起始下标    
        int weight; //字符的权值    
           
        public  Code(int n)
    {   
            bit = new int[n];   
            start = n - 1;   
        }   
    }   
       
    class HaffNode //哈夫曼树的结点类    

    int weight; //权值    
        int flag; //标记    
        int parent; //双亲结点下标    
        int leftChild; //左孩子下标    
        int rightChild; //右孩子下标    
           
        public HaffNode()
    {                         
        }   
    }   
       
    class HaffmanTree
    {   
        static final int maxValue = 10000;  //最大权值    
        private int nodeNum;                //叶结点个数    
           
        public HaffmanTree(int n)
    {   
            nodeNum = n;   
        }   
           
        public void haffman(int[] weight, HaffNode[] node)    //构造权值为weight的哈夫曼树haffTree    
    {   
            int m1, m2, x1, x2;   
            int n = nodeNum;   
       
            //哈夫曼树haffTree初始化。n个叶结点的哈夫曼树共有2n-1个结点    
            for(int i = 0; i < 2 * n - 1; i ++)
    {   
                HaffNode temp = new HaffNode();   
                if(i < n)    
                    temp.weight = weight[i];   
                else   
                    temp.weight = 0;   
                temp.parent = 0;       
                temp.flag = 0;   
                temp.leftChild = -1;   
                temp.rightChild = -1;   
                node[i] = temp;   
            }   
       
            //构造哈夫曼树haffTree的n-1个非叶结点    
            for(int i = 0; i < n - 1; i++)
    {   
                m1 = m2 = maxValue;   
                x1 = x2 = 0;   
                for(int j = 0; j < n + i; j ++)
    {   
                    if(node[j].weight < m1 && node[j].flag == 0)
    {   
                        m2 = m1;   
                        x2 = x1;   
                        m1 = node[j].weight;   
                        x1 = j;   
    }   
                    else if(node[j].weight < m2 && node[j].flag == 0)
    {   
                        m2 = node[j].weight;   
                        x2 = j;   
                    }   
                }   
       
                //将找出的两棵权值最小的子树合并为一棵子树    
                node[x1].parent  = n+i;      
                node[x2].parent  = n+i;   
                node[x1].flag    = 1;   
                node[x2].flag    = 1;   
                node[n+i].weight = node[x1].weight+node[x2].weight;   
                node[n+i].leftChild = x1;   
                node[n+i].rightChild = x2;   
            }   
        }   
           
        public void haffmanCode(HaffNode[] node, Code[] haffCode)
    {   
        //由哈夫曼树haffTree构造哈夫曼编码haffCode    
            int n = nodeNum;   
            Code cd = new Code(n);   
            int child, parent;   
       
            //求n个叶结点的哈夫曼编码    
            for(int i = 0; i < n; i ++)
    {   
                cd.start = n - 1; //不等长编码的最后一位为n-1    
                cd.weight = node[i].weight; //取得编码对应的权值    
                child = i;   
                parent = node[child].parent;   
       
                while(parent != 0)
    {   
                //由叶结点向上直到根结点循环    
                    if(node[parent].leftChild == child)   
                        cd.bit[cd.start] = 0; //左孩子结点编码0    
                    else   
                        cd.bit[cd.start] = 1; //右孩子结点编码1    
                    cd.start --;   
                    child = parent;   
                    parent = node[child].parent;           
                }   
                   
                Code temp = new Code(n);   
       
                //保存叶结点的编码和不等长编码的起始位    
                for(int j = cd.start+1; j < n; j++)    
                    temp.bit[j] = cd.bit[j];   
                temp.start  = cd.start;   
                temp.weight = cd.weight;   
                haffCode[i] = temp;   
            }   
        }   
    }   
       
    public class Huffman_Tree
    {   
        public static void main(String[] args)
    {   
            int n = 26;   
            HaffmanTree myHaff = new HaffmanTree(n);   
            int[] weight = {64,13,22,32,103,21,15,47,57,1,5,32,20,57,63,15,1,48,51,80,23,8,18,1,16,1};   
            HaffNode[] node = new HaffNode[2 * n + 1];   
            Code[] haffCode = new Code[n];
    String[] zimu = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    int[] target = {19,7,8,18,15,17,14,6,17,0,12,8,18,12,24,5,0,21,14,17,8,19,5};
       
            myHaff.haffman(weight, node);   
            myHaff.haffmanCode(node, haffCode);   
               
            //输出每个叶结点的哈夫曼编码    
            System.out.print("从A到Z的编码依次为:");
    System.out.println();
    for(int i = 0; i < n; i ++)
    {
                System.out.print(zimu[i]+":Weight = " + haffCode[i].weight + " Code = ");   
                for(int j = haffCode[i].start + 1; j < n; j ++)   
                    System.out.print(haffCode[i].bit[j]);   
                System.out.println();   
            } 
            System.out.print("“THIS PROGRAM IS MY FAVORITE”编码为:");
    System.out.println();
    for(int i = 0; i < 23; i ++)
    {   
    int j=target[i];
                for(int k = haffCode[j].start + 1; k < n; k ++)   
                    System.out.print(haffCode[j].bit[k]);    
            }
    }   
    }
      

  2.   

    public static void main(String args[]) {
    String s="a";
    System.out.println(s.hashCode());
    }
      

  3.   

    如果仅仅是要一个字符和数字的对应关系,好象和哈夫曼树没啥关系
    因为在asc表里,字符确实是顺序排列的。
    只考虑小写字母,b-a=1
    那你所要做的就仅仅是把字符转换成int型了,然后计算相对字符a的偏移值
    public static int trans(char c){
    int t = c-'a'+1;
    return t;
    }
      

  4.   


    String str1 = "string";
    byte[] byteArr = str1.getBytes();
    //然后循环此数组,过程中调用byte的intValue()方法。
    //是这意思?
      

  5.   

    String s = "a";
    int i = Integer.parseInt(s);需要try  catch
      

  6.   

    答:
    char ch='b';
    int  num=ch-'a'+1;
      

  7.   

    大家大概不是很明白我的意思  这样的 我想这么用 你们看可以不可以 就是 
      本来       System.out.print(zimu[i]+":Weight = " + haffCode[i].weight + " Code = ");  是输出第i个结点的权值
    然后我改成   System.out.print(zimu[a]+":Weight = " + haffCode[a].weight + " Code = ");  把上面的计数i改成了c 这样它变成第3个结点的权值 不知道可以不可以
       若不可以  介绍我一种方法 将输入一段英文字符  对应到我对英文字母的编码上面去 
       就是说 我现在有顺序的输入了 a-z 的权值 经过我的程序运算后 得到了顺序的  权值 = 1,2,3,...,n 对应的  编码  然后我现在输入一段字符串  ab   我有之前的编码  a对应一个01 b对应一个00 所以我的ab编码后是0100  这样 我要怎么做才行? 上面的数字都不是真实的
      

  8.   

    public class temp{
       public static void main(String[] args){
      int[] charNum = new int[26];
      char c = 'a';
      for(int i = 0; i < charNum.length; i ++){
     charNum[i] = (int)(c - 'a') + 1;
         c++;
     System.out.print(charNum[i] + " ");
      }
       }
    }