Delete the specified node in the binary tree

Delete node in binary tree:

1. Requirements:

  • If the deleted node is a leaf node, delete the node.
  • If the deleted node is a non-leaf node, the subtree is deleted.

2. Ideas:

  • First process the root node: determine whether the tree is empty, if there is only one node, determine whether this node is the node to be deleted.
  • Because the binary tree is one-way, we judge whether the current child node needs to delete the node, but cannot judge whether the current node needs to be deleted.
  • If the left child node of the current node is not empty, and the left child node is the node to be deleted, then this.left = null; and return (end recursive deletion)
  • If the right child node of the current node is not empty, and the right child node is the node to be deleted, set this.right = null; and return (end recursive deletion)
  • If the above steps are not deleted, then you need to delete the left and right subtrees recursively.

3. Code demonstration:

Insert picture description here

(1) Main method:


public class binaryTreeDemo {
    
    
	public static void main(String[] args) {
    
    
	//先创建一个二叉树
		BinaryTree binaryTree = new BinaryTree();
	//创建需要的结点
		HeroNode root = new HeroNode(1,"1");
		HeroNode node2 = new HeroNode(2,"2");
		HeroNode node3 = new HeroNode(3,"3");
		HeroNode node4 = new HeroNode(4,"4");
		HeroNode node5 = new HeroNode(5,"5");
		
	//说明:手动创建二叉树
		root.setLeft(node2);
		root.setRight(node3);
		node3.setLeft(node5);
		node3.setRight(node4);
	//测试:
		binaryTree.setRoot(root);
		System.out.println("删除前,前序遍历");
		binaryTree.centerOrder();
		binaryTree.del(5);
		System.out.println("删除后,前序遍历");
		binaryTree.centerOrder();
	}
}

(2) Define a binary tree:

//定义一个二叉树
class binaryTree{
    
    
	//根节点
	private Node root;
	//一个set方法
	public void setRoot(Node root) {
    
    
		this.root = root;
	}
	
	//删除结点
	public void del(int no){
    
    
		if(this.root!=null){
    
    
			//是否只有一个结点,这里立即判断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("二叉树为空,无法遍历");
		}
	}
	
}

(3) Create a node:

//创建结点
class Node{
    
    
	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 + "]";
	}

	//递归删除节点
	//1、如果删除的结点是叶子结点,则删除该结点
	//2、如果删除的结点是非叶子结点,则删除该子树
	public void delNode(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.delNode(no);
		}
		//向右子树进行递归删除
		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();
		}
	}

}

Code analysis:

del method: Taking the current root node 1 as an example, first enter the del method to determine whether the current node is empty and the node to be searched, if not, enter the delNode method.

//删除结点
	public void delNode(int no){
    
    
		if(this.root!=null){
    
    
			//是否只有一个结点,这里立即判断root是不是就是要删除结点
			if(root.getNo() == no) {
    
    
				root = null;
			}else {
    
    
				//递归删除
				root.delNode(no);
			}
		}else{
    
    
			System.out.println("空树不能删除");
		}
	}

delNode Method:

public void delNode(int no) {
    
    
		//如果当前结点的左子结点不为空,并且左子结点就是要删除的结点,就将this.left = null,并且就返回(结束递归删除)
		if(this.left!=null && this.left.no == no) {
    
    
			this.left = null;
			return;
		}
		//如果当前结点的右子结点不为空,并且右子结点就是要删除的结点,就将this.right = null,并且就返回(结束递归删除)
		if(this.right!=null && this.right.no == no) {
    
    
			this.right = null;
			return;
		}
		
		//向左子树进行递归删除
		if(this.left!=null) {
    
    
			this.left.delNode(no);
		}
		//向右子树进行递归删除
		if(this.right!=null) {
    
    
			this.right.delNode(no);
		}
		
	}
  1. At this time this.left refers to 2, but this node is not the node we are looking for.
  2. Then proceed to the right node judgment, at this time this.right refers to 3 is not the node we are looking for.
  3. Go back to the root node (recursion) and delete the left subtree recursively. At this time, the left node is 2 and is not empty. Enter the delNode function (pass), this.left is empty, this.right is empty, and the left subtree If the attempt to delete recursively fails, and the attempt to delete the right subtree fails, then return to node 1 (recursion).
  4. Recursively delete the right subtree. At this time, the left node this.left is not empty and is the number 5 we are looking for. At this time, the left side of number 3 will be empty.
  5. At this point, the code returns to the else in binaryTree
  6. Back to the end of the main method.

Guess you like

Origin blog.csdn.net/weixin_43690348/article/details/110587591