class SelfBalancingBinarySearchTree
package DataStructures.tree;
public class SelfBalancingBinarySearchTree {
public static void main(String[] args) {
int[] arr = {4,3,6,5,7,8};
SelfBalancingBinarySearchTree avlTree = new SelfBalancingBinarySearchTree();
for (int i = 0; i < arr.length; i++) {
avlTree.add(new Node3(arr[i]));
}
System.out.println("之前...");
avlTree.infixOrder();
System.out.println("树的高度" + avlTree.getRoot().height());
System.out.println("左子树的高度" + avlTree.getRoot().leftHeight());
System.out.println("右子树的高度" + avlTree.getRoot().rightHeight());
}
private Node3 root;
public Node3 getRoot() {
return root;
}
public void delNode(int value){
if (root == null){
return;
}else {
Node3 target = search(value);
if (target == null){
return;
}
if (root.left == null && root.right == null){
root = null;
return;
}
Node3 parent = searchParent(value);
if (target.left == null && target.right == null) {
if (parent.left != null && target == parent.left) {
parent.left = null;
} else if (parent.right != null && target == parent.right) {
parent.right = null;
}
}else if (target.left == null || target.right == null){
if (target.right == null){
if (parent == null){
root = target.left;
}
if (target == parent.left){
parent.left = target.left;
}else {
parent.right = target.left;
}
}else{
if (parent == null){
root = target.right;
}
if (target == parent.left){
parent.left = target.right;
}else {
parent.right = target.right;
}
}
} else {
target.value = delRightTreeMin(target.right);
}
}
}
public int delRightTreeMin(Node3 node){
while (node.left != null){
node = node.left;
}
delNode(node.value);
return node.value;
}
public Node3 searchParent(int value) {
return root.searchParent(value);
}
public Node3 search(int value){
return root.search(value);
}
public void add(Node3 node){
if (root == null){
root = node;
}else {
root.add(node);
}
}
public void infixOrder(){
if (root != null){
root.infixOrder();
}else {
System.out.println("空,不可遍历");
}
}
}
class Node3{
int value;
Node3 left;
Node3 right;
public Node3(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node2{" +
"value=" + value +
'}';
}
public void rightRotate(){
Node3 node = new Node3(value);
node.right = right;
node.left = left.right;
value = left.value;
left = left.left;
right = node;
}
public void leftRotate(){
Node3 node = new Node3(value);
node.left = left;
node.right = right.left;
value = right.value;
right = right.right;
left = node;
}
public int rightHeight(){
return right == null ? 0 :right.height();
}
public int leftHeight(){
return left == null ? 0 :left.height();
}
public int height(){
return Math.max(left == null ? 0 : left.height(),right == null ? 0 :right.height()) + 1;
}
public Node3 searchParent(int value) {
if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
return this;
} else {
if (value < this.left.value && this.left != null) {
return this.left.searchParent(value);
} else if (value >= this.right.value && this.right != null){
return this.right.searchParent(value);
}else {
return null;
}
}
}
public Node3 search(int value){
if (this.value == value){
return this;
}else if (value < this.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 add(Node3 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 (leftHeight() + 1< rightHeight()){
if (right.leftHeight() > right.rightHeight()){
rightRotate();
}
leftRotate();
}else if (rightHeight() + 1 >leftHeight()){
if(left.rightHeight() > left.leftHeight()){
left.leftRotate();
}
rightRotate();
}
}
public void infixOrder(){
if (this.left != null){
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null){
this.right.infixOrder();
}
}
}