哈夫曼编码过程以及实例:
由上篇文章哈夫曼树与哈夫曼编码原理分析,设置一个字符串“aasdf ghjkd hsgaf sfdga ahsjh g”
统计一下各字符出现的频率k,1 f,2 j,2 d,3 h,4 a,5 s,4 g,4
| 字符 | k | f | j | d | h | a | s | g |
|---|---|---|---|---|---|---|---|---|
| 频次 | 1 | 2 | 2 | 3 | 4 | 5 | 4 | 4 |
1.为以上每一个字符创建一个二叉树,并将其出现的频次存储在节点中,节点的数据结构定义为Node:
public class Node {
//只存储两个值 0,1 private String ch; //存储字符出现的频率次数 private int num; //左节点 private Node left; //右节点 private Node right; public Node(String c, int n){
ch = c; num = n; } public Node(){
} public String getCh() {
return ch; } public void setCh(String ch) {
this.ch = ch; } public int getNum() {
return num; } public void setNum(int num) {
this.num = num; } public Node getLeft() {
return left; } public void setLeft(Node left) {
this.left = left; } public Node getRight() {
return right; } public void setRight(Node right) {
this.right = right; } @Override public String toString() {
return "Node{" + "ch='" + ch + '\'' + ", num=" + num + ", left=" + left + ", right=" + right + '}'; } }
讯享网
2.代码中有详细解释,末尾有运行效果图<

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/101033.html