package com.gwf.AVLTree;
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};
//int[] arr = {8, 10, 7, 6, 9 ,18,14,13,16,12};
//创建一个 AVLTree对象
AVLTree avlTree = new AVLTree();
//添加结点
for (int i = 0; i < arr.length; i++) {
avlTree.add(new Node(arr[i]));
}
//遍历
System.out.println("中序遍历");
avlTree.infixOrder(avlTree.getRoot());
System.out.println("平衡前根结点=" + avlTree.getRoot());//8
System.out.println("在平衡处理~~");
System.out.println("树的高度=" + avlTree.height(avlTree.getRoot())); //3
System.out.println("树的左子树高度=" + avlTree.height(avlTree.getRoot().left)); // 2
System.out.println("树的右子树高度=" + avlTree.height(avlTree.getRoot().right)); // 2
System.out.println("当前的根结点=" + avlTree.getRoot());//8
}
}
// 创建AVLTree
class AVLTree {
private Node root;
public Node getRoot() {
return root;
}
// 返回 以该结点为根结点的树的高度
public int height(Node root) {
if (root == null) return 0;
int left = height(root.left);
int right = height(root.right);
return Math.max(left, right) + 1;
}
// 中序遍历
public void infixOrder(Node root) {
if (root == null) return;
infixOrder(root.left);
System.out.println(root);
infixOrder(root.right);
}
//左旋转方法(根节点没变,根节点的值变了)
private void leftRotate(Node root) {
//创建新的结点,以当前根结点的值
Node newNode = new Node(root.value);
//把新的结点的左子树设置成当前结点的左子树
newNode.left = root.left;
//把新的结点的右子树设置成当前结点的右子树的左子树
newNode.right = root.right.left;
//把当前结点的值替换成右子结点的值
root.value = root.right.value;
//把当前结点的右子树设置成当前结点右子树的右子树
root.right = root.right.right;
//把当前结点的左子树(左子结点)设置成新的结点
root.left = newNode;
}
//右旋转(根节点没变,根节点的值变了)
private void rightRotate(Node root) {
Node newNode = new Node(root.value);
newNode.right = root.right;
newNode.left = root.left.right;
root.value = root.left.value;
root.left = root.left.left;
root.right = newNode;
}
// 查找要删除的结点
public Node search(Node root, int value) {
if (root == null) return null;
while (root != null) {
if (value < root.value)
root = root.left;
else if (value > root.value)
root = root.right;
else
return root;
}
return null;
}
// 查找要删除结点的父结点
public Node searchParent(Node node, int value) {
if (node == null) return null;
// 如果当前结点就是要删除的结点的父结点,就返回
if ((node.left != null && node.left.value == value) || (node.right != null && node.right.value == value)) {
return node;
} else {
// 如果查找的值小于当前结点的值, 并且当前结点的左子结点不为空
if (value < node.value && node.left != null) {
return searchParent(node.left, value); // 向左子树递归查找
} else if (value >= node.value && node.right != null) {
return searchParent(node.right, value); // 向右子树递归查找
} else {
return null; // 没有找到父结点
}
}
}
// 添加结点的方法
// 递归的形式添加结点,注意需要满足二叉排序树的要求
public void add(Node node) {
if (node == null) {
return;
}
if (root == null) {
root = node;
return;
}
Node pre = null;//需要记录父节点
Node cur = root;
while (cur != null)//定位插入的位置
{
pre = cur;//记录父节点
if (node.value < cur.value)
cur = cur.left;
else
cur = cur.right;
}
//定位到合适的页节点的空白处后,根据和父节点的大小比较插入合适的位置
if (node.value < pre.value)
pre.left = node;
else if (node.value > pre.value)
pre.right = node;
//当添加完一个结点后,如果: (右子树的高度-左子树的高度) > 1 , 左旋转
if (height(root.right) - height(root.left) > 1) {
//如果它的右子树的左子树的高度大于它的右子树的右子树的高度
if (root.right != null && height(root.right.left) > height(root.right.right)) {
//先对右子结点进行右旋转
rightRotate(root.right);
//然后在对当前结点进行左旋转
leftRotate(root); //左旋转..
} else {
//直接进行左旋转即可
leftRotate(root);
}
return; //必须要!!!
// 其实不用循环,因为每次加入之前树已经平衡,不需多层嵌套
// Node adjust = root;
// while(root.right != null && height(root.right.left) > height(root.right.right)) {
// //先对右子结点进行右旋转
// rightRotate(adjust.right);
// adjust =adjust.right;
// }
// leftRotate(root);
// return ; //必须要!!!
}
//当添加完一个结点后,如果 (左子树的高度 - 右子树的高度) > 1, 右旋转
if (height(root.left) - height(root.right) > 1) {
//如果它的左子树的右子树高度大于它的左子树的左子树的高度
if (root.left != null && height(root.left.right) > height(root.left.left)) {
//先对当前结点的左结点(左子树)->左旋转
leftRotate(root.left);
//再对当前结点进行右旋转
rightRotate(root);
} else {
//直接进行右旋转即可
rightRotate(root);
}
}
}
// 删除结点
public void delete(Node root, int val) {
if (root == null) return;
Node pre = null;//记录父结点
while (root != null)//定位到需要删除的节点
{
if (val < root.value) {
pre = root;
root = root.left;
} else if (val > root.value) {
pre = root;
root = root.right;
} else {//找到了
//①待删除的是 叶子节点
if (root.left == null && root.right == null) {
if (pre == null) //根节点
root = null;
else { //叶子结点
if (pre.left == root)
pre.left = null;
else if (pre.right == root)
pre.right = null;
}
}
//② 待删除的节点只有左孩子
else if (root.left != null && root.right == null) {
if (pre == null) //根节点
root = root.left;
else {
if (pre.left == root)//待删除的本身是一个左孩子
pre.left = root.left;
else if (pre.right == root)
pre.right = root.left;
}
//② 待删除的节点只有右孩子
} else if (root.left == null && root.right != null) {
if (pre == null) {//根节点
root = root.right;
} else {
if (pre.left == root)//待删除的本身是一个右孩子
pre.left = root.right;
else if (pre.right == root) {
pre.right = root.right;
}
}
}//③待删除的节点即有左孩子又有右孩子
// 方法:得到待删除节点右子树的最小值,该最小值与待删除节点进行“ 值 ”交换,删除该最小值位置处的节点
else {
//求待删除节点的后继节点,即待删除节点的右孩子的最小值(找到的后继节点肯定没有左孩子!!!)
//TreeNode node = getMin(root.right); //方法一
//TreeNode rightMin = postNode(root); //方法二
//TreeNode rightMinP = rightMin.parent;
//方法三
Node rightMin = root.right;
Node rightMinP = root;//rightMin.parent
while (rightMin.left != null) {
rightMinP = rightMin;
rightMin = rightMin.left;
}
int temp = rightMin.value;//值交换
rightMin.value = root.value;
root.value = temp;
//删除node位置的节点,此时此位置的值已是待删节点的值(只有一个节点,或者只有右子节点)
if (rightMinP.left == rightMin)
rightMinP.left = rightMin.right;//right有就有,没有就是null
else if (rightMinP.right == rightMin)
rightMinP.right = rightMin.right;
}
break;
}
}
}
}
// 创建Node结点
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node [value=" + value + "]";
}
}
手写AVL
最新推荐文章于 2025-05-16 20:57:46 发布