前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据结构之基于Java的最优二叉树实现

数据结构之基于Java的最优二叉树实现

作者头像
IT架构圈
发布2018-05-31 15:41:35
5070
发布2018-05-31 15:41:35
举报
文章被收录于专栏:IT架构圈
代码语言:javascript
复制
import dsa.adt.BinaryTreeLinked;
public class HuffmanTreeLinked extends BinaryTreeLinked {
    public HuffmanTreeLinked(HuffmanTreeNode[] nodes) {
        this(nodes,new DefaultStrategy());
    }
    public HuffmanTreeLinked(HuffmanTreeNode[] nodes, Strategy strategy){
        super(buildHuffmanTree(nodes),strategy);
        generateHuffmanCode((HuffmanTreeNode)super.getRoot());
    }
    //返回Huffman的所有叶子结点
    public Iterator getAllLeafs(){
        LinkedList list = new LinkedListDLNode();
        getLeafs(getRoot(),list);
        return list.elements();
    }
    private void getLeafs(HuffmanTreeNode root, LinkedList list){
        if (root==null) return;
        if (root.isLeaf()) list.insertLast(root);
        getLeafs(root.getLChild(),list);
        getLeafs(root.getRChild(),list);
    }
    //递归生成Huffman编码
    private static void generateHuffmanCode(HuffmanTreeNode root){
        if (root==null) return;
        if (root.hasParent()){
            if (root.isLChild()) root.setCoding(root.getParent().getCoding() + "0");
            else                 root.setCoding(root.getParent().getCoding() + "1");
        }
        generateHuffmanCode(root.getLChild());
        generateHuffmanCode(root.getRChild());
    }
    //通过结点数组生成Huffman树
    private static HuffmanTreeNode buildHuffmanTree(HuffmanTreeNode[] nodes){
        int n = nodes.length;
        if (n<2) return nodes[0];
        List l = new ListArray();   //根结点线性表,按weight从大到小有序
        for (int i=0; i<n; i++)     //将结点逐一插入线性表
            insertToList(l,nodes[i]);
        for (int i=1; i<n; i++){    //选择weight最小的两棵树合并,循环n-1次
            HuffmanTreeNode min1 = (HuffmanTreeNode)l.remove(l.getSize()-1);//选择weight最小的树
            HuffmanTreeNode min2 = (HuffmanTreeNode)l.remove(l.getSize()-1);//选择weight次小的树
            HuffmanTreeNode newRoot = new HuffmanTreeNode(min1.getWeight()+min2.getWeight());//合并
            newRoot.setLChild(min1);
            newRoot.setRChild(min2);
            insertToList(l,newRoot);//新树插入线性表
        }
        return (HuffmanTreeNode)l.get(0);//返回Huffman树的根
    }
    //将结点按照weight从大到小的顺序插入线性表
    private static void insertToList(List l, HuffmanTreeNode node){
        for (int j=0; j<l.getSize(); j++)
            if (node.getWeight()>((HuffmanTreeNode)l.get(j)).getWeight()){
                l.insert(j,node);
                return;
            }
        l.insert(l.getSize(),node);
    }
    public HuffmanTreeNode getRoot(){
        return (HuffmanTreeNode)super.getRoot();
    }
}
public interface Strategy {
    //判断两个数据元素是否相等
    public boolean equal(Object obj1, Object obj2);
    /**
     * 比较两个数据元素的大小
     * 如果obj1 < obj2 返回-1
     * 如果obj1 = obj2 返回0
     * 如果obj1 > obj2 返回1
     */
    public int compare(Object obj1, Object obj2);
}
public final class DefaultStrategy implements Strategy 
{
    public boolean equal(Object obj1, Object obj2) 
    {
        return obj1.toString().equals(obj2.toString());
    }
    public int compare(Object obj1, Object obj2)
    {
        int comp = obj1.toString().compareTo(obj2.toString());
        if (comp==0) return 0;
        else if (comp>0) return 1;
        else return -1;
    }
}
package dsa.adt;
import dsa.adt.BinTreeNode;
public class HuffmanTreeNode extends BinTreeNode {
    private int weight;         //权值
    private String coding = ""; //编码
    public HuffmanTreeNode(int weight){
        this(weight,null);
    }
    public HuffmanTreeNode(int weight, Object e){
        super(e);
        this.weight = weight;
    }
    //改写父类方法
    public HuffmanTreeNode getParent() {
        return (HuffmanTreeNode)super.getParent();
    }
    public HuffmanTreeNode getLChild() {
        return (HuffmanTreeNode)super.getLChild();
    }
    public HuffmanTreeNode getRChild() {
        return (HuffmanTreeNode)super.getRChild();
    }
    //get&set方法
    public int getWeight(){ return weight;}
    public String getCoding(){ return coding;}
    public void setCoding(String coding){ this.coding = coding;}    
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-03-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程坑太多 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档