开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第二十三天,点击查看活动详情
二叉树-删除节点
要求
- 如果删除的节点是叶子节点,则删除该节点
- 如果删除的节点是非叶子节点,则删除该子树.
- 测试,删除掉 5号叶子节点 和 3号子树.
思路步骤:
- 若树为空(root 为空 ),若只有一个root结点,则等价于二叉树置空
- 因为当前的二叉树是单向的,判断当前节点的子结点是否需要删除节点,而不能去判断当前结点是否需要删除结点
- 如果当前节点的左子树不为空,并且左子结点就是要删除结点,就将 this.left =null ,并且返回(递归删除)
- 如果当前节点的右子树不为空,并且右子结点就是要删除结点,就将 this.right =null ,并且返回(递归删除)
- 第2和第3都没有删除结点,则进行向左子树递归删除
- 第4步左子树没有删除成功,则进行右子树递归删除
二叉树的遍历、查找、删除完整代码
package com.tree;
/**
* @author Kcs 2022/9/10
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
//创建二叉树
BinaryTree binaryTree = new BinaryTree();
//创建节点
HeroNode root = new HeroNode(1, "宋江");
HeroNode node2 = new HeroNode(2, "吴用");
HeroNode node3 = new HeroNode(3, "卢俊义");
HeroNode node4 = new HeroNode(4, "林冲");
HeroNode node5 = new HeroNode(5, "关胜");
//手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
//前序遍历
System.out.println("前序遍历:");
binaryTree.preOrder();
//中序遍历
System.out.println("中序遍历:");
binaryTree.infixOrder();
//后续遍历
System.out.println("后序遍历:");
binaryTree.postOrder();
//前序遍历查找 查找了4 次
System.out.println("前序遍历查找~~~");
HeroNode resNode = binaryTree.preOrderSearch(5);
if (resNode != null) {
System.out.printf("找到了!,HeroNode{no = %d name = %s}", resNode.getNo(), resNode.getName());
} else {
System.out.printf("该编号 no = %d 的人物不存在!", 5);
}
System.out.println();
//中序遍历查找 查找了 3 次
System.out.println("中序遍历查找~~~");
resNode = binaryTree.infixOrderSearch(5);
if (resNode != null) {
System.out.printf("找到了!,HeroNode{no = %d name = %s}", resNode.getNo(), resNode.getName());
} else {
System.out.printf("该编号 no = %d 的人物不存在!", 5);
}
System.out.println();
//后序遍历查找 查找了 5 次
System.out.println("后序遍历查找~~~");
resNode = binaryTree.postOrderSearch(5);
if (resNode != null) {
System.out.printf("找到了!,HeroNode{no = %d name = %s}", resNode.getNo(), resNode.getName());
} else {
System.out.printf("该编号 no = %d 的人物不存在!", 5);
}
//删除结点
System.out.println("删除前,前序遍历");
binaryTree.preOrder();
// binaryTree.delNode(5);
//删除子树
binaryTree.delNode(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 delNode(int no) {
if (root != null) {
//root是否为删除的结点
if (root.getNo() == no) {
root = null;
} else {
// 递归删除
root.deleteNode(no);
}
} else {
System.out.println("该树为空,无法删除!");
}
}
}
/**
* 创建 herNode节点
*/
class HeroNode {
/**
* 序号
*/
private int no;
/**
* 姓名
*/
private String name;
/**
* 左节点,默认null
*/
private HeroNode left;
/**
* 右节点,默认null
*/
private HeroNode right;
public HeroNode(int no, String name) {
this.no = no;
this.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;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + ''' +
'}';
}
/**
* 前序遍历,输出:中=>左=>右
*/
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);
}
/**
* 前序遍历查找 输出:中=>左=>右
* @param no 查找no
* @return 返回Node,为空返回null
*/
public HeroNode preOrderSearch(int no) {
System.out.println("进入前序查找");
//首先判断当前节点是否为no
if (this.no == no) {
return this;
}
//结果节点
HeroNode resNode = null;
//判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
if (this.left != null) {
resNode = this.left.preOrderSearch(no);
}
//左子树查找到节点,则返回
if (resNode != null) {
//查到结果
return resNode;
}
//右子树递归后序查找,判断当前节点的右子节点是否为空,不为空,则继续向右递归查找。
if (this.right != null) {
resNode = this.right.preOrderSearch(no);
}
return resNode;
}
/**
* 中序遍历查找 输出:左=>中=>右
* @param no 查找no
* @return 返回Node,为空返回null
*/
public HeroNode infixOrderSearch(int no) {
//结果节点
HeroNode resNode = null;
//判断当前节点的 左子节点 是否为空,如果不为空,则递归中序查找
if (this.left != null) {
resNode = this.left.infixOrderSearch(no);
}
//左子树查找到节点,则返回
if (resNode != null) {
//查到结果
return resNode;
}
System.out.println("进入中序查找");
//左子树没有找到,则判断当前节点是否为no
if (this.no == no) {
return this;
}
//右子树递归后序查找,判断当前节点的 右子节点 是否为空,不为空,则继续向右递归查找。
if (this.right != null) {
resNode = this.right.infixOrderSearch(no);
}
return resNode;
}
/**
* 后序遍历查找 输出:左=>右=>中
* @param no 查找no
* @return 返回Node,为空返回null
*/
public HeroNode postOrderSearch(int no) {
//结果节点
HeroNode resNode = null;
//判断当前节点的 左子节点 是否为空,如果不为空,则递归 后序查找
if (this.left != null) {
resNode = this.left.postOrderSearch(no);
}
//左子树后序查找到节点,则返回
if (resNode != null) {
//查到结果
return resNode;
}
//右子树递归后序查找,判断当前节点的 右子节点 是否为空,不为空,则继续向右递归查找。
if (this.right != null) {
resNode = this.right.postOrderSearch(no);
}
System.out.println("进入后序查找");
//左右子树都没有找到,判断当前节点是否为no
if (this.no == no) {
return this;
}
return resNode;
}
/**
* 递归删除结点
* 1. 如果删除的节点是叶子节点,则删除该节点
* 2. 如果删除的节点是非叶子节点,则删除该子树
*/
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);
}
}
}