1、二叉树---删除节点
1)如果删除的节点是叶子结点,则删除该节点。
2)如果删除的节点是非叶子结点,则删除该子树。
2、二叉树---删除节点思路分析
1)如果数是空数root,如果只有一个root节点,则等价于将二叉树置空。
2)因为二叉树是单向的,因此我们判断当前节点的子节点是否是需要删除的节点,而不能判断当前节点是不是需要删除的节点。
3)如果当前节点的左子节点不为空,并且左子节点就是要删除的结点,就将this.left=null,并且返回(结束递归删除)。
4)如果当前节点的右子节点不为空,并且右子节点就是要删除的结点,就将this.right=null,并且返回(结束递归删除)。
5) 如果第三步和第四步没有删除节点,那就需要向左子树进行递归删除。
6)如果第5步也没有删除节点,就需要向左子树进行递归删除。
3 代码实现
package BinaryTreeDemo;
public class BinaryTreeDemo {
public static void main(String[] args) {
//先需要创建一颗二叉树
BinaryTree binaryTree = new BinaryTree();
//创建需要的节点
HeroNode root=new HeroNode(1,"宋江");
HeroNode noed2=new HeroNode(2,"吴用");
HeroNode noed3=new HeroNode(3,"卢俊义");
HeroNode noed4=new HeroNode(4,"林冲");
HeroNode noed5=new HeroNode(5,"关胜");
//我们先手动创建二叉树
root.setLeft(noed2);
root.setRight(noed3);
noed3.setRight(noed5);
noed3.setLeft(noed4);
binaryTree.setRoot(root);
//测试删除代码
System.out.println("删除前,前序遍历");
binaryTree.preOrder();
binaryTree.deNode(5);
System.out.println("删除后,前序遍历");
binaryTree.preOrder();
}
}
//先创建HeroNode节点
class HeroNode{
private int no;
private String name;
private HeroNode left;//默认 null
private HeroNode right;//默认null
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 delNode(int no){
//3如果当前节点的左子节点不为空,并且左子节点就是要删除的结点,就将this.left=null,并且返回(结束递归删除)
if (this.left!=null && this.left.no==no){
this.left=null;
return;
}
//4如果当前节点的右子节点不为空,并且右子节点就是要删除的结点,就将this.right=null,并且返回(结束递归删除)
if (this.right!=null && this.right.no==no){
this.right=null;
return;
}
//5如果第三步和第四步没有删除节点,那就需要向左子树进行递归删除
if (this.left!=null){
this.left.delNode(no);
}
//6 如果第5步没有删除节点,那就需要向右子树进行递归删除
if (this.right!=null){
this.right.delNode(no);
}
}
//编写前序遍历的方法
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);//先输出父节点
}
}
//定义BinaryTree二叉树
class BinaryTree{
private HeroNode root;
public void setRoot(HeroNode root) {
this.root = root;
}
//删除节点
public void deNode(int no){
if (root!=null){
//如果只有一个root节点,这里就要立即判断root是不是需要删除的节点
if (root.getNo()==no){
root=null;
}else {
//递归删除
root.delNode(no);
}
}else {
System.out.println("空树,不能删除");
}
}
//前序遍历
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("二叉树为空,无法遍历");
}
}
}