- 平衡二叉树也叫平衡二叉搜索树(Self-balancing binary search tree)又被称为 AVL 树, 可以保证查询效率较高。
- 具有以下特点:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过 1,并且左右两个子树都是一棵 平衡二叉树。平衡二叉树的常用实现方法有红黑树、AVL、替罪羊树、Treap、伸展树等。
public class AVLTreeDemo {
public static void main(String[] args) {
int[] arr = { 10, 11, 7, 6, 8, 9 };
BinaryTree binaryTree = new BinaryTree();
for (int i = 0; i < arr.length; i++) {
binaryTree.add(new Node(arr[i]));
}
binaryTree.infixOrder();
System.out.println("在平衡处理~~");
System.out.println("树的高度=" + binaryTree.getRoot().height());
System.out.println("树的左子树高度=" + binaryTree.getRoot().leftHeight());
System.out.println("树的右子树高度=" + binaryTree.getRoot().rightHeight());
System.out.println("当前的根结点=" + binaryTree.getRoot());
}
}
class BinaryTree {
private Node root;
public Node getRoot() {
return 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 delRightTreeMin(Node node) {
Node target = node;
while (target.left != null) {
target = target.left;
}
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 parent = searchParent(value);
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.value == value) {
parent.left = null;
} else if (parent.right != null && parent.right.value == value) {
parent.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) {
int min = delRightTreeMin(targetNode.right);
targetNode.value = min;
} else {
if (targetNode.left != null) {
if (parent != null) {
if (parent.left.value == value) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
root = targetNode.left;
}
} else {
if (parent != null) {
if (parent.left.value == value) {
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
} 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;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
public int height(){
return Math.max(left == null? 0 : left.height(), right == null ? 0 : right.height()) + 1;
}
public int leftHeight() {
if (left == null) {
return 0;
}else {
return left.height();
}
}
public int rightHeight() {
if (right == null) {
return 0;
}else {
return right.height();
}
}
private void leftRotate(){
Node newNode = new Node(this.value);
newNode.left = this.left;
newNode.right = this.right.left;
this.value = 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 Node search(int value) {
if (this.value == value) {
return this;
}else if (this.value > 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.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 add(Node node) {
if (node.value < this.value) {
if (this.left == null) {
this.left = node;
}else {
this.left.add(node);
}
}
if (node.value > this.value) {
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();
}
leftRotate();
return;
}
if (leftHeight() - rightHeight() > 1) {
if (left != null && left.rightHeight() > left.leftHeight()) {
left.leftRotate();
rightRotate();
}
rightRotate();
return;
}
}
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this.value);
if (this.right != null) {
this.right.infixOrder();
}
}
}