package com.wqc.avl;
/**
* @author 高瞻远瞩
* @version 1.0
* 演示平衡二叉树(ALV)--》排序二叉树的改进 当某个结点的左右子树高度之差>1时 需要通过旋转的方法将排序二叉树转换为ALV树
*/
public class AVlTreeDemo {
public static void main(String[] args) {
int[] arr = {4, 3, 6, 5, 7, 8,100,35,-100,-30,6,30,990};
AVLTree avlTree = new AVLTree();
for (int i = 0; i < arr.length; i++) {
avlTree.add(new Node(arr[i]));
}
avlTree.infixOrder();
int height = avlTree.root.height();
System.out.println(" 递归求当前结点的高度=" + height + " 左子树的高度=" + avlTree.root.leftHeight()
+ " 右子树的高度=" + avlTree.root.rightHeight() + "\n此时它的左结点=" + avlTree.root.left
+ " 右节点=" + avlTree.root.right);
// int[] arr = {10, 12, 8, 9, 7, 6};
// 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]));
// }
// avlTree.infixOrder();
// System.out.println("非递归当前结点的高度=" + avlTree.root.getHeight()
// + " 左子树的高度=" + avlTree.root.leftHeight() + " 右子树的高度=" + avlTree.root.rightHeight()
// + "\n此时它的左结点=" + avlTree.root.left + " 右节点=" + avlTree.root.right);
}
}
class AVLTree {
Node root;
//add
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
//中序遍历
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("平衡二叉树为空");
}
}
}
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
//返回该结点以根节点树的高度
public int height() {
return Math.max(this.left == null ? 0 : this.left.height(), this.right == null ? 0 : this.right.height()) + 1;
}
//返回该节点左子树的高度
public int leftHeight() {
if (this.left == null) {
return 0;
} else {
return this.left.height();
}
}
//返回该节点右子树的高度
public int rightHeight() {
if (this.right == null) {
return 0;
} else {
return this.right.height();
}
}
//当此节点的右子树的高度 - 左子树的高度 > 1时--》左旋转的方法-->把一颗二叉排序树转换成平衡二叉树
private void leftRotate() {
//1 创建一个新的结点 newNode(以不平衡的这个结点创建)
Node newNode = new Node(this.value);
//2 把新节点的左子树设置为当前结点的左子树
newNode.left = this.left;
//3 把新节点的右子树设置为当前结点的右子树的左子树
newNode.right = this.right.left;
//4 把当前结点的值换为右子节点的值
this.value = this.right.value;
//5 把当前结点的右子树设置成右子树的右子树
this.right = this.right.right;
//6 把当前结点的左子树设置为新节点
this.left = newNode;
}
//当左子树的高度 - 右子树的高度 > 1 -- 》右旋转的方法
public void rightRotate() {
//1 创建一个新的结点 newNode(以不平衡的这个结点创建)
Node newNode = new Node(this.value);
//2 把新节点的右子树设置为当前结点的右子树
newNode.right = this.right;
//3 把新节点的左子树设置为当前结点的左子树的右子树
newNode.left = this.left.right;
//4 把当前结点的值换为左子节点的值
this.value = this.left.value;
//5 把当前结点的左子树设置成左子树的左子树
this.left = this.left.left;
//6 把当前结点的右子树设置为新节点
this.right = newNode;
}
//添加结点到排序二叉树
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();
return;//双旋转后就直接return 防止继续进行以下代码
}
//回溯的时候 对当前结点进行判断是否失衡 即左子树的高度 - 右子树的高度 大于1 说明失衡需要进行右旋转
if (this.leftHeight() - this.rightHeight() > 1) {
//当进行右旋转时
//如果旋转结点的左子节点的右子树的高度大于左子树的高度 需要对左子节点进行左旋 然后再对当前结点进行右旋转
if(this.left != null && this.left.rightHeight() > this.left.leftHeight()){
this.left.leftRotate();
}
this.rightRotate();
}
}
/**
* 查找要删除结点
*
* @param value 要找到结点的值
* @return 返回的是要删除的结点
*/
public Node search(int value) {
if (this.value == value) {
return this;
} else if (value < this.value) {
if (this.left != null) {
return this.left.search(value);
} else {
return null;
}
} else {
if (this.right != null) {
return this.right.search(value);
} else {
return null;
}
}
}
/**
* 查找要删除结点的父节点
*
* @param value 要找到结点的值
* @return 返回要删除结点的父节点 如果没有就返回null
*/
public Node searchParent(int value) {
if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
return this;
} else if (this.left != null && value < this.value) {
return this.left.searchParent(value);//向左递归查找
} else if (this.right != null && value >= this.value) {//向右递归查找
// 等于是因为可能有重复的值 也可能是根结点 继续向下递归只能返回空
return this.right.searchParent(value);
} else {
return null;
}
}
//中序遍历
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 +
'}';
}
}
java实现平衡二叉树(ALV)---------参考视频 韩顺平《java数据结构和算法》
于 2022-06-17 10:57:27 首次发布