package com.atguigu.avl;
/**
* @author
* @create 2022-07-04-9:06
*/
public class AVLTreeDemo {
public static void main(String[] args) {
//int[] arr = {4, 3, 6, 5, 7, 8};//测试左旋转用
//int[] arr = {10, 12, 8, 9, 7, 6};//测试右旋转用
int[] arr = {10, 11, 7, 6, 8, 9};//测试双旋转用
//双旋转本质上可以这么理解:
/*
如果想左旋转必须保证当前节点及所有右子节点对应的右子树高度都大于左子树高度
如果想右旋转必须保证当前节点及所有左子节点对应的左子树高度都大于右子树高度
否则就需要进行双旋转,且双旋转时肯定是一个左旋一个右旋
*/
//创建一个AVLTree对象
AVLTree avlTree = new AVLTree();
for (int i = 0; i < arr.length; i++) {
avlTree.add(new Node(arr[i]));
}
System.out.println("中序遍历:");
avlTree.infixOrder();
System.out.print("平衡处理后,当前树的高度为:");
System.out.println(avlTree.getRoot().height());//4 3
System.out.println("当前树左子树的高度为:" + avlTree.getRoot().leftHeight());//1 2
System.out.println("当前树右子树的高度为:" + avlTree.getRoot().rightHeight());//3 2
System.out.println("当前根节点为:" + avlTree.getRoot());
System.out.println("根节点的左子节点:" + avlTree.getRoot().right);
}
}
//创建AVLTree
class AVLTree {
private Node root;
public Node getRoot() {
return root;
}
/**
* @param node 传入的节点,当做二叉排序树的根节点
* @return 返回以node为根节点的二叉排序树的最小值,并删除。
*/
public int delRightTreeMin(Node node) {
Node target = node;
while (target.left != null) {
target = target.left;
}
//此时target指向最小节点
delNode(target.value);
return target.value;
}
//删除节点
public void delNode(int value) {
if (root == null) {
return;
} else {
//考虑单个节点的问题
if (root.left == null && root.right == null && root.value == value) {
root = null;
return;
}
//找到要删除的节点
Node targetNode = search(value);
if (targetNode == null) {//没有找到
return;
}
//找到要删除节点的父节点
Node parentNode = searchParent(value);
//此种写法要单独考虑父节点为空的情况
if (parentNode == null) {
//情况一:targetNode是叶子节点情况
if (targetNode.left == null && targetNode.right == null) {
root = null;
return;
}
//情况二:targetNode有两颗子树的情况
else if (targetNode.left != null && targetNode.right != null) {
int rightTreeMinValue = delRightTreeMin(targetNode.right);
targetNode.value = rightTreeMinValue;
return;
}
//情况三:targetNode有一颗子树情况
else {
if (targetNode.left != null) {
root = targetNode.left;
}
if (targetNode.right != null) {
root = targetNode.right;
}
return;
}
}
if (parentNode.left != null && parentNode.left.value == value) {//targetNode是父节点的左子节点
//情况一:targetNode是叶子节点情况
if (targetNode.left == null && targetNode.right == null) {
parentNode.left = null;
}
//情况二:targetNode有两颗子树的情况
if (targetNode.left != null && targetNode.right != null) {
int rightTreeMinValue = delRightTreeMin(targetNode.right);
targetNode.value = rightTreeMinValue;
}
//情况三:targetNode有一颗子树情况
if ((targetNode.left == null && targetNode.right != null) || (targetNode.right == null && targetNode.left != null)) {
if (targetNode.left != null) {//说明targetNode的子节点是左子节点
parentNode.left = targetNode.left;
}
if (targetNode.right != null) {//说明targetNode的子节点是右子节点
parentNode.left = targetNode.right;
}
}
} else if (parentNode.right != null && parentNode.right.value == value) {//targetNode是父节点的右子节点
//情况一:targetNode是叶子节点情况
if (targetNode.left == null && targetNode.right == null) {
parentNode.right = null;
}
//情况二:targetNode有两颗子树的情况
if (targetNode.left != null && targetNode.right != null) {
int rightTreeMinValue = delRightTreeMin(targetNode.right);
targetNode.value = rightTreeMinValue;
}
//情况三:targetNode有一颗子树情况
if ((targetNode.left == null && targetNode.right != null) || (targetNode.right == null && targetNode.left != null)) {
if (targetNode.left != null) {//说明targetNode的子节点是左子节点
parentNode.right = targetNode.left;
}
if (targetNode.right != null) {
parentNode.right = targetNode.right;
}
}
}
}
}
//查找要删除的节点
public Node search(int value) {
if (root == null) {
System.out.println("此树为空,查找失败");
return null;
} else {
return root.search(value);
}
}
//查找要删除节点的父节点
public Node searchParent(int value) {
if (root == null) {
System.out.println("此树为空,查找失败");
return null;
} else {
return root.searchParent(value);
}
}
//添加节点
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
//中序遍历
public void infixOrder() {
if (root == null) {
System.out.println("此树为空,无法遍历~");
} else {
root.infixOrder();
}
}
}
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
//返回左子树的高度
public int leftHeight() {
if (left == null) {
return 0;
} else {
return left.height();
}
}
//返回右子树高度
public int rightHeight() {
if (right == null) {
return 0;
} else {
return right.height();
}
}
//返回以当前节点为根节点的树的高度
public int height() {
return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
}
//左旋转方法,在添加节点时右子树比左子树高时使用
private void leftRotate() {
//1.创建新节点,以当前根节点的值、
Node newNode = new Node(value);
//2.新节点的left指向当前节点的left
newNode.left = this.left;
//3.新节点的right指向当前节点右子树的左子树
newNode.right = this.right.left;
//4.把当前节点的值替换成右子节点的值
this.value = this.right.value;
//5.当前节点的右子树指向当前节点右子树的右子树
this.right = this.right.right;
//6.当前节点的左子树指向新节点
this.left = newNode;
}
//右旋转方法,在添加节点时左子树比右子树高时使用
public void rightRotate() {
//1.创建新节点,值为当前节点
Node newNode = new Node(value);
//2.新节点的左子节点指向当前节点的左子树的右子树
newNode.left = this.left.right;
//3.新节点的右子树指向当前节点的右子树
newNode.right = this.right;
//4.当前节点的值替换成左子节点的值
this.value = this.left.value;
//5.当前节点左子节点指向当前节点左子节点的左子节点
this.left = this.left.left;
//6.当前节点右子节点指向新节点
this.right = newNode;
}
//查找删除节点的方法
/**
* @param value 查找的值
* @return 返回该值对应的节点
*/
public Node search(int value) {
if (value == this.value) {//找到了
return this;
} else if (value < this.value) {//查找的值比当前的值小,向左子树递归查找
if (this.left == null) {//判断左子树是否为空,如果为空则返回null
return null;
}
return this.left.search(value);
} else {//查找的值比当前的值大,向右子树递归查找
if (this.right == null) {
return null;
}
return this.right.search(value);
}
}
//查找要删除节点的父节点
/**
* @param value 要查找的值
* @return 返回该值对应节点的父节点
*/
public Node searchParent(int value) {
if ((this.left != null && value == this.left.value) || (this.right != null && value == this.right.value)) {
//找到了
return this;
} else if (value < this.value && this.left != null) {//应该向左子树递归查找
return this.left.searchParent(value);
} else if (value > this.value && this.right != null) {//应该向右子树递归查找
return this.right.searchParent(value);
} else {
return null;
}
}
//添加节点
//递归的方式添加,主要要满足二叉排序树的要求:小的在节点左侧,大的在节点右侧
public void add(Node node) {
if (node == null) {
return;
}
if (node.value > this.value) {//添加节点比当前节点大,挂到右侧
if (this.right == null) {
this.right = node;
} else {
//如果不是空则继续向右递归添加
this.right.add(node);
}
} else {//添加节点比当前节点小,挂到左侧
if (this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
}
//当添加完一个结点后,如果 右子树高度-左子树高度 >1 =》进行左旋转
if (this.rightHeight() - this.leftHeight() > 1) {
//如果当前节点的右子节点的左子树的高度大于当前节点的右子节点的右子树高度,要先对当前节点的右子节点进行右旋转
if (this.right != null && this.right.leftHeight() > this.right.rightHeight()) {
//对当前节点的右子节点进行右旋
this.right.rightRotate();
//对当前节点进行左旋
this.leftRotate();
} else {//直接进行左旋转即可
this.leftRotate();
}
return;//必须要!否则会继续执行下面语句
}
当添加完一个结点后,如果 左子树高度-右子树高度 >1 =》进行右旋转
if (this.leftHeight() - this.rightHeight() > 1) {
//如果当前节点的左子节点的右子树的高度大于当前节点的左子节点的左子树高度,要先对当前节点的左子节点进行左旋转
if (this.left != null && this.left.rightHeight() > this.left.leftHeight()) {
this.left.leftRotate();//对当前节点的左子节点进行左旋转
this.rightRotate();//对当前节点进行右旋转
} else {//直接进行右旋转即可
this.rightRotate();
}
}
}
//中序遍历
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
}
数据结构与算法学习之平衡二叉树(AVL树)
最新推荐文章于 2022-11-27 16:07:01 发布