写一个Java 平衡二叉树
时间: 2024-04-10 10:11:36 浏览: 97
平衡二叉树也叫AVL树,是一种自平衡的二叉搜索树,它的左右子树的高度差(平衡因子)不超过1。下面是Java实现平衡二叉树的示例代码:
```java
class Node {
int key, height;
Node left, right;
Node(int d) {
key = d;
height = 1;
}
}
class AVLTree {
Node root;
// 获取节点高度
int height(Node N) {
if (N == null)
return 0;
return N.height;
}
// 获取最大值
int max(int a, int b) {
return (a > b) ? 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 T2 = y.left;
y.left = x;
x.right = T2;
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 N) {
if (N == null)
return 0;
return height(N.left) - height(N.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;
}
// 删除节点
Node deleteNode(Node root, int key) {
if (root == null)
return root;
if (key < root.key)
root.left = deleteNode(root.left, key);
else if (key > root.key)
root.right = deleteNode(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 = deleteNode(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.left) < 0) {
root.left = leftRotate(root.left);
return rightRotate(root);
}
if (balance < -1 && getBalance(root.right) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root.right) > 0) {
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
// 获取最小值节点
Node minValueNode(Node node) {
Node current = node;
while (current.left != null)
current = current.left;
return current;
}
// 中序遍历
void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
System.out.print(node.key + " ");
inOrder(node.right);
}
}
public static void main(String[] args) {
AVLTree tree = new AVLTree();
tree.root = tree.insert(tree.root, 10);
tree.root = tree.insert(tree.root, 20);
tree.root = tree.insert(tree.root, 30);
tree.root = tree.insert(tree.root, 40);
tree.root = tree.insert(tree.root, 50);
tree.root = tree.insert(tree.root, 25);
System.out.println("Inorder traversal of the constructed AVL tree is ");
tree.inOrder(tree.root);
tree.root = tree.deleteNode(tree.root, 30);
System.out.println("\nInorder traversal after deletion of 30 ");
tree.inOrder(tree.root);
}
}
```
该代码实现了AVL树的插入、删除、旋转、遍历等操作。可以通过main函数进行测试。
阅读全文