package binarysorttree;
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i = 0; i < arr.length; i++) {
binarySortTree.add(new Node(arr[i]));
}
System.out.println("中序遍历结果为: ");
binarySortTree.infixOrder();
Node node = binarySortTree.search(3);
System.out.println("查找结果为: " + node);
Node parent = binarySortTree.searchParent(7);
System.out.println("父节点为: " + parent);
binarySortTree.deleteNode(2);
binarySortTree.deleteNode(5);
binarySortTree.deleteNode(9);
binarySortTree.deleteNode(12);
binarySortTree.deleteNode(7);
binarySortTree.deleteNode(3);
binarySortTree.deleteNode(10);
binarySortTree.deleteNode(1);
System.out.println("删除节点后的遍历结果: ");
binarySortTree.infixOrder();
}
}
class BinarySortTree{
private Node root;
public void deleteNode(int value){
if(root == null){
return;
}else{
Node targetNode = search(value);
if(targetNode == null){
return;
}
Node parent = searchParent(value);
if(root.getLeft() == null && root.getRight() == null){
root = null;
return;
}
if(targetNode.getLeft() == null && targetNode.getRight() == null){
if(parent.getLeft() != null && parent.getLeft().getValue() == value){
parent.setLeft(null);
}
if(parent.getRight() != null && parent.getRight().getValue() == value){
parent.setRight(null);
}
}else if(targetNode.getLeft() != null && targetNode.getRight() != null){
int min = getMinValue(targetNode.getRight());
targetNode.setValue(min);
}else{
if(targetNode.getLeft() != null){
if(parent == null){
root = targetNode.getLeft();
}else{
if(parent.getLeft().getValue() == value){
parent.setLeft(targetNode.getLeft());
}else{
parent.setRight(targetNode.getLeft());
}
}
}else{
if(parent == null){
root = targetNode.getRight();
}else{
if(parent.getLeft().getValue() == value){
parent.setLeft(targetNode.getRight());
}else{
parent.setRight(targetNode.getRight());
}
}
}
}
}
}
public int getMinValue(Node node){
Node target = node;
while(target.getLeft() != null){
target = target.getLeft();
}
deleteNode(target.getValue());
return target.getValue();
}
public void add(Node node){
if(root == null){
this.root = node;
}else{
this.root.add(node);
}
}
public void infixOrder(){
if(root != null){
this.root.infixOrder();
}else {
System.out.println("根节点为空,无法遍历");
}
}
public Node search(int value){
if(this.root == null){
return null;
}
return this.root.search(value);
}
public Node searchParent(int value){
if(this.root == null){
return null;
}
return this.root.searchParent(value);
}
}
class Node{
private int value;
private Node left;
private Node right;
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;
}
@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(this.right == null){
this.right = node;
}else{
this.right.add(node);
}
}
}
public Node searchParent(int value){
if((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)){
return this;
}else{
if(this.value > value && this.left != null){
return this.left.searchParent(value);
}else if(this.value <= value && this.right != null){
return this.right.searchParent(value);
}else{
return null;
}
}
}
public Node search(int value){
if(this.value == value){
return this;
}else if(this.value > 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 infixOrder(){
if(this.left != null){
this.left.infixOrder();
}
System.out.println(this);
if(this.right != null){
this.right.infixOrder();
}
}
}