码迷,mamicode.com
首页 > 其他好文 > 详细

霍夫曼树

时间:2016-03-11 22:23:56      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

1.构造原理:权值小的节点应该处于树的下面,他们必是作为孩子,因此选择权值小的节点作为子节点自底向上构造树。根据二叉树的特点,两个节点合并后对于剩下来说他们相当于一个节点而已,所以计算权值和把其当作一个新的节点放到未构造的节点集中。不断构造合并,最后便会使kmp最小

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

/**
 * Created by 刘逗逼 on 2016/3/11.
 */
public class MaxHeap<T extends Comparable<T>> {
    private List<T> head;
    public MaxHeap(){
        this.head=new ArrayList<T>();
    }
    protected void filterup(int start){          //向上调整,用于插入
        int child=start;
        int parent=(child-1)/2;                 //因为数组下标从0开始
        T tmp=head.get(child);
        while(true){
            int cmp=head.get(parent).compareTo(tmp);
            if(cmp>0){
                break;
            }else {             //换位
                head.set(child,head.get(parent));
                child=parent;
                parent=(parent-1)/2;
            }
        }
        head.set(child,tmp);
    }
    protected void filterdown(int start,int end){       //end是判断是否出界的
        int parent=start;
        int left=2*parent+1;
        T tmp=head.get(parent);
        while(left<=end){
            int cmp=head.get(left).compareTo(head.get(left+1));
            if(cmp<0)left=+1;
            head.set(parent,head.get(left));
            cmp=head.get(parent).compareTo(head.get(left));
            if(cmp>0){
                break;
            }else {
                head.set(parent,head.get(left));
                parent=left;
                left=left*2+1;
            }
        }
        head.set(parent,tmp);
    }
      /*
       * 删除最大堆中的data
       *
       * 返回值:
       *      0,成功
       *     -1,失败
       */

    protected int remove(T data){
        if(head.isEmpty()){
            return -1;
        }
        int index=head.indexOf(data);
        if(index==-1)return -1;
        int size=head.size();
        head.set(index,head.get(size-1));
        head.remove(size-1);
        if(head.size()>1){
            filterdown(index,head.size()-1);
        }
        return 0;
    }
    protected void insert(T data){
        head.add(data);
        filterup(head.size()-1);
    }
    @Override
    public String toString(){
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<head.size();i++){
            stringBuilder.append(head.get(i)+" ");
        }
        return stringBuilder.toString();
    }
    public static void main(String[] args){
        int i;
        int a[]={10,40,30,60,90,70,20,50,80};
        MaxHeap<Integer> tree=new MaxHeap();
        System.out.println("== 依次添加: ");
        for( i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
            tree.head.add(a[i]);
        }
        System.out.println("");
        System.out.println("==添加最大堆  "+tree.toString());
        i=85;
        tree.insert(i);
        System.out.println("==添加元素: "+i);
        System.out.println("==最大堆     "+tree.toString());
        i=90;
        tree.remove(i);
        System.out.println("==删除元素: "+i);
        System.out.println("==最大堆     "+tree.toString());
    }
}

 

霍夫曼树

标签:

原文地址:http://www.cnblogs.com/liuzhugu/p/5267272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!