二叉排序树(BST)总结
package com.shizhong.binarysorttree;
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i= 0; i < arr.length; i++) {
binarySortTree.add(new Node(arr[i]));
}
binarySortTree.infixOrder();
binarySortTree.delNode(7);
binarySortTree.infixOrder();
}
}
//创建二叉排序树
class BinarySortTree {
private Node root;
//查找要删除的节点
public Node search(int value) {
if (root == null) {
return null;
}else {
return root.search(value);
}
}
//查找要删除的父节点
public Node searchParent(int value) {
if (root == null) {
return null;
}else {
return root.searchParent(value);
}
}
//编写方法
/**
*功能:返回以node为根节点的二叉排序树的最小节点的值,同时还要删除以node为根节点的二叉排序树的最小节点
* @param node 传入的节点(当作二叉树的根节点)
* @return 返回以node为根节点的二叉排序树的最小节点的值
*/
public int delRightTreeMin(Node node) {
Node target = node;
//循环的查找左子节点
while(target.left != null) {
target = target.left;
}
//这时target指向最小的节点
//删除最小的节点
delNode(target.value);
return target.value;
}
//编写方法
public int delLeftTreeMax(Node node) {
Node target = node;
while (target.right != null) {
target = target.right;
}
delNode(target.value);
return target.value;
}
//删除节点
public void delNode(int value) {
if (root == null) {
return ;
}else {
Node targetNode = search(value);//找到要删除的目标节点
if(targetNode == null) {//判断二叉排除树中是否有目标节点
return ;
}
if (root.left == null && root.right == null) {//判断二叉排序树中是否只有一个根节点
root = null;
return ;
}
Node parent = searchParent(value);//查找要删除节点的父节点
//如果要删除的节点是叶子节点
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.value == value) {//要删除节点是父节点的左子节点
parent.left = null;
}else if (parent.right != null && parent.right.value == value) {//要删除节点是父节点的右子节点
parent.right = null;
}
}else if (targetNode.left != null && targetNode.right != null) {//要删除的节点有两棵子树
//int minValue = delRightTreeMin(targetNode.right);
int maxValue = delLeftTreeMax(targetNode.left);
targetNode.value = maxValue;
}else {//要删除的节点只有一棵子树
if (targetNode.left != null) {//如果要删除的节点的左子树不为空
if (parent != null) {
if (parent.left.value == value) {//要删除的节点是父节点的左子节点还是右子节点
parent.left = targetNode.left;
}else {
parent.right = targetNode.left;
}
}else {
root = targetNode.left;
}
}else {//如果要删除的节点的右子树不空
if (parent != null) {
if (parent.left.value == value) {//判断要删除的节点是父节点的左子节点还是右子节点
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
}else {
root = targetNode.right;
}
}
}
}
}
//添加节点的方法
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
//中序遍历二叉树
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉排序数为空");
}
}
}
//创建二叉排序树(BST)的节点
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
//查找要删除的节点
/**
*
* @param value 要删除的节点的值
* @return 如果知道,则返回该节点,否则返回null
*/
public Node 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(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);//递归向右子树进行添加
}
}
}
//查找要删除节点的父节点
/**
*
* @param value 需要删除节点的值
* @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 (value < this.value && this.left != null) {
return this.left.searchParent(value);//向左子树进行递归查找
} else if (value >= this.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);
if (this.right != null) {
this.right.infixOrder();
}
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
}
平衡二叉树(平衡二叉搜索树)AVL树
package com.shizhong.avl;
public class AVLTreeDemo {
public static void main(String[] args) {
//int[] arr = {4, 3, 6, 5, 7, 8};
//int[] arr = {10, 12, 8, 9, 7, 6};
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("在做平衡处理之后");
System.out.println("树的高度" + avlTree.getRoot().height());
System.out.println("树的左子树高度" + avlTree.getRoot().leftHeight());
System.out.println("树的右子树高度" + avlTree.getRoot().rightHeight());
}
}
class AVLTree {
private Node root;
public Node getRoot() {
return root;
}
//查找要删除的节点
public Node search(int value) {
if (root == null) {
return null;
} else {
return root.search(value);
}
}
//查找要删除的父节点
public Node searchParent(int value) {
if (root == null) {
return null;
} else {
return root.searchParent(value);
}
}
//编写方法
/**
* 功能:返回以node为根节点的二叉排序树的最小节点的值,同时还要删除以node为根节点的二叉排序树的最小节点
*
* @param node 传入的节点(当作二叉树的根节点)
* @return 返回以node为根节点的二叉排序树的最小节点的值
*/
public int delRightTreeMin(Node node) {
Node target = node;
//循环的查找左子节点
while (target.left != null) {
target = target.left;
}
//这时target指向最小的节点
//删除最小的节点
delNode(target.value);
return target.value;
}
//编写方法
public int delLeftTreeMax(Node node) {
Node target = node;
while (target.right != null) {
target = target.right;
}
delNode(target.value);
return target.value;
}
//删除节点
public void delNode(int value) {
if (root == null) {
return;
} else {
Node targetNode = search(value);//找到要删除的目标节点
if (targetNode == null) {//判断二叉排除树中是否有目标节点
return;
}
if (root.left == null && root.right == null) {//判断二叉排序树中是否只有一个根节点
root = null;
return;
}
Node parent = searchParent(value);//查找要删除节点的父节点
//如果要删除的节点是叶子节点
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.value == value) {//要删除节点是父节点的左子节点
parent.left = null;
} else if (parent.right != null && parent.right.value == value) {//要删除节点是父节点的右子节点
parent.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) {//要删除的节点有两棵子树
//int minValue = delRightTreeMin(targetNode.right);
int maxValue = delLeftTreeMax(targetNode.left);
targetNode.value = maxValue;
} else {//要删除的节点只有一棵子树
if (targetNode.left != null) {//如果要删除的节点的左子树不为空
if (parent != null) {
if (parent.left.value == value) {//要删除的节点是父节点的左子节点还是右子节点
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
root = targetNode.left;
}
} else {//如果要删除的节点的右子树不空
if (parent != null) {
if (parent.left.value == value) {//判断要删除的节点是父节点的左子节点还是右子节点
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
} else {
root = targetNode.right;
}
}
}
}
}
//添加节点的方法
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
//中序遍历二叉树
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉排序数为空");
}
}
}
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
// 返回左子树的高度
public int leftHeight() {
if (left == null) {
return 0;
}
return left.height();
}
//返回右子树的高度
public int rightHeight() {
if (right == null) {
return 0;
}
return right.height();
}
//左旋转的方法
private void leftRotate() {
//创建新的节点,以当前根节点的值作为新的节点的值
Node newNode = new Node(value);
//将新的节点的左子树设置为当前节点的左子树
newNode.left = left;
//把新的节点的右子树设置为当前节点的右子树的左子树
newNode.right = right.left;
//将当前节点的值替换成右子节点的值
value = right.value;
//将当前节点的右子树设置为当前节点的右子树的右子树
right = right.right;
//将当前节点的左子节点设置为新的节点
left = newNode;
}
//右旋转的方法
private void rightRotate() {
Node newNode = new Node(value);
newNode.right = right;
newNode.left = left.right;
value = left.value;
left = left.left;
right = newNode;
}
//返回当前节点的高度,就是以当前节点为根节点的树的高度
public int height() {
// if (left == null || right == null) {
// return 0;
// }
// int leftHeight = left.height();
// int rightHeight = right.height();
// return Math.max(leftHeight, rightHeight) + 1;
return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
}
//查找要删除的节点
/**
* @param value 要删除的节点的值
* @return 如果知道,则返回该节点,否则返回null
*/
public Node 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(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);//递归向右子树进行添加
}
}
//当添加完一个节点之后,如果(右子树的高度 - 左子树的高度 > 1), 则发生左旋转
if (rightHeight() - leftHeight() > 1) {
//如果他的右子树的左子树的高度大于他的右子树的右子树高度
if (right != null && right.leftHeight() > right.rightHeight()) {
right.rightRotate();//先对他的右子节点进行右旋转
leftRotate();
}else {
leftRotate();//否则直接左旋转
}
return ;
}
//当添加完一个节点后,如果(左子树的高度- 右子树的高度 > 1),则发生右旋转
if (leftHeight() - rightHeight() > 1) {
//如果他的左子树的右子树高度大于他的左子树的左子树高度
if (left != null && left.rightHeight() > left.leftHeight()) {
//先对当前节点的左节点进行左旋转
left.leftRotate();
rightRotate();//在对当前节点进行右旋转
}else {
rightRotate();//直接进行右旋转
}
}
}
//查找要删除节点的父节点
/**
* @param value 需要删除节点的值
* @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 (value < this.value && this.left != null) {
return this.left.searchParent(value);//向左子树进行递归查找
} else if (value >= this.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);
if (this.right != null) {
this.right.infixOrder();
}
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
}
2-3树
B树、B+树、B*树