目录
一、二叉排序树介绍
二叉排序树:BST: (Binary Sort(Search) Tree), 对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大。 特别说明:如果有相同的值,可以将该节点放在左子节点或右子节点
简单来说就是在一个二叉排序树中,对于任何一个非叶子结点所在的【三角关系】(ps:自身+左孩子+右孩子构成一个三角关系), [右孩子≥当前结点≥左孩子]
以Arr{7, 3, 10,12, 5,1,9}为例
二、二叉排序树的创建和遍历
package com.zengwen.tree;
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {7,3,10,12,5,1,9};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i = 0; i < arr.length; i++) {
binarySortTree.add(new Node(arr[i]));
}
binarySortTree.midOrder();
}
}
class BinarySortTree{
private Node root;
public void add(Node node){
if (root == null) {
root = node;
return;
}
root.addNode(node);
}
public void midOrder(){
if (root != null) {
root.midOrder();
}else {
System.out.println("二叉排序树为空~");
}
}
}
class Node{
private int value;
private Node left;//左子结点
private Node right;//右子结点
public Node(int value){
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
//添加结点
public void addNode(Node node){
if (node == null) return;
//判断传入的结点的值和当前子树的根结点的值
if (node.value < this.value){//如果新添加结点小于当前子树的根结点的值
if (this.left == null){//如果当前结点左孩子为空
this.left = node;
}else{
this.left.addNode(node);//递归向左子结点添加
}
}else {//如果新添加结点大于当前子树的根结点的值
if (this.right == null){//如果当前结点右孩子为空
this.right = node;
}else {
this.right.addNode(node);//递归向右孩子添加
}
}
}
//中序遍历
public void midOrder(){
if (this.left != null) this.left.midOrder();
System.out.println(this);
if (this.right != null) this.right.midOrder();
}
}
三、二叉排序树的删除
思路分析:
第一种情况:删除叶子结点, parent和targetNode有2种关系,直接parent.left为空或者parent.right为空
第二种情况,删除只有一颗子树的节点,parent和targetNode之间有2种关系,即将替换掉targetNode的结点和targetNode之间也有两种关系,所以有4种可能。
第三种情况,删除有2颗子树的节点,首先明确最后谁来替换掉targetNode,(既可以是targetNode右子树最小的,也可以是targetNode左子树中最大的来替换targetNode),其实找targetNode右子树最小的,就是从targetNode的右子树一直往左边找即可。
代码实现:
package com.zengwen.tree;
//
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {7,3,10,12,5,1,9};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i = 0; i < arr.length; i++) {
binarySortTree.add(new Node(arr[i]));
}
binarySortTree.midOrder();
binarySortTree.delNode(2);
binarySortTree.delNode(5 );
binarySortTree.delNode(3);
binarySortTree.delNode(1);
binarySortTree.delNode(12);
binarySortTree.delNode(10);
binarySortTree.delNode(9);
binarySortTree.delNode(7);
System.out.println("=========================================");
binarySortTree.midOrder();
}
}
class BinarySortTree{
private Node root;
public Node search(int value){
if (root == null) return null;
return root.search(value);
}
public Node searchParent(int value){
if (root == null) return null;
return root.searchParent(value);
}
/**
* 找到targetNode右子树最小节点并返回删除
* @param targetNode
* @return
*/
public int delRightTreeMin(Node targetNode){
Node target = targetNode;
//循环查找左节点,就会找到最小值
while(target.left != null) target=target.left;
//这时target就指向了最小节点
delNode(target.value);//删除最小节点
return target.value;
}
public void delNode(int value){
if (root == null) return;
Node targetNode = search(value);
if (targetNode == null) return;//如果没有找到要删除的节点,直接return
if (root.left == null && root.right == null) { //如果我们发现二叉树只有一个结点
root=null;
return;
}
Node parent = searchParent(value); //去找到parent
//如果要删除的节点是叶子结点
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 minVal = delRightTreeMin(targetNode.right);
targetNode.value = value;
}else {//删除只有一颗子树的节点
//如果targetNode只有左子结点
if (targetNode.left != null){
if (parent != null){
//如果targetNode是parent的左子结点
if (parent.left != null && parent.left.value == value){
parent.left = targetNode.left;
}else {//如果targetNode是parent的右子结点
parent.right = targetNode.left;
}
}else {
root = targetNode.left;
}
}else {//如果targetNode只有右子结点
if (parent != null){
//如果targetNode是parent的左子结点
if (parent.left != null && parent.left.value == value){
parent.left = targetNode.right;
}else {//如果targetNode是parent的右子结点
parent.right = targetNode.right;
}
}else {
root = targetNode.right;
}
}
}
}
public void add(Node node){
if (root == null) {
root = node;
return;
}
root.addNode(node);
}
public void midOrder(){
if (root != null) {
root.midOrder();
}else {
System.out.println("二叉排序树为空~");
}
}
}
class Node{
int value;
Node left;//左子结点
Node right;//右子结点
public Node(int value){
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + 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){
//如果查找的值d大于当前结点的值,并且当前结点的右子节点不为空,向右递归查找
return this.right.searchParent(value);
}else {
return null;//没有找到父节点
}
}
/**
* 查找要删除的节点
*/
public Node search(int value){
if (value == this.value) return this;
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 addNode(Node node){
if (node == null) return;
//判断传入的结点的值和当前子树的根结点的值
if (node.value < this.value){//如果新添加结点小于当前子树的根结点的值
if (this.left == null){//如果当前结点左孩子为空
this.left = node;
}else{
this.left.addNode(node);//递归向左子结点添加
}
}else {//如果新添加结点大于当前子树的根结点的值
if (this.right == null){//如果当前结点右孩子为空
this.right = node;
}else {
this.right.addNode(node);//递归向右孩子添加
}
}
}
//中序遍历
public void midOrder(){
if (this.left != null) this.left.midOrder();
System.out.println(this);
if (this.right != null) this.right.midOrder();
}
}