平衡二叉树(Balanced Binary Tree)是一种特殊的二叉搜索树,它保持树的高度平衡,从而确保各种操作的时间复杂度始终为O(log n)。在平衡二叉树中,任何节点的左子树和右子树的高度差不超过1。
public class BalancedBinaryTree {
Node root;
// 获取节点的高度
int height(Node node) {
if (node == null){
return 0;
}
return node.height;
}
// 获取两个数中的较大值
int max(int a, int b) {
return Math.max(a, b);
}
// 右旋转
Node rightRotate(Node y) {
Node x = y.left;
Node T2 = x.right;
x.right = y;
y.left = T2;
y.height = max(height(y.left), height(y.right)) + 1;
x.height = max(height(x.left), height(x.right)) + 1;
return x;
}
// 左旋转
Node leftRotate(Node x) {
Node y = x.right;
Node z = y.left;
y.left = x;
x.right = z;
x.height = max(height(x.left), height(x.right)) + 1;
y.height = max(height(y.left), height(y.right)) + 1;
return y;
}
// 获取节点的平衡因子
int getBalance(Node node) {
if (node == null){
return 0;
}
return height(node.left) - height(node.right);
}
// 插入节点
Node insert(Node node, int key) {
if (node == null)
return new Node(key);
if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
else
return node;
node.height = 1 + max(height(node.left), height(node.right));
int balance = getBalance(node);
// 左旋转
if (balance > 1 && key < node.left.key)
return rightRotate(node);
// 右旋转
if (balance < -1 && key > node.right.key)
return leftRotate(node);
// 左右旋转
if (balance > 1 && key > node.left.key) {
node.left = leftRotate(node.left);
return rightRotate(node);
}
// 右左旋转
if (balance < -1 && key < node.right.key) {
node.right = rightRotate(node.right);
return leftRotate(node);
}
return node;
}
// 对外暴露的插入方法
void insert(int key) {
root = insert(root, key);
}
// 中序遍历
void inorder() {
inorderRec(root);
}
// 中序遍历递归方法
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}
// 搜索节点
Node search(Node node, int key) {
if (node == null || node.key == key)
return node;
if (key < node.key)
return search(node.left, key);
else
return search(node.right, key);
}
// 对外暴露的搜索方法
Node search(int key) {
return search(root, key);
}
// 删除节点
Node delete(Node root, int key) {
if (root == null) {
return root;
}
if (key < root.key) {
root.left = delete(root.left, key);
} else if (key > root.key) {
root.right = delete(root.right, key);
} else {
if (root.left == null || root.right == null) {
Node temp = null;
if (temp == root.left) {
temp = root.right;
} else {
temp = root.left;
}
if (temp == null) {
temp = root;
root = null;
} else {
root = temp;
}
} else {
Node temp = minValueNode(root.right);
root.key = temp.key;
root.right = delete(root.right, temp.key);
}
}
if (root == null) {
return root;
}
root.height = max(height(root.left), height(root.right)) + 1;
int balance = getBalance(root);
// 左旋转
if (balance > 1 && getBalance(root.left) >= 0) {
return rightRotate(root);
}
// 右旋转
if (balance < -1 && getBalance(root.right) <= 0) {
return leftRotate(root);
}
// 左右旋转
if (balance > 1 && getBalance(root.left) < 0) {
root.left = leftRotate(root.left);
return rightRotate(root);
}
// 右左旋转
if (balance < -1 && getBalance(root.right) > 0) {
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
// 对外暴露的删除方法
void delete(int key) {
root = delete(root, key);
}
// 寻找子树中最小值的节点
Node minValueNode(Node node) {
Node current = node;
while (current.left != null) {
current = current.left;
}
return current;
}
public static void main(String[] args) {
BalancedBinaryTree tree = new BalancedBinaryTree();
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.insert(40);
tree.insert(50);
tree.insert(25);
System.out.println("Inorder traversal of the balanced binary tree:");
tree.inorder();
tree.delete(30);
System.out.println("Inorder traversal after deleting key 30:");
tree.inorder();
}
}
class Node {
int key, height;
Node left, right;
Node(int item) {
key = item;
height = 1;
left = right = null;
}
}