package avl;
public class AVLTreeDemo {
public static void main(String[] args) {
// int[] arr = { 4, 3, 6, 5, 7, 8 };
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.infixOrder();
System.out.println("没平衡前:");
System.out.println("树的高度=" + avlTree.root.height());
System.out.println("左子树的高度=" + avlTree.root.leftHeight());
System.out.println("右子树的高度=" + avlTree.root.rightHeight());
System.out.println(avlTree.root);
}
}
class AVLTree {
Node root;
// 找要找的节点
public Node search(int value) {
if (root == null) {
return null;
} else {
return root.search(value);
}
}
// 找要找的节点的父节点
public Node searchParent(int value) {
if (root == null) {
return null;
} else {
return root.searchParent(value);
}
}
// 找到左子树的最大值,返回这个值并删除
public int delLeftTreeMax(Node node) {
Node target = node;
while (target.right != null) {
target = target.right;
}
// target指向了最大节点
delNode(target.value);
return target.value;
}
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 {
Node targetNode = search(value);
if (targetNode == null) {
return;
}
if (root.left == null && root.right == null) {
root = null;
return;
}
Node parentNode = searchParent(value);
// 删除叶子节点
if (targetNode.left == null && targetNode.right == null) {
if (parentNode.left != null && parentNode.left.value == targetNode.value) {
parentNode.left = null;
} else if (parentNode.right != null && parentNode.right.value == targetNode.value) {
parentNode.right = null;
}
// 删除有两颗子树的节点
} else if (targetNode.left != null && targetNode.right != null) {
int maxNode = delLeftTreeMax(targetNode.left);
targetNode.value = maxNode;
// 删除只有一颗子树的节点
} else {
// 如果是parent的左子节点
if (parentNode != null) {
if (parentNode.left != null && parentNode.left.value == targetNode.value) {
if (targetNode.left != null) {
parentNode.left = targetNode.left;
} else {
parentNode.left = targetNode.right;
}
// 如果是parent的右子节点
} else {
if (targetNode.left != null) {
parentNode.right = targetNode.left;
} else {
parentNode.right = targetNode.right;
}
}
} else {
if (targetNode.left != null) {
root = targetNode.left;
} else {
root = targetNode.right;
}
}
}
}
}
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 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;
}
// 左旋
public void leftRotate() {
Node newNode = new Node(value);
newNode.left = left;
newNode.right = right.left;
value = right.value;
right = right.right;
left = newNode;
}
// 右旋
public void rightRotate() {
Node newNode = new Node(value);
newNode.left = left.right;
newNode.right = right;
value = left.value;
left = left.left;
right = newNode;
}
// 找要找的节点
public Node search(int value) {
if (value == this.value) {
return this;
} else if (value < this.value) {
if (this.left == null) {
return null;
}
return this.left.search(value);
} else {
if (this.right == null) {
return null;
}
return this.right.search(value);
}
}
// 找要找的节点的父节点
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.value > 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.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();
}
} else if(leftHeight()-rightHeight()>1) {
if(left!=null&&left.rightHeight()>left.leftHeight()) {
left.leftRotate();
rightRotate();
}else {
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 + "]";
}
}
平衡排序二叉树的实现java
最新推荐文章于 2024-07-24 17:27:32 发布