1 Star 0 Fork 0

besti1923/JAVA

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
HaffTree.java 1.35 KB
一键复制 编辑 原始数据 按行查看 历史
yifei 提交于 5年前 . bianma
package text.java;
import java.util.Arrays;
public class HaffTree <T>{
HaffNode root;
public HaffTree(){
root=null;
}
public HaffTree(HaffNode [] node){
while(node.length>1){
node=sort(node);
HaffNode current=new HaffNode(node[0],node[1]);
node[1]=current;
node= Arrays.copyOfRange(node,1,node.length);
}
root=node[0];
}
public HaffNode[] sort(HaffNode[] node){
for(int gap=node.length/2;gap>0;gap/=2){
for(int i=gap;i<node.length;i++){
int j=i;
while (j-gap>=0&&node[j].weight<node[j-gap].weight){
swap(node,j,j-gap);
j-=gap;
}
}
}
return node;
}
public void swap(HaffNode [] node,int a,int b){
HaffNode temp;
temp=node[a];
node[a]=node[b];
node[b]=temp;
}
public String decode(String s){//解码
String str="";
int i=0;
HaffNode current=root;
while(i<s.length()) {
while (current.leftChild != null) {
if (s.charAt(i)=='0') current = current.leftChild;
else current = current.rightChild;
i++;
}
str+=current.data;
current=root;
}
return str;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/besti1923/java.git
git@gitee.com:besti1923/java.git
besti1923
java
JAVA
master

搜索帮助