哈弗曼樹及其操作
阿新 • • 發佈:2017-05-06
ret main turn 分享 nbsp new private pla 運行
1.哈弗曼樹的節點聲明
1 package com.neusoft.Tree; 2 3 public class HuffmanNode { 4 public int weight; 5 //加入哈夫曼樹的標誌,flag=0表示該節點沒有加入哈夫曼樹,=1表示加入 6 public int flag; 7 public HuffmanNode parent,lchild,rchild; 8 public HuffmanNode() { 9 this(0); 10 } 11 public HuffmanNode(intweight){ 12 this.weight=weight; 13 flag=0; 14 parent=lchild=rchild=null; 15 } 16 }
點擊可復制代碼
1 package com.neusoft.Tree; 2 3 public class HuffmanNode { 4 public int weight; 5 //加入哈夫曼樹的標誌,flag=0表示該節點沒有加入哈夫曼樹,=1表示加入 6 public int flag; 7 public點擊+可復制代碼HuffmanNode parent,lchild,rchild; 8 public HuffmanNode() { 9 this(0); 10 } 11 public HuffmanNode(int weight){ 12 this.weight=weight; 13 flag=0; 14 parent=lchild=rchild=null; 15 } 16 }
2.哈夫曼編碼的構建及測試
1 package com.neusoft.Tree;2 /** 3 * @author zhao-chj 4 * 哈夫曼樹及其操作 5 */ 6 public class HuffmanTree { 7 8 //求哈夫曼編碼的算法,w存放n個字符的權值 9 public int[][] huffmanCoding (int[] w){ 10 int n= w.length;//哈夫曼樹的字符個數 11 int m=2*n-1;//哈夫曼樹的節點個數 12 HuffmanNode[] HN =new HuffmanNode[m]; 13 int i=0; 14 for (i = 0; i< n; i++) { 15 HN[i] = new HuffmanNode(w[i]);//構造n個具有權值的節點 16 } 17 for (i = n; i< m; i++) { 18 HuffmanNode min1=selectMin(HN,i-1); 19 min1.flag=1;//表示已標記 20 HuffmanNode min2=selectMin(HN, i-1); 21 min2.flag=1; 22 //構造min1和min2的父節點,並且修改權值 23 HN[i] =new HuffmanNode(); 24 min1.parent=HN[i]; 25 min2.parent=HN[i]; 26 HN[i].lchild=min1; 27 HN[i].rchild=min2; 28 HN[i].weight=min1.weight+min2.weight; 29 } 30 //從葉子節點到根逆向求每個字符的哈夫曼編碼 31 int[][] HuffCode =new int[n][n];//分配n個字符編碼存儲空間 32 for (int j = 0; j <n; j++) { 33 int start=n-1;//編碼的開始位置,初始化為數組的結尾 34 for (HuffmanNode c=HN[j],p=c.parent; p!=null; c=p,p=p.parent) { 35 //從葉子結點到根逆向求解編碼 36 if (p.lchild.equals(c)) {//左孩子編碼為0 37 HuffCode[j][start--] =0; 38 }else { 39 //右孩子編碼為1 40 HuffCode[j][start--]=1; 41 } 42 HuffCode[j][start]=-1;//編碼的開始標誌為-1 43 } 44 } 45 return HuffCode; 46 } 47 /** 48 * 在HN[0...i-1]選擇不再哈夫曼樹中且weight最小的節點 49 */ 50 private HuffmanNode selectMin(HuffmanNode[] HN, int end) { 51 HuffmanNode min=HN[end]; 52 for (int i = 0; i < end; i++) { 53 HuffmanNode h=HN[i]; 54 if (h.flag==0&&h.weight<min.weight) { 55 //不再哈夫曼樹中且weight最小的節點 56 min=h; 57 } 58 } 59 return min; 60 } 61 public static void main(String[] args) { 62 int [] w={23,11,5,3,29,14,7,8}; 63 HuffmanTree T =new HuffmanTree();//構造哈夫曼樹 64 int [][] HN=T.huffmanCoding(w);//求哈夫曼編碼 65 System.out.println("哈夫曼編碼為:"); 66 for (int i = 0; i < HN.length; i++) { 67 System.out.print(w[i]+" "); 68 for (int j = 0; j < HN[i].length; j++) { 69 if (HN[i][j] ==-1 ) {//數組結尾標誌 70 for (int k = j+1; k < HN[i].length; k++) { 71 System.out.print(HN[i][k]); 72 } 73 break; 74 } 75 } 76 System.out.println(); 77 } 78 } 79 }
點擊可復制代碼
1 package com.neusoft.Tree; 2 /** 3 * @author zhao-chj 4 * 哈夫曼樹及其操作 5 */ 6 public class HuffmanTree { 7 8 //求哈夫曼編碼的算法,w存放n個字符的權值 9 public int[][] huffmanCoding (int[] w){ 10 int n= w.length;//哈夫曼樹的字符個數 11 int m=2*n-1;//哈夫曼樹的節點個數 12 HuffmanNode[] HN =new HuffmanNode[m]; 13 int i=0; 14 for (i = 0; i< n; i++) { 15 HN[i] = new HuffmanNode(w[i]);//構造n個具有權值的節點 16 } 17 for (i = n; i< m; i++) { 18 HuffmanNode min1=selectMin(HN,i-1); 19 min1.flag=1;//表示已標記 20 HuffmanNode min2=selectMin(HN, i-1); 21 min2.flag=1; 22 //構造min1和min2的父節點,並且修改權值 23 HN[i] =new HuffmanNode(); 24 min1.parent=HN[i]; 25 min2.parent=HN[i]; 26 HN[i].lchild=min1; 27 HN[i].rchild=min2; 28 HN[i].weight=min1.weight+min2.weight; 29 } 30 //從葉子節點到根逆向求每個字符的哈夫曼編碼 31 int[][] HuffCode =new int[n][n];//分配n個字符編碼存儲空間 32 for (int j = 0; j <n; j++) { 33 int start=n-1;//編碼的開始位置,初始化為數組的結尾 34 for (HuffmanNode c=HN[j],p=c.parent; p!=null; c=p,p=p.parent) { 35 //從葉子結點到根逆向求解編碼 36 if (p.lchild.equals(c)) {//左孩子編碼為0 37 HuffCode[j][start--] =0; 38 }else { 39 //右孩子編碼為1 40 HuffCode[j][start--]=1; 41 } 42 HuffCode[j][start]=-1;//編碼的開始標誌為-1 43 } 44 } 45 return HuffCode; 46 } 47 /** 48 * 在HN[0...i-1]選擇不再哈夫曼樹中且weight最小的節點 49 */ 50 private HuffmanNode selectMin(HuffmanNode[] HN, int end) { 51 HuffmanNode min=HN[end]; 52 for (int i = 0; i < end; i++) { 53 HuffmanNode h=HN[i]; 54 if (h.flag==0&&h.weight<min.weight) { 55 //不再哈夫曼樹中且weight最小的節點 56 min=h; 57 } 58 } 59 return min; 60 } 61 public static void main(String[] args) { 62 int [] w={23,11,5,3,29,14,7,8}; 63 HuffmanTree T =new HuffmanTree();//構造哈夫曼樹 64 int [][] HN=T.huffmanCoding(w);//求哈夫曼編碼 65 System.out.println("哈夫曼編碼為:"); 66 for (int i = 0; i < HN.length; i++) { 67 System.out.print(w[i]+" "); 68 for (int j = 0; j < HN[i].length; j++) { 69 if (HN[i][j] ==-1 ) {//數組結尾標誌 70 for (int k = j+1; k < HN[i].length; k++) { 71 System.out.print(HN[i][k]); 72 } 73 break; 74 } 75 } 76 System.out.println(); 77 } 78 } 79 }點擊+可復制上述代碼
3.測試及運行結果
哈弗曼樹及其操作