二叉树的实现-Huffman树-摘自数据结构实现java版本(个人笔记整理)

//树节点的实现

package tree;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Stack;

public class BinTreeNode {
	private Object data;
	private int height;
	private int size;
	private BinTreeNode lChild;
	private BinTreeNode rChild;
	public BinTreeNode(){
		this(null);
	}
	public BinTreeNode(Object data){
		this.data = data;
	}
	private BinTreeNode parent;
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public BinTreeNode getlChild() {
		return lChild;
	}
	public BinTreeNode setlChild(BinTreeNode lChild) {
		BinTreeNode oldLeftChild = getlChild();
		if(hasLChild()) oldLeftChild.sever();
		if(lChild!=null){
			lChild.sever();
			this.setlChild(lChild);
			lChild.setParent(this);
			this.upDateHeight();
			this.upDateSize();
		}
		return oldLeftChild;
	}
	public BinTreeNode getrChild() {
		return rChild;
	}
	public BinTreeNode setrChild(BinTreeNode rChild) {
		BinTreeNode oldRightChild = getrChild();
		if(hasRightChild())oldRightChild.sever();
		if(rChild!=null){
			rChild.sever();
			rChild.setParent(this);
			this.setrChild(rChild);
		}
		return oldRightChild;
	}
	public BinTreeNode getParent() {
		return parent;
	}
	public void setParent(BinTreeNode parent) {
		this.parent = parent;
	}
	public boolean hasParent(){
		return parent!=null;
	}
	public boolean hasLChild(){
		return lChild!=null;
	}
	public boolean hasRightChild(){
		return rChild!=null;
	}
	public boolean isLeaf(){
		return getlChild()==null&&getrChild()==null;
	}
	public boolean isRightChild(){
		return hasParent()&& getParent().getrChild()==this;
	}
	public boolean isLeftChild(){
		return hasParent()&&getParent().getrChild()==this;
	}
	public void upDateHeight(){
		int newHeight =0;
		if(hasRightChild())newHeight=Math.max(newHeight, getrChild().getHeight()+1);
		if(hasLChild())newHeight = Math.max(newHeight, getlChild().getHeight()+1);
		if(newHeight==getHeight())return;
		setHeight(newHeight);
		//向上递归更新
		if(hasParent())getParent().upDateHeight();
	}
	public void upDateSize(){
		size = 1;
		if(hasLChild())size+=getlChild().getSize();
		if(hasRightChild())size+=getrChild().getSize();
		if(hasParent())getParent().upDateSize();
	}
	//断开与父节点的关系
	public void sever(){
		if(getParent()==null)return;
		if(isRightChild()){
			getParent().setrChild(null);}else{
				getParent().setlChild(null);
			}
		getParent().upDateHeight();
		getParent().upDateSize();
		setParent(null);
		
	}
	public Iterator PreOrder(){
		LinkedList list = new LinkedList<BinTreeNode>();
		PreOrderTravelRecusion(this,list);
		return list.iterator();
	}
	//前续递归算法
	private void PreOrderTravelRecusion(BinTreeNode root,LinkedList list){
		if(root==null) return;
	 list.add(root);
	 if(hasLChild())PreOrderTravelRecusion(getlChild(), list);
	 if(hasRightChild())PreOrderTravelRecusion(getrChild(), list);
	}
	//前续非递归算法
	private void PreOrderTravel(BinTreeNode root,LinkedList list){
		if(root==null) return;
		Stack stack =new Stack<BinTreeNode>();
		BinTreeNode p = root;
		while(p!=null){
			while(p!=null){
				list.add(p);
				if(p.hasRightChild())stack.push(p.getrChild());
				p=p.getlChild();
			}
			if(!stack.isEmpty())p=(BinTreeNode) stack.pop();
		}
	}
	//中序非递归算法
	private void inOrderTravel(BinTreeNode root,LinkedList list){
		BinTreeNode p =root;
		if(root==null)return;
		Stack stack = new Stack<BinTreeNode>();
		while(!stack.isEmpty()||p!=null){
			while(p!=null){
				stack.push(p);
				p = p.getlChild();
			}
			if(!stack.isEmpty()){
				p = (BinTreeNode) stack.pop();
				list.add(p);
				p=p.getrChild();
			}
		}
		
	}
	//后续遍历非递归
	private void postOrderTravel(BinTreeNode root,LinkedList list){
		if(root == null) return;
		BinTreeNode p = root;
		Stack stack = new Stack<BinTreeNode>();
		while(!stack.isEmpty()||p!=null){
			stack.push(p);
			if(p.hasLChild()){p=p.getlChild();
			}else{
				p = p.getrChild();
			}
		}
		if(!stack.isEmpty()){
			p = (BinTreeNode) stack.pop();
			list.add(p);
		}
		BinTreeNode top = (BinTreeNode) stack.peek();
		if(!stack.isEmpty()&&top.getrChild()==p){
			p = (BinTreeNode) stack.pop();
			list.add(p);
		}
		if(!stack.isEmpty())
		{
		p = top.getrChild();}else{
			p=null;
		}
	}
}

//Huffman节点

package tree;

public class HuffmanTreeNode extends BinTreeNode{
	private int weight;
	private String code="";
	public HuffmanTreeNode(int weight){
		this(weight,null);
	}
	public HuffmanTreeNode(int weight,Object data){
		super(data);
		this.weight=weight;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public HuffmanTreeNode getParent(){
		return (HuffmanTreeNode) super.getParent();
	}
	public HuffmanTreeNode getlChild(){
		return (HuffmanTreeNode) super.getlChild();
	}
	public HuffmanTreeNode getrChild(){
		return (HuffmanTreeNode) super.getrChild();
	}
}

//haffman的构造

package tree;

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

public class HuffmanTreeBuilder {
public static HuffmanTreeNode buildHuffmanTree(HuffmanTreeNode[] nodes){
int length = nodes.length;
List list = new ArrayList<HuffmanTreeNode>();
if(length<2){return nodes[0];}
for(int i=0;i<length;i++){
insertIntoList(nodes[i], list);
}
for(int i=0;i<length;i++){
HuffmanTreeNode min1 = (HuffmanTreeNode) list.remove(list.size()-1);
HuffmanTreeNode min2 = (HuffmanTreeNode) list.remove(list.size()-1);
HuffmanTreeNode newNode = new HuffmanTreeNode(min1.getWeight()+min2.getWeight());
newNode.setlChild(min1);
newNode.setrChild(min2);
min1.setParent(newNode);
min2.setParent(newNode);
insertIntoList(newNode, list);
}
return (HuffmanTreeNode) list.get(0);


}

//按照大小顺序插入链表中 ,具体的链表自己实现
public static void insertIntoList(HuffmanTreeNode node,List< HuffmanTreeNode> list){
for(int i=0;i<list.size();i++)
if(node.getWeight()>list.get(i).getWeight()){
list.add(node);
}
}
public static void generatorHuffmanTree(HuffmanTreeNode root){
if(root == null) return;
if(root.hasParent()){
if(root.isLeftChild()) root.setCode(root.getParent().getCode()+”0″);
if(root.isRightChild())root.setCode(root.getParent().getCode()+”1″);
}
generatorHuffmanTree(root.getlChild());
generatorHuffmanTree(root.getlChild());
}
}

点赞