java实现AVL树

public class AVLTree<T extends Comparable>{
	public T data;
	public AVLTree<T> left;
	public AVLTree<T> right;
	public int bf;
	private static boolean taller;
	
	public AVLTree(){
		
	}
	
	public AVLTree(T[] array){
		AVLTree<T> pre=new AVLTree<T>();
		for(T ele:array){
			try{
				insert(pre,pre.left,true,ele);
				this.data=pre.left.data;
				this.left=pre.left.left;
				this.right=pre.left.right;
			}catch(Exception e){
				System.out.println(e.getMessage());
			}finally{
				
			}
		}
	}
	private void LRotate(AVLTree<T> parent,AVLTree<T> current,boolean isLeft){
		AVLTree<T> right=current.right;
		current.right=right.left;
		right.left=current;
		if(isLeft) 
			parent.left=right;
		else
			parent.right=right;
	}
	
	private void RRotate(AVLTree<T> parent,AVLTree<T> current,boolean isLeft){
		AVLTree<T> left=current.left;
		current.left=left.right;
		left.right=current;
		if(isLeft)
			parent.left=left;
		else
			parent.right=left;
		}
	
	private void leftBalance(AVLTree<T> parent,AVLTree<T> current,boolean isLeft){
		AVLTree<T> left=current.left;
		switch(left.bf){
		case 1:
			RRotate(parent,current,isLeft);
			left.bf=0;
			current.bf=0;
			break;
		case -1:
			AVLTree<T> lRight=left.right;
			switch(lRight.bf){
			case 1:
				left.bf=0;
				current.bf=-1;
				break;
			case 0:
				left.bf=current.bf=0;
				break;
			case -1:
				left.bf=1;
				current.bf=0;
				break;
			}
			lRight.bf=0;
			LRotate(current,current.left,true);
			RRotate(parent,current,isLeft);
		}
	}
	
	private void rightBalance(AVLTree<T> parent,AVLTree<T> current,boolean isLeft){
		AVLTree<T> right=current.right;
		switch(right.bf){
		case -1:
			RRotate(parent,current,isLeft);
			right.bf=0;
			current.bf=0;
			break;
		case 1:
			AVLTree<T> rLeft=right.left;
			switch(rLeft.bf){
			case 1:
				right.bf=1;
				current.bf=0;
				break;
			case 0:
				right.bf=current.bf=0;
				break;
			case -1:
				right.bf=0;
				current.bf=-1;
				break;
			}
			rLeft.bf=0;
			RRotate(current,current.right,false);
			LRotate(parent,current,true);
		}
	}
	
	public boolean insert(AVLTree<T> pre,AVLTree<T> p,boolean isLeft,T ele) throws Exception{/*规定实参 pre不能为空,根节点的pre规定为data域为null的AVLTree实例*/
		if(pre==null){
			throw new Exception("Bad Parameter [pre]");
		}
		if(p==null){ 
			AVLTree<T> node=new AVLTree();
			node.data=ele;
			AVLTree<T> n=(isLeft)?(pre.left=node):(pre.right=node);/*n没有用,只是满足条件选择运算符的语法要求*/
			taller=true;
			return true;                /*不存在相同元素则插入成功*/                           
		}else{
			if(p.data.compareTo(ele)==0){
				taller=false;
				return false;/*存在相同元素返回false*/
			}
			if(p.data.compareTo(ele)>0){
				boolean isSuccess=insert(p,p.left,true,ele);
				if(!isSuccess){
					taller=false;
					return false;
				}
				if(taller){
					switch(p.bf){
					case 0: 
						p.bf=1;
						taller=true;
						break;
					case 1:
						leftBalance(pre,p,isLeft);
						taller=false;
						break;
					case -1:
						p.bf=0;
						taller=false;
						break;
					}
				}
				return true;
			}else{
				boolean isSuccess=insert(p,p.right,false,ele);
				if(!isSuccess){
					taller=false;
					return false;
				}
				if(taller){
					switch(p.bf){
					case 0: 
						p.bf=-1;
						taller=true;
						break;
					case 1:
						p.bf=0;
						taller=false;
						break;
					case -1:
						rightBalance(pre,p,isLeft);
						taller=false;
						break;
					}
				}
				return true;
			}
		}
		
	}
	
	public String inOrderTraverse(AVLTree<T> root){
		if(root!=null){
			return inOrderTraverse(root.left)+root.data.toString()+","+inOrderTraverse(root.right);
		}else
			return "";
	}
	
	public String toString() {
		String string=inOrderTraverse(this);
		string=string.substring(0, string.length()-1);
		return "AVLTree [" + string + "]";
	}

	public static void main(String[] args){
		Integer[] array=new Integer[]{4,7,9,3,1,6,5,2,8,0};
		AVLTree<Integer> avl=new AVLTree<>(array);
		System.out.println(avl);
	}
}

输出结果:

null
AVLTree [0,1,2,3,4,5,6,7,8,9]

    原文作者:AVL树
    原文地址: https://blog.csdn.net/u012334071/article/details/41317513
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞