2013.12.19

* 变量声明的位置…

    在while循环外声明t结点,由于在循环内通过t存储从que中remove()的结点,还要将t重新insert()到que内,这样会导致t重新指向被remove()出来的自身,出现死循环;

    因此应该将t声明在while循环内,每次循环重新创建;

        public static HuffNode huffEstab(PQueue que){
//              HuffNode t=new HuffNode(new HuffData());
                while(que.number()>1){
                        HuffNode t=new HuffNode(new HuffData());
                        t.left=que.remove();
                        t.right=que.remove();
                        t.d.context='G';
                        t.d.freq=t.left.d.freq+t.right.d.freq;
                        que.insert(t);
                }
                return que.remove();
        }

* 霍夫编码中的优先级队列,可以以树为元素,也可以设置为以结点为元素存储,都是存储根结点;


霍夫编码树@@java/DataStructure/unit8

import java.util.*;

class HuffNode{
	public HuffData d;
	public HuffNode left;
	public HuffNode right;

	public HuffNode(){
		d=new HuffData();
		left=null;
		right=null;
	}

	public HuffNode(HuffData d){
		this.d=d;
		left=null;
		right=null;
	}

	public void display(){
		System.out.println(d.context+", "+d.freq);
	}
}

class HuffData{
	public char context;
	public double freq;

	public HuffData(){
		context='!';
		freq=0.0;
	}

	public HuffData(char context, double freq){
		this.context=context;
		this.freq=freq;
	}
}

class PQueue{
	private HuffNode [] nArr;
	private int max;
	private int current;

	public PQueue(int max){
		this.max=max;
		nArr=new HuffNode [max];
		for(int i=0; i<max; i++) nArr[i]=null;
		current=0;
	}

	public void insert(HuffNode n){
		int i=current-1;
		if(this.isFull()){System.out.println("This Queue is Full! "); return;}
		for(; i>=0 && nArr[i].d.freq<n.d.freq; i--) nArr[i+1]=nArr[i];
		nArr[i+1]=n;
		current++;
	}

	public int number(){
		return current;
	}

	public HuffNode remove(){
		return nArr[--current];
	}

	public HuffNode peek(){
		return nArr[current-1];
	}

	public boolean isEmpty(){
		return (current==0);
	}
	
	public boolean isFull(){
		return (current==max);
	}

	public void display(){
		for(int i=0; i<current; i++) System.out.println("['"+nArr[i].d.context+"', "+nArr[i].d.freq+"] ");
		System.out.println();
	}
}

class HuffTree{
	public HuffNode root;
	
	public HuffTree(){
		root=null;
	}

	public HuffTree(HuffNode root){
		this.root=root;
	}

	public void inOrderTransverse(){
		inOrder(root);
	}

	public void inOrder(HuffNode n){
		if(n==null) return;
		else{
			inOrder(n.left);
			n.display();
			inOrder(n.right);
		}
	}
}

public class HuffCode{
	public static void main(String [] args){
		Random rand=new Random(47);
		HuffNode huffRoot=new HuffNode(new HuffData());
		HuffNode [] nArr={new HuffNode(new HuffData('A', rand.nextDouble())),
   				  new HuffNode(new HuffData('B', rand.nextDouble())), 
				  new HuffNode(new HuffData('C', rand.nextDouble())), 
				  new HuffNode(new HuffData('D', rand.nextDouble())), 
  				  new HuffNode(new HuffData('E', rand.nextDouble())), 
				 };
		PQueue que=new PQueue(nArr.length);
		for(int i=0; i<nArr.length; i++) que.insert(nArr[i]);
		que.display();
		huffRoot=huffEstab(que);
		HuffTree huffTree=new HuffTree();
		huffTree.root=huffRoot;
		System.out.println("InOrder Transverse:");
//		huffTree.inOrder(huffRoot);
		huffTree.inOrderTransverse();
	}

	public static HuffNode huffEstab(PQueue que){
//		HuffNode t=new HuffNode(new HuffData());
		while(que.number()>1){
			HuffNode t=new HuffNode(new HuffData());
			t.left=que.remove();
			t.right=que.remove();
			t.d.context='G';
			t.d.freq=t.left.d.freq+t.right.d.freq;
			que.insert(t);
		}
		return que.remove();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值