二叉排序树
经过尚硅谷韩顺平老师的视频讲解 总结下来的笔记
二叉排序树介绍
二叉排序树:BST: (Binary Sort(Search) Tree),对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大。
特别说明:如果有相同的值,可以将该节点放在左子节点或右子节点
package com.zhangan.tree.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]));
}
//中序遍历
System.out.println("中序遍历二叉树");
binarySortTree.infixOrder();
//测试删除
binarySortTree.delNode(2);
binarySortTree.delNode(5);
binarySortTree.delNode(9);
binarySortTree.delNode(12);
binarySortTree.delNode(7);
binarySortTree.delNode(3);
binarySortTree.delNode(10);
binarySortTree.delNode(1);
System.out.println("删除后");
binarySortTree.infixOrder();
}
}
//二叉排序树
class BinarySortTree {
private Node root;
//添加节点
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("二叉树为空");
}
}
//查找删除的节点
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);
}
}
/**
* @param node 传入的节点当作小二叉树的根节点
* @return 返回的以node为根结点的二叉排序树的最小结点的值
* <p>
* 并删除以node为根结点的二叉排序树的最小结点
*/
public int delRightTreeMin(Node node) {
Node target = node;
//循环的查找左节点,就会找到最小值
while (target.left != null) {
target = target.left;
}
//以node为根结点的二叉排序树的最小结点
//删除最小节点
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;
}
//去找到targetNode的父节点
Node parent = searchParent(value);
//如果要删除的节点 是叶子节点
if (targetNode.left == null && targetNode.right == null) {
//判断targetNode是Parent的左子节点还是右子节点
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);
targetNode.value = minValue;
} else {//要删除的节点有一棵子树
//如果要删除的节点有左子节点
if (targetNode.left != null) {
if (parent != null) {
//如果 targetNode 是 parent 的左子节点
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;
}
}
}
}
}
}
//节点
class Node {
int value;
Node left;
Node right;
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
public Node(int value) {
this.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);
}
}
}
//中序遍历
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
//查找要删除的节点
public Node search(int value) {
if (value == this.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 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;//没有找到父节点
}
}
}
}