package Hafuman;

import java.util.ArrayList;
import java.util.List;
//哈夫曼树原理
//http://blog.csdn.net/shuangde800/article/details/7341289

public class TreeNode {
    int id;
    String nodeVaule;
    double weight;
    TreeNode leftNode;
    TreeNode rightNode;
    TreeNode parentNode;
    
    
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getNodeVaule() {
        return nodeVaule;
    }
    public void setNodeVaule(String nodeVaule) {
        this.nodeVaule = nodeVaule;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    
    public TreeNode getLeftNode() {
        return leftNode;
    }
    public void setLeftNode(TreeNode leftNode) {
        this.leftNode = leftNode;
    }
    public TreeNode getRightNode() {
        return rightNode;
    }
    public void setRightNode(TreeNode rightNode) {
        this.rightNode = rightNode;
    }
    public TreeNode getParentNode() {
        return parentNode;
    }
    public void setParentNode(TreeNode parentNode) {
        this.parentNode = parentNode;
    }
    public static int getSelfId() {
        return selfId;
    }
    public static void setSelfId(int selfId) {
        TreeNode.selfId = selfId;
    }
    static int selfId = 0;
    //建立一个哈夫曼树
    public TreeNode creatTree(List<List<String>>dataList){
        //创建一个森林
        List<TreeNode> dataNodes = new ArrayList();
        for(int i = 0;i<dataList.size();i++){
            List temp = dataList.get(i);
            TreeNode tempNode = new TreeNode();
            
            tempNode.setId(++selfId);
            tempNode.setNodeVaule((String) temp.get(0));
            tempNode.setWeight(Double.valueOf((String) temp.get(1)));
            tempNode.setLeftNode(new TreeNode());
            tempNode.setRightNode(new TreeNode());
            dataNodes.add(tempNode);
        }
        //for(int i = 0;i<3;i++){
        while(dataNodes.size()>1){
            dataNodes=creatNew(dataNodes);
        }
        TreeNode tree = dataNodes.get(0);
        return tree;
    }
    public List<TreeNode> creatNew(List<TreeNode> dataNodes){
        int FirstIndex = selectmin(dataNodes);
        TreeNode First = dataNodes.get(FirstIndex);
        dataNodes.remove(FirstIndex);
        int SecondIndex = selectmin(dataNodes);
        TreeNode Second = dataNodes.get(SecondIndex);
        dataNodes.remove(SecondIndex);
        TreeNode newNode = new TreeNode();
        newNode.setLeftNode(First);
        newNode.setRightNode(Second);
        First.setParentNode(newNode);
        Second.setParentNode(newNode);
        newNode.setWeight(First.getWeight()+Second.getWeight());
        newNode.setId(++selfId);
        dataNodes.add(newNode);
        return dataNodes;
    }
    private int selectmin(List<TreeNode> dataNodes) {
        double min = dataNodes.get(0).getWeight();
        int minIndex = 0;
        for(int i = 1;i<dataNodes.size();i++){
            if(min>dataNodes.get(i).getWeight()){
                min = dataNodes.get(i).getWeight();
                minIndex = i;
            }
        }
        return minIndex;
    }
    
    
    //哈夫曼编码
    public void traverse(){
        if(this.getNodeVaule()!=null){
            List codethis = Code(this);
            System.out.print(this.getNodeVaule()+":  ");
            for(int i = 0;i<codethis.size();i++){
                
                System.out.print(codethis.get(codethis.size()-i-1));
            }
            System.out.println();
        }
        if(this.getRightNode()==null){
            return;
        }else{
            this.getRightNode().traverse();
        }
        if(this.getLeftNode()==null){
            return;
        }else{
            this.getLeftNode().traverse();
        }
    }
    
    //对于一个确定有值得node的编码
    public List Code(TreeNode node){
        List codeList = new ArrayList();
        if(node.getParentNode()!=null){
            TreeNode parent = node.getParentNode();
            if(parent.getLeftNode().getId()==node.getId()){
                codeList.add(0);
                codeList.addAll(Code(parent));
                return codeList;
            }else{
                codeList.add(1);
                codeList.addAll(Code(parent));
                return codeList;
            }
        }else{
            return codeList;
        }
}
}




package Hafuman;

import java.util.ArrayList;
import java.util.List;

public class testClass {

    public static void main(String[] args) {
        String [][]data ={{"a","1"},{"b","5"},{"c","10"},{"d","8"},{"u","3"},{"p","11"}};
        List<List<String>>dataList = new ArrayList();
        for(int i = 0;i<data.length;i++){
            List temp = new ArrayList();
            for(int j = 0;j<data[i].length;j++){
                temp.add(data[i][j]);
            }
            dataList.add(temp);
        }
        TreeNode t = new TreeNode();
        t=t.creatTree(dataList);
        t.traverse();
    }

}

 

 

 

posted on 2016-12-03 22:22  薄樱  阅读(323)  评论(0编辑  收藏  举报