package Tree.ADT.JZJ.AVLTree;
import java.nio.BufferUnderflowException;
public class AvlTree<AnyType extends Comparable<? super AnyType>> {
/**
* 定义节点
* @param <AnyType>
*/
public static class AvlNode<AnyType> {
AvlNode(AnyType theElement){
this(theElement,null,null);
}
AvlNode(AnyType theElement,AvlNode<AnyType> lt,AvlNode<AnyType> rt){
element=theElement;left=lt;right=rt;height=0;
}
AnyType element;
AvlNode<AnyType> left;
AvlNode<AnyType> right;
int height;
}
private AvlNode<AnyType> root;
public boolean isEmpty(){
return root==null;
}
// 未给出 findMax 这个不难,练练手呗
public AnyType findMin(){
if (isEmpty()) throw new BufferUnderflowException();
return findMin(root).element;
}
private AvlNode<AnyType> findMin(AvlNode<AnyType> t){
if (t==null)
return null;
else if (t.left==null)
return t;
return findMin(t.left);
}
public AvlTree(){
root=null;
}
// 在树中,空树的高度是-1
private int height(AvlNode<AnyType> t){
return t==null?-1:t.height;
}
/**
* 插入方法
* @param x 需要插入的节点
* @param t
* @return
*/
private AvlNode<AnyType> insert(AnyType x,AvlNode<AnyType> t){
if (t==null)
return new AvlNode<>(x,null,null);
int compareResult=x.compareTo(t.element);
if (compareResult<0)
t.left=insert(x,t.left);
else if (compareResult>0)
t.right=insert(x,t.right);
else
;
// 插入完成后需要保持树的平衡
return balance(t);
}
// 平衡的阈值,1
private static final int ALLOWED_IMBALANCE=1;
/**
* 平衡 旋转
* @param t 发生了不平衡的点
* @return
*/
private AvlNode<AnyType> balance(AvlNode<AnyType> t){
if (t==null)
return t;
if ((height(t.left)-height(t.right))>ALLOWED_IMBALANCE) {
if (height(t.left) >= height(t.left.right))
t = rotateWithLeftChild(t);
else
t = doubleWithLeftChild(t);
}else if (height(t.right)-height(t.left)>ALLOWED_IMBALANCE){
if (height(t.right.right)>=height(t.right.left))
t=rotateWithRightChild(t);
else
t=doubleWithRightChild(t);
}
t.height=Math.max(height(t.left),height(t.right))+1;
return t;
}
/**
* 情况1 左儿子的左子树
* @param t2
* @return
*/
private AvlNode<AnyType> rotateWithLeftChild(AvlNode<AnyType> t2){
AvlNode<AnyType> t1=t2.left;
t2.left=t1.right;
t1.right=t2;
t2.height=Math.max(height(t2.left),height(t2.right))+1;
t1.height=Math.max(height(t1.left),height(t1.right))+1;
return t1;
}
/**
* 情况4 右儿子的右子树
* @param t2
* @return
*/
private AvlNode<AnyType> rotateWithRightChild(AvlNode<AnyType> t2){
AvlNode<AnyType> t1=t2.right;
t2.right=t1.left;
t2.left=t1;
t2.height=Math.max(height(t2.left),height(t2.right))+1;
t1.height=Math.max(height(t1.left),height(t1.right))+1;
return t1;
}
/**
* 情况2 左儿子的右子树
* @param ttL
* @return
*/
private AvlNode<AnyType> doubleWithLeftChild(AvlNode<AnyType> ttL){
ttL.left=rotateWithRightChild(ttL.left);
return rotateWithLeftChild(ttL);
}
/**
* 情况3 右儿子的左子树
* @param ttR
* @return
*/
private AvlNode<AnyType> doubleWithRightChild(AvlNode<AnyType> ttR){
ttR.right=rotateWithLeftChild(ttR.right);
return rotateWithRightChild(ttR);
}
private AvlNode<AnyType> remove(AnyType x,AvlNode<AnyType> t){
if (t==null)
return t;
int compareResult=x.compareTo(t.element);
if (compareResult<0)
t.left=remove(x,t.left);
else if (compareResult>0)
t.right=remove(x,t.right);
else if (t.left!=null && t.right!=null){
t.element=findMin(t.right).element;
t.right=remove(t.element,t.right);
}
else
t=(t.left!=null)?t.left:t.right;
return balance(t);
}
}
JZJ,基中基,基础中的基础
原理分析如下:https://blog.csdn.net/CDTU_stu1_2016_Lx/article/details/105393920