平衡二叉树
一、概念
二、源码
1.Node
代码如下(示例):
package BinarySortTree;
public class Node {
public int value;
public Node left;
public Node right;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
//返回左子树的高度
public int getleftheight() {
if (this.left == null) {
return 0;
}
return this.left.getheight();
}
//返回右子树的高度
public int getrightheight() {
if (this.right == null) {
return 0;
}
return this.right.getheight();
}
//查找以该节点为根节点的树的高度
public int getheight() {
//递归的次数
return Math.max(this.left == null ? 0 : this.left.getheight(), this.right == null ? 0 : this.right.getheight()) + 1;
}
//左旋转
public void leftRotate() {
Node newNode = new Node(this.value);
newNode.left = this.left;
newNode.right = this.right.left;
this.value = this.right.value;
this.left = newNode;
this.right = this.right.right;
}
//右旋转
public void rightRotate() {
Node newNode = new Node(this.value);
newNode.right = this.right;
newNode.left = this.left.right;
this.value = this.left.value;
this.right = newNode;
this.left = this.left.left;
}
//添加节点
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();
}
return;
}
}
//查找要删除的节点
public Node Search(int value) {
//说明找到该节点
if (this.value == value) {
return this;
} else if (this.value < value) {
//向右边递归查找
if (this.right == null) {
return null;
}
return this.right.Search(value);
} else if (this.value > value) {
//向左递归查找
if (this.left == null) {
return null;
}
return this.left.Search(value);
} else {
return null;
}
}
//查找要删除节点的父节点
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 void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this.value);
if (this.right != null) {
this.right.infixOrder();
}
}
}
2.Tree
代码如下(示例):
package BinarySortTree;
public class BinarySortTree {
private Node root;
//得到root节点
public Node getRoot(){
return root;
}
//添加节点
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
//查找到删除的这个节点
public Node Search(int value) {
if (root != null) {
return root.Search(value);
} else {
return null;
}
}
//查找要删除的节点的父节点
public Node SearchParent(int value) {
if (root != null) {
return root.SearchParent(value);
} else {
return null;
}
}
//删除这个节点
public void deleteNode(int value) {
if (root == null) {
return;
} else {
Node targetNode = Search(value);
if (targetNode == null) {
return;
}
//证明targetNode不为空,所以当下面的if成立,targetNode就是root
if (root.right == null & root.left == null) {
root = null;
return;
}
Node ParentNode = SearchParent(value);
if (targetNode.left == null && targetNode.right == null) {
if (ParentNode.left.value == targetNode.value && ParentNode.left != null) {
ParentNode.left = null;
} else if (ParentNode.right.value == targetNode.value && ParentNode.right != null) {
ParentNode.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) {
int intmin = Intmin(targetNode.right);
targetNode.value = intmin;
} else {
if (targetNode.left!=null){
if (ParentNode!=null){
if (ParentNode.left.value==value){
ParentNode.left = targetNode.left;
}else {
ParentNode.right = targetNode.left;
}
}else {
root = targetNode.left;
}
}else {
if (targetNode.right!=null){
if (ParentNode!=null){
if (ParentNode.left.value ==value){
ParentNode.left = targetNode.right;
}else {
ParentNode.right = targetNode.right;
}
}else {
root = targetNode.right;
}
}
}
}
}
}
//查找右边节点的最小值min
public int Intmin(Node node) {
Node temp = node;
while (temp.left != null) {
temp = temp.left;
}
deleteNode(temp.value);
return temp.value;
}
//中序遍历
public void infixOrder() {
if (root == null) {
System.out.println("二叉树为空,无法遍历");
} else {
root.infixOrder();
}
}
}
2.Demo
代码如下(示例):
package BinarySortTree;
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {10,11,7,6,8,9};
BinarySortTree binarySortTree = new BinarySortTree();
//将数组中的每个数依次以节点的形式加入到树中
for (int i = 0; i < arr.length; i++) {
binarySortTree.add(new Node(arr[i]));
}
System.out.println("中序遍历");
binarySortTree.infixOrder();
/* binarySortTree.deleteNode(7);
binarySortTree.deleteNode(3);
binarySortTree.deleteNode(10);
binarySortTree.deleteNode(1);
System.out.println("中序遍历");
binarySortTree.infixOrder();*/
Node root = binarySortTree.getRoot();
int height = root.getheight();
System.out.println("==============");
System.out.println(height);
int getleftheight = root.getleftheight();
System.out.println(getleftheight);
int getrightheight = root.getrightheight();
System.out.println(getrightheight);
}
}
总结
代码是基于二叉排序树进行改进的。注意左旋转和右旋转的条件,也需要注意双旋转的条件。