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 = {10, 7, 15, 14, 13, 16};
/*
4 10 8
/ \ / \ 右旋转 / \
3 6 8 12 =====> 7 10
/ \ / \ / / \
5 7 7 9 6 9 12
\ /
8 6
*/
AVLTree tree = new AVLTree();
int len = arr.length;
for (int i = 0; i < len; i++) {
tree.add(new Node(arr[i]));
}
tree.infixOrder();
System.out.println("\n树的高度:" + tree.getRoot().height());
System.out.println("树的左子树高度:" + tree.getRoot().left.height());
System.out.println("树的右子树高度:" + tree.getRoot().right.height());
System.out.println("树的左右子树高度差的绝对值:" + tree.getRoot().differ());
}
}
class AVLTree {
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉树为空,无法遍历~");
}
}
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
}
class Node {
public int num;
public Node left;
public Node right;
public Node(int num) {
this.num = num;
}
@Override
public String toString() {
return num + "";
}
/**
*中序遍历
*/
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.print("=>" + num);
if (this.right != null) {
this.right.infixOrder();
}
}
/**
* 返回以该节点为根节点的树的高度
*/
public int height() {
return Math.max(this.left == null ? 0 : this.left.height(),
this.right == null ? 0 : this.right.height()) + 1;
}
/**
* 返回左子树的高度
*/
public int leftHeight() {
if (this.left == null) {
return 0;
} else {
return this.left.height();
}
}
/**
* 返回右子树的高度
*/
public int rightHeight() {
if (this.right == null) {
return 0;
} else {
return this.right.height();
}
}
/**
* 返回左右子树高度的差
*/
public int differ() {
return Math.abs(leftHeight() - rightHeight());
}
/**
* 左旋转的方法
*/
public void leftRotate() {
//创建新的结点,值为当前结点的值
Node newNode = new Node(this.num);
//新结点的左子树指向当前结点的左子树
newNode.left = this.left;
//新结点的右子树指向当前结点的右子树的左子树
newNode.right = this.right.left;
//当前结点的左子树设置成新结点
this.left = newNode;
//当前结点的值替换成右子节点的值
this.num = this.right.num;
//把当前结点的右子树设置成当前结点右子树的右子树
this.right = this.right.right;
}
/**
* 右旋转的方法
*/
public void rightRotate() {
Node newNode = new Node(this.num);
newNode.right = this.right;
newNode.left = this.left.right;
this.right = newNode;
this.num = this.left.num;
this.left = this.left.left;
}
/**
* 添加新的节点【二叉排序树】
*/
public void add(Node node) {
if (node == null) {
return;
}
if (node.num < this.num) {
if (this.left != null) {
this.left.add(node);
} else {
this.left = node;
}
} else {
if (this.right != null) {
this.right.add(node);
} else {
this.right = node;
}
}
//当添加完一个结点后,如果(右子树的高度-左子树的高度)> 1,则进行左旋转
if (rightHeight() - leftHeight() > 1) {
//如果(右子节点的左子树高度-右子节点的右子树高度 > 1),则对右子节点右旋转
if (this.right != null && (this.right.leftHeight() - this.right.rightHeight() > 0)) {
this.right.rightRotate();
}
this.leftRotate();
return; //必须要
}
//当添加完一个结点后,如果(左子树的高度-右子树的高度)> 1,则进行右旋转
if (leftHeight() - rightHeight() > 1) {
//如果(左子节点的右子树高度-左子节点的左子树高度 > 1),则对左子结点进行左旋转
if (this.left != null && (this.left.rightHeight() - this.left.leftHeight() > 0)) {
this.left.leftRotate();
}
this.rightRotate();
}
}
}
平衡二叉树的实现
最新推荐文章于 2024-04-06 15:06:05 发布