package com.jiahui.java;
/**7,3,10,12,5,1,9
* @author shkstart
* @create 2022-11-25 19:34
*/
public class Sort {
public static void main(String args[]){
SortBinaryTree tree=new SortBinaryTree(new Node(7));
tree.add(new Node(3));
Node node1=new Node(10);
tree.add(node1);
tree.add(new Node(12));
tree.add(new Node(5));
tree.add(new Node(1));
tree.add(new Node(9));
Node node=new Node(2);
tree.add(node);
tree.delete(node);
tree.delete(node1);
tree.inforOrder();
}
}
class SortBinaryTree{
Node root;
public SortBinaryTree(Node root) {
this.root = root;
}
public void inforOrder(){
if (root==null){
System.out.println("二叉排序树为空,无法遍历");
}else {
root.infoOrder();
}
}
public void delete(Node node){
if (root!=null){
if (root==node){
//左右都空
if (root.getLeft()==null && root.getRight()==null){
root=null;
return;
}
//左右都不空
if (root.getRight()!=null && root.getRight()!=null){
Node rightTree = root.getRight().getRight();
Node minNode = rightTree.minValue();
Node parentOfMin = root.searchParent(minNode);
if (parentOfMin.getLeft()!=null &&parentOfMin.getLeft()==minNode){
parentOfMin.setLeft(null);
}else {
parentOfMin.setRight(null);
}
root.setValue(minNode.getValue());
return;
}
//左空
if (root.getLeft()!=null){
root=root.getLeft();
return;
}
//右空
if (root.getRight()!=null){
root=root.getRight();
return;
}
}else {
root.delete(node);
}
}else{
System.out.println("二叉树为空,无法删除");
}
}
public void add(Node node){
if (root==null){
root=node;
}else {
root.add(node);
}
}
}
class Node{
private int value;
private Node left;
private Node right;
public void setValue(int value) {
this.value = value;
}
public void setLeft(Node left) {
this.left = left;
}
public void setRight(Node right) {
this.right = right;
}
/**
* 删除
* @param node 要删除的节点
*/
public void delete(Node node){
int i=node.getValue();
//左子树等于要找的node
if (this.getLeft()!=null && this.getLeft().getValue()==i){
//都为空
if (this.getLeft().getLeft()==null && this.getLeft().getRight()==null){
this.setLeft(null);
return;
}
//都不为空
if (this.getLeft().getLeft()!=null&&this.getLeft().getRight()!=null){
Node rightTree = this.getLeft().getRight();
Node minNode = rightTree.minValue();
// Node parent = rightTree.searchParent(minNode);
// if (parent.getLeft()==minNode){
// parent.delete();
// }else {
//
// }
Node parentOfMin = this.searchParent(minNode);
if (parentOfMin.getLeft()!=null &&parentOfMin.getLeft()==minNode){
parentOfMin.setLeft(null);
}else {
parentOfMin.setRight(null);
}
this.getLeft().setValue(minNode.getValue());
return;
}
//左不为空
if (this.getLeft().getLeft()!=null ){
this.setLeft(this.getLeft().getLeft());
return;
}
//右不为空
if (this.getLeft().getRight()!=null){
this.setLeft(this.getLeft().getRight());
return;
}
}
//右子树等于
if (this.getRight()!=null && this.getRight().getValue()==i){
//都为空
if (this.getRight().getLeft()==null && this.getRight().getRight()==null){
this.setRight(null);
return;
}
//都不为空
if (this.getRight().getLeft()!=null && this.getRight().getRight()!=null){
Node rightTree = this.getRight().getRight();
Node minNode = rightTree.minValue();
Node parentOfMin = this.searchParent(minNode);
if (parentOfMin.getLeft()!=null &&parentOfMin.getLeft()==minNode){
parentOfMin.setLeft(null);
}else {
parentOfMin.setRight(null);
}
this.getRight().setValue(minNode.getValue());
return;
}
//左不为空
if (this.getRight().getLeft()!=null ){
this.setRight(this.getRight().getLeft());
return;
}
//右不为空
if (this.getRight().getRight()!=null){
this.setRight(this.getRight().getRight());
return;
}
}
if (this.getLeft()!=null){
this.getLeft().delete(node);
}
if (this.getRight()!=null){
this.getRight().delete(node);
}
}
public Node searchParent(Node temp){
if (this.getLeft()==temp||this.getRight()==temp){
return this;
}
if (this.getLeft()!=null && temp.getValue()<this.getValue()){
return this.getLeft().searchParent(temp);
}
if (this.getRight()!=null && temp.getValue()>this.getValue()){
return this.getRight().searchParent(temp);
}
return null;
}
public Node minValue(){
Node node=this;
while (node.getLeft()!=null){
node=node.getLeft();
}
return node;
}
/**
* 中序遍历
*/
public void infoOrder(){
if (this.getLeft()!=null){
this.getLeft().infoOrder();
}
System.out.println(this);
if (this.getRight()!=null){
this.getRight().infoOrder();
}
}
//添加
public void add(Node node){
if (node.getValue()>this.getValue()){
if (this.getRight()==null){
this.setRight(node);
}else {
this.getRight().add(node);
}
}
if (node.getValue()<=this.getValue()){
if (this.getLeft()==null){
this.setLeft(node);
}else {
this.getLeft().add(node);
}
}
}
public Node(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
}
共勉!
这篇博客介绍了如何使用Java实现二叉排序树(BST)的基本操作,包括插入、删除和中序遍历。代码展示了如何在BST中查找节点的父节点以及找到最小值节点来处理删除操作。通过示例展示了不同情况下删除节点的逻辑,包括节点无子节点、只有一个子节点和有两个子节点的情况。
4315

被折叠的 条评论
为什么被折叠?



