packagecom.demo.tree;importjava.util.LinkedList;importjava.util.Queue;public classBalancedBinaryTree {public static voidmain(String[] args){
BalancedBinaryTree tree= newBalancedBinaryTree();
tree.batchInsert(new int[]{8,6,3,4,5,20,15,23,28,1,2});
tree.tierPrint();
}privateNode root;/*** 节点*/
private classNode{int data; //数据
Node left; //左指针
Node right; //右指针
private Node(intdata) {this.data =data;this.left = null;this.right = null;
}
}/*** 右旋操作(左孩子的左子树插入节点)
*@paramp*/
privateNode rightRotate(Node p){
Node temp= p.left; //temp指向p的左子树
p.left = temp.right; //p的左子树指向temp的右子树
temp.right =p;returntemp;
}/*** 左旋操作(右孩子的右子树插入节点)
*@paramp*/
privateNode leftRotate(Node p){
Node temp= p.right; //temp指向p的右子树
p.right = temp.left; //p的右子树指向temp的左子树
temp.left =p;returntemp;
}/*** 先左旋再右旋(左孩子的右子树插入节点)
*@paramp*/
privateNode leftRightRotate(Node p){
p.left=leftRotate(p.left);returnrightRotate(p);
}/*** 先右旋再左旋(右孩子的左子树插入节点)
*@paramp*/
privateNode rightLeftRotate(Node p){
p.right=rightRotate(p.right);returnleftRotate(p);
}/*** 树高
*@paramnode
*@return
*/
private intgetDepth(Node node){if (node == null){return 0;
}return Math.max(getDepth(node.left), getDepth(node.right))+1;
}/*** 平衡因子(左高:>1 等高:0 右高:
*@return
*/
public intbalanceFactor(Node node){if (node == null){return 0;
}return getDepth(node.left) -getDepth(node.right);
}/*** 插入
*@paramnode
*@paramdata*/
public Node insert(Node node, intdata){
Node newData= newNode(data);if (node == null){returnnewData;
}if (data
node.left=insert(node.left, data);
}else if (data >node.data){
node.right=insert(node.right, data);
}else{returnnode;
}int bf =balanceFactor(node);if (bf > 1 && data
System.out.println("LL" +data);returnrightRotate(node);
}else if (bf < -1 && data >node.right.data){//RR
System.out.println("RR" +data);returnleftRotate(node);
}else if (bf > 1 && data >node.left.data){//LR
System.out.println("LR" +data);returnleftRightRotate(node);
}else if (bf < -1 && data
System.out.println("RL" +data);returnrightLeftRotate(node);
}returnnode;
}/*** 批量插入
*@paramarr*/
public void batchInsert(int[] arr){for (intdata : arr){
root=insert(root, data);
}
}/*** 前序遍历*/
public voidprePrint(){
System.out.print("前序遍历\t");if (root != null){
pre(root);
}
System.out.println();
}private voidpre(Node node){if (node != null) {
System.out.print(node.data+ "\t");
pre(node.left);
pre(node.right);
}
}/*** 中序遍历*/
public voidmidPrint(){
System.out.print("中序遍历\t");if (root != null){
mid(root);
}
System.out.println();
}private voidmid(Node node){if (node != null) {
mid(node.left);
System.out.print(node.data+ "\t");
mid(node.right);
}
}/*** 后序遍历*/
public voidpostPrint(){
System.out.print("后序遍历\t");if (root != null){
post(root);
}
System.out.println();
}private voidpost(Node node){if (node != null) {
post(node.left);
post(node.right);
System.out.print(node.data+ "\t");
}
}/*** 层序遍历,利用队列先进先出*/
public voidtierPrint(){if (root != null){
Queue queue = new LinkedList<>();
queue.add(root);
System.out.print("层序遍历\t");while (!queue.isEmpty()){
Node temp=queue.remove();
System.out.print(temp.data+ "\t");if (temp.left != null){//左节点不为空,放进队列
queue.add(temp.left);
}if (temp.right != null){//右节点不为空,放进队列
queue.add(temp.right);
}
}
}
}
}