package bst;
import java.math.BigInteger;
/**
* @author yzy
* @version 1.0
*/
//应该尽量避免相同的情况
public class BST {
//main
public static void main(String[] args) {
int[] arr = {7,3,10,12,5,1,9,0};
BinarySortTree bst = new BinarySortTree();
for(int value:arr){
bst.add(new Node(value));
}
//测试
bst.delete(7);
bst.minOrder();
}
}
//树
class BinarySortTree{
//node类中写添加等,是底层代码,在bst树中需要对其进行封装,使得在主函数能直接调用bst的方法
Node root;
//添加
public void add(Node node){
if(node == null){
return;
}
if(root == null){
root = node;
}else {
root.add(node);
}
}
//遍历
public void minOrder(){
if(root != null){
root.midOrder();
}
}
//查找待删节点
public Node search(int value){
if(root == null){
return null;
}else {
return root.search(value);
}
}
//查找父节点
public Node searchParentNode(int value){
if(root == null){
return null;
}else {
return root.searchParentNode(value);
}
}
//删除
public void delete(int value){
Node target = root.search(value);
Node parent = root.searchParentNode(value);
Node temp;
if(target == null || root == null){
System.out.println("不可删除");
return;
}else {
if(target.left == null && target.right == null){//情况一叶子结点
//有了target不一定有parent
if(parent == null){
root = null;
}
if(parent.value > target.value){
parent.left = null;
}else {
parent.right = null ;
}
}else if(target.left == null && target.right != null){//情况二
//有了target不一定有parent
if(parent == null){
root = target.right;
}
if(parent.value > target.value){
parent.left = target.right;
}else {
parent.right = target.right ;
}
}else if(target.left != null && target.right == null){//情况三
//有了target不一定有parent
if(parent == null){
root = target.left;
}
if(parent.value > target.value){
parent.left = target.left;
}else {
parent.right = target.left ;
}
}else {//情况四,两个子树的结点
temp = target.right;//表示右子树的最小值
while(true){
if(temp.left != null){
temp = temp.left;
continue;
}else {
break;
}
}
Node tempParent = root.searchParentNode(temp.value);
target.value = temp.value;
//删除叶子结点
if(temp.value < tempParent.value){
tempParent.left = null;
}else {
tempParent.right = null;
}
}
}
}
}
//节点类
class Node{
int value;
Node left;
Node right;
//构造器
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 midOrder(){
if(this.left != null){
this.left.midOrder();
}
System.out.println(this);
if(this.right != null){
this.right.midOrder();
}
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
//查找
public Node search(int value){
if(this.value == value){
return this;
}
if(this.left != null){
if(value < this.value){
return this.left.search(value);
}
}
if(this.right != null){
if(value >= this.value){
return this.right.search(value);
}
}
return null;
}
//查找待删节点的父节点
public Node searchParentNode(int value){
if (this.search(value) == null){
return null;
}
//根节点的父节点返回null
if(value == this.value){
return null;
}
if((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value) ){
return this;
}else if(value < this.value){
return this.left.searchParentNode(value);
}else {
return this.right.searchParentNode(value);
}
}
}
二叉排序树BST代码java版
最新推荐文章于 2024-09-10 10:21:38 发布