package com.chj.BinaryOrderTree;
import java.util.HashMap;
import java.util.Map;
/**
-
@author chj
-
@create 2021-08-12-10:41
*/
public class test{
public static void main(String[] args) {
int[] arr={7,3,10,12,15,1,9,2};
BinarySortTree binarySortTree=new BinarySortTree();
for(int i=0;i<arr.length;i++){
binarySortTree.add(new Node(arr[i]));
}
binarySortTree.infixOrder();
System.out.println();
binarySortTree.delete(10);
binarySortTree.infixOrder();}
}
class BinarySortTree{
private Node root;
public void add(Node node){
if (rootnull){
root=node;
}else {
root.add(node);
}
}
/中序遍历,得到的排列刚好从小到大排列/
public void infixOrder(){
if (rootnull){
System.out.println(“树为空”);
}
else {
root.infixOrder();
}
}
//找到要删除的节点本身和其父节点
public Map search(int value){
Map search=new HashMap();
if (rootnull){
System.out.println(“树空”);
}else {
if (root.valuevalue){
search.put(root,root);
}else {
search = root.search(value);
}
}
return search;
}
/删除节点,分四种情况/
public void delete(int value){
Map search = search(value);
Node parent=null;
Node target=null;
/1.没找到/
if (searchnull||search.size()0){
System.out.println(“没找到”);
}else{
for(Object entry:search.entrySet()) {
Map.Entry entry1=(Map.Entry)entry;
parent = (Node) entry1.getKey();
target=(Node)entry1.getValue();
}
/2.要删除的节点是叶子结点/
if (target.leftnull&&target.rightnull){
if (parent.lefttarget){
parent.left=null;
}else {
parent.right=null;
}
/3.要删除的节点是带有左节点和右节点的根节点/
}else if (target.left!=null&&target.right!=null){
//右子节点找最小
int rightMin = getRightMin(target.right);
target.value=rightMin;
/4.要删除的节点带有单一的左节点或者右节点/
} else{
if (parent.lefttarget){
if (target.left!=null){
parent.left=target.left;
}else {
parent.left=target.right;
}
}else {
if (target.left!=null){
parent.right=target.left;
}else {
parent.right=target.right;
}
}
}
}
}
/得到该节点右子树上的最小权值/
public int getRightMin(Node node){
Node temp=node;
while (temp.left!=null){
temp=temp.left;
}
delete(temp.value);
return temp.value;
}
}
class Node {
public int value;
public Node left;
public Node right;
private Map map=new HashMap();
public Node() {
}public Node(int value) {
this.value = value;
}public int getValue() {
return value;
}public void setValue(int value) {
this.value = value;
}public Node getLeft() {
return left;
}public void setLeft(Node left) {
this.left = left;
}public Node getRight() {
return right;
}public void setRight(Node right) {
this.right = right;
}
/增加节点/
public void add(Node node){
if (nodenull){
return;
}
if (this.value>node.value){
if (this.leftnull){
this.left=node;
}else {
this.left.add(node);
}
}else{
if (this.rightnull){
this.right=node;
}else {
this.right.add(node);
}
}
}
/查询要删除的目标节点和其父节点,并将其保存进一个map里/
public Map search(int value){
Node parent=this;
if (value<parent.value){
if (parent.left.valuevalue){
map.put(parent,parent.left);
return map;
}
return parent.left.search(value);
}else {
if (parent.right.value==value){
map.put(parent,parent.right);
return map;
}
return parent.right.search(value);
}
}
/中序遍历/
public void infixOrder(){
if (this.left!=null){
this.left.infixOrder();
}
System.out.print(this.value+"\t");
if (this.right!=null){
this.right.infixOrder();
}
}@Override
public String toString() {
return “Node{” +
“value=” + value +
‘}’;
}
}