日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

哈弗曼树及其操作

發布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 哈弗曼树及其操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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(int weight){
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.測試及運行結果

?

轉載于:https://www.cnblogs.com/jackchen-Net/p/6816003.html

總結

以上是生活随笔為你收集整理的哈弗曼树及其操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。