package avltree;
public class AVLTreeDemo {
public static void main(String[] args) {
int[] arr = {10, 11, 7, 6, 8, 9};
AVLTree avlTree = new AVLTree();
for (int i = 0; i < arr.length; i++) {
avlTree.add(new Node(arr[i]));
}
System.out.println("中序遍历············");
avlTree.infixOrder();
System.out.println("二叉树高度为: " + avlTree.getRoot().getTreeHeight());
System.out.println("左子树高度为: " + avlTree.getRoot().getLeftHeight());
System.out.println("右子树高度为: " + avlTree.getRoot().getRightHeight());
System.out.println(avlTree.getRoot());
System.out.println(avlTree.getRoot().getLeft());
System.out.println(avlTree.getRoot().getLeft().getLeft());
System.out.println(avlTree.getRoot().getLeft().getRight());
System.out.println(avlTree.getRoot().getRight());
}
}
class AVLTree{
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
public void deleteNode(int value){
if(root == null){
return;
}else{
Node targetNode = search(value);
if(targetNode == null){
return;
}
Node parent = searchParent(value);
if(root.getLeft() == null && root.getRight() == null){
root = null;
return;
}
if(targetNode.getLeft() == null && targetNode.getRight() == null){
if(parent.getLeft() != null && parent.getLeft().getValue() == value){
parent.setLeft(null);
}
if(parent.getRight() != null && parent.getRight().getValue() == value){
parent.setRight(null);
}
}else if(targetNode.getLeft() != null && targetNode.getRight() != null){
int min = getMinValue(targetNode.getRight());
targetNode.setValue(min);
}else{
if(targetNode.getLeft() != null){
if(parent == null){
root = targetNode.getLeft();
}else{
if(parent.getLeft().getValue() == value){
parent.setLeft(targetNode.getLeft());
}else{
parent.setRight(targetNode.getLeft());
}
}
}else{
if(parent == null){
root = targetNode.getRight();
}else{
if(parent.getLeft().getValue() == value){
parent.setLeft(targetNode.getRight());
}else{
parent.setRight(targetNode.getRight());
}
}
}
}
}
}
public int getMinValue(Node node){
Node target = node;
while(target.getLeft() != null){
target = target.getLeft();
}
deleteNode(target.getValue());
return target.getValue();
}
public void add(Node node){
if(root == null){
this.root = node;
}else{
this.root.add(node);
}
}
public void infixOrder(){
if(root != null){
this.root.infixOrder();
}else {
System.out.println("根节点为空,无法遍历");
}
}
public Node search(int value){
if(this.root == null){
return null;
}
return this.root.search(value);
}
public Node searchParent(int value){
if(this.root == null){
return null;
}
return this.root.searchParent(value);
}
}
class Node{
private int value;
private Node left;
private Node right;
public Node(int value){
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
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;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
public void add(Node node){
if(node == null){
return;
}
if(node.value < this.value){
if(this.left == null){
this.left = node;
}else{
this.left.add(node);
}
}else{
if(this.right == null){
this.right = node;
}else{
this.right.add(node);
}
}
if(this.getRightHeight() - this.getLeftHeight() > 1){
if(this.right != null && this.right.getLeftHeight() > this.right.getRightHeight()){
this.right.rightRotate();
this.leftRotate();
}else{
this.leftRotate();
}
return;
}
if(this.getLeftHeight() - this.getRightHeight() > 1){
if(this.left != null && this.left.getRightHeight() > this.left.getLeftHeight()){
this.left.leftRotate();
this.rightRotate();
}else{
this.rightRotate();
}
}
}
public Node searchParent(int value){
if((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)){
return this;
}else{
if(this.value > value && this.left != null){
return this.left.searchParent(value);
}else if(this.value <= value && this.right != null){
return this.right.searchParent(value);
}else{
return null;
}
}
}
public Node search(int value){
if(this.value == value){
return this;
}else if(this.value > value){
if(this.left == null){
return null;
}
return this.left.search(value);
}else{
if(this.right == null){
return null;
}
return this.right.search(value);
}
}
public void infixOrder(){
if(this.left != null){
this.left.infixOrder();
}
System.out.println(this);
if(this.right != null){
this.right.infixOrder();
}
}
public int getTreeHeight(){
return Math.max(left == null? 0 : left.getTreeHeight() , right == null? 0 : right.getTreeHeight()) + 1;
}
public int getLeftHeight(){
if(left == null){
return 0;
}
return left.getTreeHeight();
}
public int getRightHeight(){
if(right == null){
return 0;
}
return right.getTreeHeight();
}
public void leftRotate(){
Node node = new Node(this.value);
node.setLeft(this.getLeft());
node.setRight(this.getRight().getLeft());
this.value = this.getRight().getValue();
this.setRight(this.getRight().getRight());
this.setLeft(node);
}
public void rightRotate(){
Node node = new Node(this.value);
node.setRight(this.getRight());
node.setLeft(this.getLeft().getRight());
this.value = this.getLeft().getValue();
this.setLeft(this.getLeft().getLeft());
this.setRight(node);
}
}