思路:在增加数据时,保持此树为顺序存储二叉树,然后判断此二叉树是否为平衡二叉树,如果bf >1 就右旋 bf<-1左旋。但是还有一种情况,需要双选,暂时还没实现。
class AvlTree {
Node root;
/**
* 增加数值
* @param data
*/
public void insert(Integer data) {
if (root == null) {
root = new Node(data);
} else {
insert(data, null, root, false);
}
//二叉树如果失去平衡
if (this.bf() > 1) {//右旋 bnm,./
rightRotate();
}
if (this.bf() < -1) {//右旋 bnm,./
leftRotate();
}
}
public void rightRotate() {
Node tmp = root;
root = root.left;
Node tmp1 = root.right;
root.right = tmp;
root.right.left = tmp1;
}
public void leftRotate() {
Node tmp = root;
root = root.right;
Node tmp1 = root.left;
root.left = tmp;
root.left.right = tmp1;
}
public void insert(Integer data, Node pre, Node now, boolean isLeft) {
if (now == null) {
if (isLeft) {
Node node = new Node(data);
node.setParentLeft(true);
node.parent = pre;
pre.setLeft(node);
} else {
Node node = new Node(data);
node.setParentRright(true);
node.parent = pre;
pre.setRight(node);
}
return;
}
if (now.data > data) {
insert(data, now, now.getLeft(), true);
}
if (now.data < data) {
insert(data, now, now.getRight(), false);
}
}
/**
* 获取树的高度
* @return
*/
public int height() {
return height(root);
}
private int height(Node root) {
if (root == null) {
return 0;
}
return Math.max(height(root.left), height(root.right)) + 1;
}
/**
* 是否为平衡二叉树
* @return
*/
public boolean isBalance() {
return isBalance(root);
}
public boolean isBalance(Node root) {
if (root == null) {
return true;
}
if (Math.abs(height(root.left) - height(root.right)) > 1) {
return false;
}
return isBalance(root.left) && isBalance(root.right);
}
//>1 右旋 <-1左旋
public int bf(Node root) {
if (root == null) {
return 999;
}
return height(root.left) - height(root.right);
}
//>1 右旋 <-1左旋
public int bf() {
return bf(root);
}
}
class Node {
//节点数值
int data;
//左节点
Node left;
//右节点
Node right;
//父节点
Node parent;
//位于父节点的左边
boolean isParentLeft;
//位于父节点的右边
boolean isParentRright;
public Node(int data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public boolean isParentLeft() {
return isParentLeft;
}
public void setParentLeft(boolean parentLeft) {
isParentLeft = parentLeft;
}
public boolean isParentRright() {
return isParentRright;
}
public void setParentRright(boolean parentRright) {
isParentRright = parentRright;
}
}
public class Demo1 {
public static void main(String[] args) {
AvlTree avlTree = new AvlTree();
/*avlTree.insert(4);
avlTree.insert(2);
avlTree.insert(5);
avlTree.insert(1);
avlTree.insert(3);
avlTree.insert(0);*/
avlTree.insert(4);
avlTree.insert(2);
avlTree.insert(9);
avlTree.insert(8);
avlTree.insert(11);
avlTree.insert(12);
System.out.println(avlTree.bf());
}
}