package com.atguigu.binaryTree;
import java.util.Objects;
public class BinaryTree {
public static void main(String[] args) {
int[] arr = {7,3,10,12,5,1,9,2};
BinaryTreeTest tree = new BinaryTreeTest();
for (int num: arr) {
tree.add(new Node(num));
}
System.out.println("中序遍历的结果为");
tree.infixOrder();
tree.del(2);
tree.del(5);
tree.del(9);
tree.del(12);
tree.del(7);
tree.del(3);
System.out.println("删除后的中序遍历结果为");
tree.infixOrder();
}
}
class BinaryTreeTest{
public Node root;
public BinaryTreeTest() {
}
public BinaryTreeTest(Node root) {
this.root = root;
}
public void add(Node node){
if (root == null) {
root = node;
} else {
root.add(node);
}
}
public void del(int values){
if (searchNode(values) == null){
System.out.println("待删除的节点不存在");
}else {
Node delNode = searchNode(values);
Node parent = searchParent(values);
boolean flag1 = (delNode.left != null);
boolean flag2 = (delNode.right != null);
boolean flagpar = !(parent == null);
Node temp;
if (!flag1 && !flag2){
if (parent == null){
root =null;
}else {
if (parent.left != null && parent.left.equals(delNode)){
parent.left = null;
}else if (parent.right != null && parent.right.values == values){
parent.right = null;
}
}
}else if ( (flag1 && !flag2 )
||(!flag1 && flag2)){
if (flag1 && !flagpar){
root = delNode.left;
}else if (flag1 && flagpar){
if (parent.left != null && parent.left.values == values){
parent.left = delNode.left;
}else if (parent.right != null && parent.right.values == values){
parent.right = delNode.left;
}
}else if (flag2 && !flagpar){
root = delNode.right;
}else if (flag2 && flagpar){
if (parent.left != null && parent.left.values == values){
parent.left = delNode.right;
}else if (parent.right != null && parent.right.values == values){
parent.right = delNode.right;
}
}
}else if (flag1 && flag2){
temp = delNode.left.fingMax();
del(delNode.left.fingMax().values);
delNode.values = temp.values;
}
}
}
public Node searchNode(int values){
if (root.values == values){
return root;
}else{
return root.search(values);
}
}
public Node searchParent(int values){
if (root.values == values){
return null;
}else {
return root.searchParents(values);
}
}
public void infixOrder(){
if (root == null){
System.out.println("空树");
}else{
root.infixOrder();
}
}
}
class Node{
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return values == node.values;
}
public Node fingMin(){
if (this.left == null ){
return this;
}else {
return this.left.fingMin();
}
}
public Node fingMax(){
if (this.right == null ){
return this;
}else {
return this.right.fingMax();
}
}
@Override
public int hashCode() {
return Objects.hash(values);
}
public int values;
Node left;
Node right;
public Node search(int values){
if (this.values == values){
return this;
}else if (this.left != null && this.values > values){
return this.left.search(values);
}else if (this.right != null && this.values <= values){
return this.right.search(values);
}else {
return null;
}
}
public Node searchParents(int values){
if (this.values == values){
return null;
}
if ((this.left != null && this.left.values == values) ||
this.right != null && this.right.values == values ){
return this;
}else if (this.left != null && this.values > values){
return this.left.searchParents(values);
}else if (this.right != null && this.values <= values){
return this.right.searchParents(values);
}else {
return null;
}
}
public Node(Node left) {
this.left = left;
}
public Node(int values) {
this.values = values;
}
public void infixOrder(){
if (this.left != null){
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null){
this.right.infixOrder();
}
}
public void add(Node node){
if (node == null){
return;
}else{
if (node.values < this.values){
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);
}
}
}
}
@Override
public String toString() {
return "Node{" +
"values=" + values +
'}';
}
}