# AVL树插入删除

AVL树是一种平衡二叉树，其每个节点的左右子树高度最多差1，空树高度定为－1，当左右的高度超过1，即失去了平衡，不是AVL树了。

private static class AVLNode<AnyType>{
AVLNode (AnyType element)
{this(element ,null,null);}
AVLNode(AnyTape element,AVLNode<AnyType> left,AVLNode<AnyType> right){
this.element = element;
this.left = left;
this.right = right;
}
AnyTape element;
AVLNode<AnyType> left;
AVLNode<AnyType> right;
int height;
}

1.对于ｔ的左儿子的左子树进行插入－＞单旋转

2.对于ｔ的左儿子的右子树进行插入－＞双旋转

3.对于ｔ的右儿子的左子树进行插入－＞双旋转

4.对于ｔ的右儿子的右子树进行插入－＞单旋转

private int height(AVLNode<AnyType> t){
return t == null ? -1 : t.height;
}

1.左－左：单旋,右旋

private AVLNode<AnyType> rotateRight(AVLNode<AnyType> k2){
AVLNode<AnyType> k1 = k2.left;
k2.left = k1.right;
k2.height = Maht.max(height(k2.left),height(k2.right))+1;
k1.height = Maht.max(height(k1.left),k2.height)+1;
return k1;
}
2.左－右：双旋转,先左后右

private AVLNode<AnyType> doubleRight(AVLNode<AnyType> k3){
k3.left = rotateLeft(k3.left);
return rotateRight(k3);
}


3.右－左：双旋转,先右后左

private AVLNode<AnyType> doubleLeft(AVLNode<AnyType> k3){
k3.right = rotateRightk3.right);
return rotateLeft(k3);
}

4.右－右：单旋，左旋

private AVLNode<AnyType> rotateLeft(AVLNode<AnyType> k2){
AVLNode<AnyType> k1 = k2.left;
k2.left = k1.right;
k2.height = Maht.max(height(k2.left),height(k2.right))+1;
k1.height = Maht.max(height(k1.left),k2.height)+1;
return k1;
}

private AVLNode<AnyType> insert(AnyType x,AVLNode<AnyType>t){
if(t == null)
return new AVLNode<>(x,null,null);
int result = x.compareTo(t.element);
if( result < 0)		//如果X小于节点值，则在左子树插入，递归更新
t.left = insert(x,t.left);
else if( result > 0)	//如果X大于节点值，则在右子树插入，递归
t.right = insert(x,t.right);
return balance(t);
}
private static final int balance_num = 1;
private AVLNode<AnyType> balance(AVLNode<AnyType>t){
if(t == null)
return t;
if(height(t.left) - height(right) > balance_num){
if(height(t.left.left) > height(t.left.right))  //左-左
t = rotateRight(t);
else
t = doubleRight(t);	//左-右
}else{
if(height(t.right.right) > height(t.right.left))	//右-左
t = rotateLeft(t);
else
t = doubleLeft(t);	//右-左
}

t.height = Math.max(height(t.left),height(t.right))+1;
return t;
}


private AVLNode<AnyType> remove(AnyType x,AVLNode<AnyType>t){
if(t == null)
return new AVLNode<>(x,null,null);
int result = x.compareTo(t.element);
if( result < 0)		//如果X小于节点值，则在左子树删除，递归更新
t.left = insert(x,t.left);
else if( result > 0)	//如果X大于节点值，则在右子树删除，递归
t.right = insert(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);
}


#### AVL树的插入与删除

2016-05-25 11:00:23

#### avl树的插入删除

2008年04月03日 14KB 下载

#### AVL树的插入与删除操作

2016-07-22 20:30:19

#### AVL树插入、删除的分析与实现

2014-07-10 10:19:23

#### AVL树的插入与删除---Java实现

2015-07-16 15:57:14

#### AVL树的插入与删除(均为递归实现)

2016-09-21 21:22:41

#### 平衡二叉树(AVL)的插入和删除详解(上)

2012-08-22 19:02:16

#### AVL树的插入与删除，重点是四种旋转

2015-12-03 15:01:13

#### AVL树的插入_删除操作实现~

2011-08-08 10:04:02

#### 平衡二叉查找树（AVL）的查找、插入、删除

2014-09-18 10:46:20