平衡二叉树
平衡二叉树又叫平衡二叉搜索树,可以保证查询效率较高。 特点一棵树它左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
右旋转
构建二叉查找树:  构成的二叉树的形状:  此时,左右子树高度差为为2,根节点值为10  这时二叉树:左子树高度-右子树的高度=2 不满足平衡二叉树,为了让二叉树成为平衡二叉树就需要进行右旋转降低左子树的高度,增加右子树的深度。 右旋转的步骤:(紫色为当前结点,红色为新结点) 
private void rightRotate(){
Node newNode=new Node(this.value);
newNode.right=this.right;
newNode.left=this.left.right;
this.value=this.left.value;
this.left=this.left.left;
this.right=newNode;
}
执行右旋转完后,左右子树高度差为为0,根节点值为8,满足平衡二叉树 
左旋转
构建二叉查找树:  构成的二叉树的形状:   此时,左右子树高度差为为2,根节点值为4  这时二叉树:右子树高度-左子树的高度=2 不满足平衡二叉树,为了让二叉树成为平衡二叉树就需要进行左旋转降低右子树的高度,增加左子树的深度。 左旋转的步骤:(紫色为当前结点,红色为新结点) 
private void leftRotate(){
Node newNode=new Node(this.value);
newNode.left=this.left;
newNode.right=this.right.left;
this.value=this.right.value;
this.right=this.right.right;
this.left=newNode;
}
执行左旋转完后,左右子树高度差为为0,根节点值为6,满足平衡二叉树 
双旋转
拿数据为例  此时构建的二叉查找树还会有这种情况  此时二叉树满足右旋转,旋转完后,二叉树左右子树高度差还是大于1  就要用到双旋转,什么条件执行双旋转呢,拿开始为右旋转为例当 (左子树的高度 - 右子树的高度) > 1并且它的左子树的右子树高度大于它的左子树的高度,就先对当前结点的左结点(左子树)->左旋转,再对当前结点进行右旋转,反之开始为左旋转。  执行双旋转完后,左右子树高度差为为0,根节点值为8,满足平衡二叉树 
代码汇总
package avl;
public class AVLTreeDemo {
public static void main(String[] args) {
int[] arr = { 10, 11, 7, 6, 8, 9 };
AVLTree avlTree=new AVLTree();
for (int i=0;i<arr.length;i++){
avlTree.add(new Node(arr[i]));
}
System.out.println("二叉树的高度="+(avlTree.getRoot().height()-1));
System.out.println("二叉树的左子树高度=" + (avlTree.getRoot().leftHeight()-1));
System.out.println("二叉树的右子树高度=" + (avlTree.getRoot().rightHeight()-1));
System.out.println("当前的根结点值=" + avlTree.getRoot());
}
}
class AVLTree{
private Node root;
public Node getRoot(){
return root;
}
public void add(Node node){
if(root==null){
root=node;
}else{
root.add(node);
}
}
}
class Node{
int value;
Node left;
Node right;
public Node(int value){
this.value =value;
}
public int leftHeight(){
if(left==null){
return 0;
}
return left.height();
}
public int rightHeight() {
if (right == null) {
return 0;
}
return right.height();
}
public int height(){
return Math.max(left==null?0:left.height(),right==null?0:right.height())+1;
}
private void leftRotate(){
Node newNode=new Node(this.value);
newNode.left=this.left;
newNode.right=this.right.left;
this.value=this.right.value;
this.right=this.right.right;
this.left=newNode;
}
private void rightRotate(){
Node newNode=new Node(this.value);
newNode.right=this.right;
newNode.left=this.left.right;
this.value=this.left.value;
this.left=this.left.left;
this.right=newNode;
}
public void add(Node node){
if(node==null){
return;
}
if(node.value<this.value){
if(this.left==null){
this.left=node;
}else{
this.left.add(node);
}
}else{
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
if(rightHeight() - leftHeight() > 1) {
if(right != null && right.leftHeight() > right.rightHeight()) {
right.rightRotate();
leftRotate();
} else {
leftRotate();
}
return ;
}
if(leftHeight() - rightHeight() > 1) {
if(left != null && left.rightHeight() > left.leftHeight()) {
left.leftRotate();
rightRotate();
} else {
rightRotate();
}
}
}
@Override
public String toString() {
return "Node [value=" + value + "]";
}
}
|