Ai
1 Star 1 Fork 1

xcc/structures-and-algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
AVLTreeDemo.java 4.97 KB
一键复制 编辑 原始数据 按行查看 历史
xcc 提交于 2020-12-22 22:41 +08:00 . 添加AVLtree
package com.xcc.dataStructures.demo12_avl;
/**
* 平衡二叉树
*/
public class AVLTreeDemo {
public static void main(String[] args) {
// int[] arr = {10, 11, 7, 6, 8, 9};
int[] arr = {2, 1, 6, 5, 7, 3};
AVLTree tree = new AVLTree();
for (int i = 0; i < arr.length; i++) {
tree.add(new Node(arr[i]));
}
System.out.println("中序遍历");
tree.midOrder();
System.out.println("根结点为:" + tree.getRoot());
System.out.println("二叉树高度为:" + tree.height());
System.out.println("左子树高度为:" + tree.leftHeight());
System.out.println("右子树高度为:" + tree.rightHeight());
}
}
class AVLTree {
private Node root;
public Node getRoot() {
return root;
}
/**
* 右子树高度
*/
public int rightHeight() {
return root == null ? 0 : root.rightHeight();
}
/**
* 左子树高度
*/
public int leftHeight() {
return root == null ? 0 : root.leftHeight();
}
/**
* 二叉树高度
*/
public int height() {
return root == null ? 0 : root.height();
}
/**
* 添加
*/
public void add(Node node) {
if (root == null) {
root = node;
return;
}
root.add(node);
}
/**
* 中序遍历
*/
public void midOrder() {
if (root == null) {
System.out.println("二叉树为空,不能遍历");
return;
}
root.midOrder();
}
}
class Node {
int value;
Node left;
Node right;
/**
* 左子树高度
*/
public int leftHeight() {
return this.left == null ? 0 : this.left.height();
}
/**
* 右子树高度
*/
public int rightHeight() {
return this.right == null ? 0 : this.right.height();
}
/**
* 二叉树的总高度
*/
public int height() {
//取左子树和右子树的最大值为总高度。左子树递归查找,如果为null则是0,不为空则获取原先高度+1
return Math.max(this.left == null ? 0 : this.left.height(), this.right == null ? 0 : this.right.height()) + 1;
}
/**
* 左旋转(以当前结点为根节点)
*/
private void leftRotate() {
//1)创建新节点值为当前根节点的值
Node newNode = new Node(value);
//2)新结点的左结点为根节点的左结点
newNode.left = this.left;
//3)新结点的右结点为根节点的右结点的左结点
newNode.right = this.right.left;
//4)当前结点值为当前结点的右结点值
this.value = this.right.value;
//5)当前结点的左结点指向新结点
this.left = newNode;
//6)当前结点的右结点指向原来右子树的右结点
this.right = this.right.right;
}
/**
* 右旋转
*/
public void rightRotate() {
//1)创建新结点值为当前根节点的值
Node newNode = new Node(value);
//2)新结点的右子树为当前右子树
newNode.right = this.right;
//3)新结点的左子树为当前根节点的左子树的右子树
newNode.left = this.left.right;
//4)当前结点值为当前结点的左子树的值
this.value = this.left.value;
//5)当前结点的右子树为新结点
this.right = newNode;
//6)当前结点的左子树为当前结点左子树的左子树
this.left = this.left.left;
}
/**
* 添加结点
*/
public void add(Node node) {
if (node == null) {
return;
}
//如果当前节点值大于添加的值
if (this.value > node.value) {
//如果左节点不为空就向左递,否则就赋值给左节点
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;
}
}
if (rightHeight() - leftHeight() > 1) {
if (right.leftHeight() > right.rightHeight()) {
right.rightRotate();
}
leftRotate();
}
if (leftHeight() - rightHeight() > 1) {
if (left.rightHeight() > left.leftHeight()) {
left.leftRotate();
}
rightRotate();
}
}
/**
* 中序遍历
*/
public void midOrder() {
if (this.left != null) {
this.left.midOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.midOrder();
}
}
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node[" +
"value=" + value +
']';
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiaocheng0902/structures-and-algorithm.git
git@gitee.com:xiaocheng0902/structures-and-algorithm.git
xiaocheng0902
structures-and-algorithm
structures-and-algorithm
master

搜索帮助