Delete a node from a binary tree

Source: Internet
Author: User

The deletion of a binary tree can be regarded as the most complex operation of a binary tree. Many situations should be taken into account during the deletion:
1. The deleted node is a leaf node.
2. Only the left child node is deleted.
3. Only the right child node is deleted.
4. Two deleted Child Nodes

Therefore, the four cases must be taken into account during deletion. In the four cases, there will be a detailed division. The following describes how to delete them.

To delete a node in a binary tree, you first need to find the node. Because the binary tree follows the principle when inserting the node, the value of the node is used to determine
The location where the node will be inserted (or add a key to the node, which is determined by the key ). Start from the root node, and smaller (or equal to) than the current node will be inserted
On the left of the current node, the node larger than the current node will be inserted to the right of the current node. Therefore, find the node to be deleted based on the Binary Tree insertion rules. The Code is as follows;

// Save the current Node parent = root; // Save the current Node parent Node boolean isLeft = true; // check whether the record is at the left: // locate the node to be deleted while (key! = Curr. key) {if (key <= curr. key) {isLeft = true; // pass through the left node if (curr. left! = Null) {parent = curr; curr = curr. left ;}} else {isLeft = false; // pass through the right node if (curr. right! = Null) {parent = curr; curr = curr. right ;}}if (curr = null) {return false ;}}


In this Code, two references are defined first, one representing the current node, and the other representing the parent node of the current node, and then a while loop is entered,
In the loop, we first determine whether to go to the left or to the right based on the key comparison. If we go to the left, it indicates that the current node is the left child of its parent node.
Set ifLeft to true. If you go to the right, set isLeft to false (isLeft will be used later ). Now, go down.
It is determined, and then it determines whether the passed node (that is, the current node) is empty. If the left and right nodes are empty, it indicates that the node you want to delete is not found, and the program
False is returned. If the left child of the current node is not empty, the current node is equal to its left child, which is equivalent to taking another step. The process of searching is
Keep judging based on the value and keep moving down until the conditions are met and the cycle stops. At this time, the current node will save the reference of the node to be deleted, and the deleted
Who is the parent node, and who is the parent node of the deleted node knows.
After finding the deleted node, we can see how to delete it.
1. If the deleted node is a leaf node, the Code indicates
if(curr.left==null&&curr.right==null){if(curr==root){root=null;}else if(isLeft){parent.left=null;}else{parent.right=null;}}


It can be seen that there are three further subdivisions. When a deleted node is a leaf node and a root node, it will have only one root node in the tree and be deleted directly.
The following two types are used to determine the edge of the parent node of the deleted node.

2. Only the left child node is deleted.
if(curr.right==null){if(curr==root){root=root.left;}else if(isLeft){parent.left=curr.left;}else{parent.right=curr.left;}}
When the deleted node has only one child, you only need to use its child node and replace it with itself. The specifics are the same as above. Three situations are required.
If it is a root node, direct the root node to the left child of the root node. As a result, the original root node cannot be accessed and will be automatically reclaimed by the virtual machine.
The same is true.


3. The deleted node only has the right child node. This is similar to the second case.
if(curr.left==null){if(curr==root){root=root.right;}else if(isLeft){parent.left=curr.right;}else{parent.right=curr.right;}}



4. The deleted two child nodes are the most complicated because the order after deletion cannot be messy.
Therefore, this type of node needs to be deleted. If it is directly deleted, the size order of the tree is messy. Therefore, you need to consider finding a proper node in the tree to delete the node.
Replace it with, and use this method to ensure the stability of the entire number. So another question comes. Which node should I find to replace it? The conclusion is that we need to find all the ratios in the tree.
The number of all deleted nodes with a large value, and the smallest number is found. It sounds tough. If you use graphs to describe it, it is starting from the deleted node.
After its right node, the leaf node on the far left of the right node is what we are looking for. It has a specialized term called the Middle-order successor node. Here is a specific method to find it:

Public Node getSuccessor (Node delNode) {// The parameter defines a reference of the current Node for the deleted Node. Let's take the next step directly, go to the right Node of the deleted Node. Node curr = delNode. right; Node successor = curr; // used to point to Node sucParent = null next to the intermediate Node; // used to point to the parent Node of the intermediate Node after the while (curr! = Null) {sucParent = successor; successor = curr; curr = curr. left;} // stops the loop. if (successor! = DelNode. right) {// replace the child node (only the right node may exist) of the successor node with sucParent. left = successor. right; // connect the deleted right child to successor on the right node of the successor node. right = delNode. right; // connect the deleted left child to the right node of the successor node (that is, replacing the deleted node with the successor node)} return successor ;}



Because the process is complicated, the diagram is used here.



Complete code for deleting a node: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;">/*** delete Node * @ param key */public boolean delete (int key) {Node curr = root; // Save the current Node parent = root; // Save the current Node parent Node boolean isLeft = true; // check whether the record is at the left: // locate the node to be deleted while (key! = Curr. key) {if (key <= curr. key) {isLeft = true; // pass through the left node if (curr. left! = Null) {parent = curr; curr = curr. left ;}} else {isLeft = false; // pass through the right node if (curr. right! = Null) {parent = curr; curr = curr. right ;}} if (curr = null) {return false ;}// if the deleted node is a leaf node if (curr. left = null & curr. right = null) {if (curr = root) {root = null;} else if (isLeft) {parent. left = null;} else {parent. right = null;} // if the deleted node has only the left node} else if (curr. right = null) {if (curr = root) {root = root. left;} else if (isLeft) {parent. left = curr. left;} else {parent. right = curr. left;} // if the deleted node only has the right node} else if (curr. left = null) {if (curr = root) {roo T = root. right;} else if (isLeft) {parent. left = curr. right;} else {parent. right = curr. right ;}} else {Node successor = getSuccessor (curr); // connect the successor Node to the deleted parent Node to complete the replacement process if (curr = root) {root = successor;} else if (curr = parent. left) {parent. left = successor;} else {parent. right = successor;} successor. left = curr. left;} return true;} public Node getSuccessor (Node delNode) {Node curr = delNode. right; Node successor = curr; Node sucParent = n Ull; while (curr! = Null) {sucParent = successor; successor = curr; curr = curr. left;} if (successor! = DelNode. right) {// replace the child node (only the right node may exist) of the successor node with sucParent. left = successor. right; // connect the deleted right child to successor on the right node of the successor node. right = delNode. right; // connect the deleted left child to the right node of the successor node (that is, replacing the deleted node with the successor node)} return successor ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.