package demo1;
public class BinarySortTree {
public static void main(String[] args) {
//测试
int[] arr = {7,3,10,12,5,1,9,4};
CreatTree creatTree = new CreatTree();
for (int i = 0; i < arr.length; i++) {
creatTree.addTree(new Node(arr[i]));
}
creatTree.showTree();
// creatTree.deleteNode(12);
System.out.println("---------------------");
//creatTree.showTree();
//creatTree.deleteNode(5);
creatTree.deleteNode(7);
creatTree.showTree();
}
}
//创建二叉排序树
class CreatTree{
private Node root;
//添加树
public void addTree(Node node){
if (root == null){
root = node;
}else {
root.add(node);
}
}
//遍历树
public void showTree(){
root.midList();
}
//找寻需要删除的节点
public Node targetNode(int value){
if (root == null){
return null;
}
return root.targetNode(value);
}
//找寻需要删除节点的父节点
public Node parentNode(int value){
if (root == null){
return null;
}
return root.parentNode(value);
}
//删除一棵树里面的最小节点,并返回最小值
public int deleteMin(Node node){
Node temp = node;
while (temp.left != null){
temp = temp.left;
}
//此时temp指向最小值
//删除最小值也就是叶子节点
deleteNode(temp.getValue());
return temp.getValue();
}
//删除节点
public void deleteNode(int value){
if (root == null){
return;
}
//找到目标节点
Node targetNode = targetNode(value);
if (targetNode == null){
return;
}
//判断是否只有一个节点,因为它没有父节点
if (root.left == null && root.right == null){
root = null;
return;
}
//找到父节点
Node parentNode = parentNode(value);
//判断要删除的是不是叶子节点
if (targetNode.left == null && targetNode.right == null){
//说明是叶子节点
//判断是左子节点还是右子节点对应删除
if (parentNode.left != null && parentNode.left.getValue() == value){
//说明是左子节点
parentNode.left = null;
}else if (parentNode.right != null && parentNode.right.getValue() == value){
//说明是右子节点
parentNode.right = null;
}
}else if(targetNode.left != null && targetNode.right != null){
//说明要删除的有两个子节点
int min = deleteMin(targetNode.right);
targetNode.setValue(min);
}else {
//判断要删除的是不是只有一颗子树的节点
//判断要删除的节点有左子节点还是右子节点
//要删除的节点是左子节点
if (targetNode.left != null){
//判断要删除的节点是父节点的左子节点还是右子节点
if (parentNode != null) {
if (parentNode.left != null && parentNode.left.getValue() == value) {
parentNode.left = targetNode.left;
} else {
parentNode.right = targetNode.left;
}
}else {
root = targetNode.left;
}
}
//要删除的节点有右子节点
else {
//判断要删除的节点是父节点的左子节点还是右子节点
if (parentNode != null) {
if (parentNode.left != null && parentNode.left.getValue() == value) {
parentNode.left = targetNode.right;
} else {
parentNode.right = targetNode.right;
}
}else {
root = targetNode.right;
}
}
}
}
}
//创建节点
class Node{
private int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
public Node() {
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
//找到要删除的目标节点
public Node targetNode(int value){
if (this.value == value){
return this;
}else if (value < this.value){
if (this.left != null){
return this.left.targetNode(value);
}else {
return null;
}
}else if (value > this.value){
if (this.right != null){
return this.right.targetNode(value);
}else {
return null;
}
}else {
return null;
}
}
//找到要删除节点的父节点
public Node parentNode(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.parentNode(value);
}else if (value >= this.value && this.right != null){
return this.right.parentNode(value);
}else {
return null;
}
}
}
@Override
public String toString() {
return "Node{" +
"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 (node.value >= this.value){
if (this.right == null){
this.right = node;
}else {
this.right.add(node);
}
}
}
//中序遍历节点
public void midList(){
if (this.left != null){
this.left.midList();
}
System.out.println(this);
if (this.right != null){
this.right.midList();
}
}
}
二叉排序树节点创建与删除
于 2023-12-07 11:03:37 首次发布