删除的思路
- 判断当前节点的子节点是否需要删除,而不是去判断当前节点是否需要删除
- 如果当前节点的左子节点不为空,并且左子节点就是要删除的节点,就将this.left=null,并且返回,结束递归
- 如果当前节点的右子节点不为空,并且右子节点就是要删除的节点,就将this.right=null,并且返回,结束递归
- 如果2,3没有完成删除节点,则需要向左子树进行递归删除
- 如果4没有完成删除节点,则需要向右子树进行递归删除
- 如果树是空树,树只有一个root节点,则等价将二叉树置空
package com.hanlin.tree;
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
HeroNode root = new HeroNode(1, "root");
HeroNode node2 = new HeroNode(2, "node2");
HeroNode node3 = new HeroNode(3, "node3");
HeroNode node4 = new HeroNode(4, "node4");
HeroNode node5 = new HeroNode(5, "node5");
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
System.out.println("删除前的树!");
binaryTree.preOrder();
binaryTree.deleteNode(3);
System.out.println("删除后的树!");
binaryTree.preOrder();
}
}
class BinaryTree{
private HeroNode root;
public void setRoot(HeroNode root) {
this.root = root;
}
public void preOrder(){
if(this.root != null){
this.root.preOrder();
}else {
System.out.println("当前二叉树为空,无法遍历!");
}
}
public void infixOrder(){
if(this.root != null){
this.root.infixOrder();
}else {
System.out.println("当前二叉树为空,无法遍历!");
}
}
public void postOrder(){
if(this.root != null){
this.root.postOrder();
}else {
System.out.println("当前二叉树为空,无法遍历!");
}
}
public HeroNode preOrderSearch(int no){
if(root != null) {
return root.preOrderSearch(no);
}else {
return null;
}
}
public HeroNode infixOrderSearch(int no) {
if(root != null) {
return root.infixOrderSearch(no);
}else {
return null;
}
}
public HeroNode postOrderSearch(int no) {
if(root != null) {
return root.postOrderSearch(no);
}else {
return null;
}
}
public void deleteNode(int no) {
if(this.root != null) {
if(root.getNo() == no) {
root = null;
}else {
root.deleteNode(no);
}
}else {
System.out.println("空树,不能删除!");
}
}
}
class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
public void preOrder(){
System.out.println(this);
if(this.left != null) {
this.left.preOrder();
}
if(this.right != null) {
this.right.preOrder();
}
}
public void infixOrder(){
if(this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if(this.right != null) {
this.right.infixOrder();
}
}
public void postOrder(){
if(this.left != null) {
this.left.postOrder();
}
if(this.right != null) {
this.right.postOrder();
}
System.out.println(this);
}
public HeroNode preOrderSearch(int no){
if(this.no == no) {
return this;
}
HeroNode resultNode = null;
if(this.left != null) {
resultNode = this.left.preOrderSearch(no);
}
if(resultNode != null) {
return resultNode;
}
if(this.right != null) {
resultNode = this.right.preOrderSearch(no);
}
return resultNode;
}
public HeroNode infixOrderSearch(int no) {
HeroNode resultNode = null;
if(this.left != null) {
resultNode = this.left.infixOrderSearch(no);
}
if(resultNode != null) {
return resultNode;
}
if(this.no == no) {
return this;
}
if(this.right != null) {
resultNode = this.right.infixOrderSearch(no);
}
return resultNode;
}
public HeroNode postOrderSearch(int no) {
HeroNode resultNode = null;
if(this.left != null) {
resultNode = this.left.postOrderSearch(no);
}
if(resultNode != null) {
return resultNode;
}
if(this.right != null) {
resultNode = this.right.postOrderSearch(no);
}
if(resultNode != null) {
return resultNode;
}
if(this.no == no) {
return this;
}
return resultNode;
}
public void deleteNode(int no){
if(this.left !=null && this.left.no == no) {
this.left = null;
return;
}
if(this.right != null && this.right.no == no) {
this.right = null;
return;
}
if(this.left != null) {
this.left.deleteNode(no);
}
if(this.right != null) {
this.right.deleteNode(no);
}
}
}