## Binary search tree node deletion

Before this section introduces the deletion of binary search tree nodes, it first introduces how to find the minimum and maximum values, and delete the minimum and maximum values.

Take the minimum value as an example (the maximum value is the same):

Find the minimum key value code logic, and recursively search for the left child node:

...
// Return the node where the minimum key value of the binary search tree rooted at node is located
private Node minimum(Node node){     if( node.left == null )         return node;     return minimum(node.left); } ...

Delete the minimum key value of the binary search tree. If the node does not have a right subtree, delete it directly. If there is a right subtree, as shown in the figure:

Delete node 22, there is a right child, only need to replace node 22 with node 33 of the right subtree.

This removal minimum is expressed in code:

...
// Delete the smallest node in the binary search tree rooted at node
// Return the root of the new binary search tree after deleting the node
private Node removeMin(Node node){     if( node.left == null ){         Node rightNode = node.right;         node.right = null;         count --;         return rightNode;     }     node.left = removeMin(node.left);     return node; } ...

Now discuss the following three cases of binary search tree node deletion:

1. Delete the node with only left child, as shown in the figure below node 58.

Delete element 58, let the left subtree directly replace the position of 58, and the properties of the whole binary search tree remain unchanged.

2. Delete the node with only the right child, such as node 58 in the figure below.

Delete element 58, let the right subtree directly replace the position of 58, and the properties of the whole binary search tree remain unchanged.

3. Delete the nodes that have children on the left and right, as shown in the figure below node 58.

(1) Find the minimum value in the right subtree, which is node 59

(2) Node 59 replaces node 58 to be deleted

Based on the above rules, delete the node whose key value is key in the binary search tree rooted at node. The core code example:

## src/runoob/binary/BSTRemove.java file code:

package runoob.binary;

/**
* Binary search tree node deletion
*/
public class BSTRemove<Key extends Comparable<Key>, Value> {     // Nodes in the tree are private classes, not outside Need to understand the specific implementation of binary search tree nodes     private class Node {         private Key key;         private Value value;         private Node left, right;         public Node(Key key, Value value) {             this.key = key;             this.value = value;             left = right = null;         }         public Node(Node node){             this.key = node.key;             this.value = node.value;             this.left = node.left;

this.right = node.right;
}
}

private Node root; // the root
node private int count; // the number of nodes of the tree type

// constructor, an empty binary search tree is constructed by default
public BSTRemove() {         root = null ;         count = 0;     }     // Return the number of nodes in the binary search tree     public int size() {         return count;     }     // Return whether the binary search tree is empty     public boolean isEmpty() {         return count == 0;     }     // Insert a new (key, value) data pair into the binary search tree     public void insert(Key key, Value value){         root = insert(root, key, value);     }     // Check whether the key key exists in the binary search tree

public boolean contain(Key key){         return contain(root, key);     }     // Search the value corresponding to the key key in the binary search tree. If this value does not exist, return null     public Value search(Key key){         return search( root , key );     }     // preorder traversal of binary search tree     public void preOrder(){         preOrder(root);     }     // binary Inorder traversal of search tree     public void inOrder(){         inOrder(root);     }     // Postorder traversal of binary search tree     public void postOrder(){         postOrder(root);     }     // Level order traversal of binary search tree     public void levelOrder(){         // We use LinkedList as our queue

while( !q.isEmpty()){             Node node = q.remove();             System.out.println(node.key) ;             if( node.left != null )                 q.add( node.left );             if( node.right != null )                 q.add( node.right );         }     }     // Find the smallest key value of binary search tree     public Key minimum(){         assert count != 0;         Node minNode = minimum( root );         return minNode.key;     }     // Find the maximum key value of the binary search tree     public Key maximum(){         assert count != 0;

Node maxNode = maximum(root);
return maxNode.key;
}

// Delete the node where the minimum value is located from the binary search tree
public void removeMin(){         if( root != null )             root = removeMin( root );     }     // from Delete the node where the maximum value is located in the binary search tree     public void removeMax(){         if( root != null )             root = removeMax( root );     }     // Delete the node whose key value is key from the binary search tree     public void remove(Key key ){         root = remove(root, key);     }     //********************     //* Auxiliary function of binary search tree     //***** ***************     // Insert node (key, value) into the binary search tree rooted at node, using recursive algorithm

// Return the root of the binary search tree after inserting a new node
private Node insert(Node node, Key key, Value value){         if( node == null ){             count ++;             return new Node(key, value);         }         if ( key.compareTo(node.key) == 0 )             node.value = value;         else if( key.compareTo(node.key) < 0 )             node.left = insert( node.left , key, value);         else / / key > node->key             node.right = insert( node.right, key, value);         return node;     }     // Check whether the binary search tree rooted at node contains a node whose key is key, using a recursive algorithm     private boolean contain(Node node, Key key){         if( node == null )

return false;

if( key.compareTo(node.key) == 0 )
return true;
else if( key.compareTo(node.key) < 0 )
return contain( node.left , key );
else // key > node ->key
return contain( node.right , key );
}

// Find the value corresponding to the key in the binary search tree rooted at node, recursive algorithm
// If the value does not exist, return NULL
private Value search(Node node, Key key){         if( node == null )             return null;         if( key.compareTo(node.key) == 0 )             return node.value;         else if( key.compareTo(node.key) < 0 )             return search( node. left , key );

else // key > node->key
return search( node.right, key );
}

// Preorder traversal of the binary search tree rooted at node, recursive algorithm
private void preOrder(Node node){         if( node != null ){             System.out.println(node.key);             preOrder(node.left);             preOrder(node.right);         }     }     // Inorder traversal of the binary search tree rooted at node, recursive Algorithm     private void inOrder(Node node){         if( node != null ){             inOrder(node.left);             System.out.println(node.key);             inOrder(node.right);         }     }     // for node Post-order traversal of rooted binary search tree, recursive algorithm

private void postOrder(Node node){         if( node != null ){             postOrder(node.left);             postOrder(node.right);             System.out.println(node.key);         }     }     // return with node as the root private Node minimum     (Node node){         if( node.left == null )             return node;         return minimum(node.left);     }     // return binary search rooted at node The node where the maximum key value of the tree is located     private Node maximum(Node node){         if( node.right == null )             return node;         return maximum(node.right);     }     // Delete the binary search tree rooted at node The smallest node of

// Return the root of the new binary search tree after deleting the node
private Node removeMin(Node node){         if( node.left == null ){             Node rightNode = node.right;             node.right = null;             count --;             return rightNode ;         }         node.left = removeMin(node.left);         return node;     }     // delete the largest node in the binary search tree rooted at node     // return the root of the new binary search tree after deleting the node     private Node removeMax( Node node){         if( node.right == null ){             Node leftNode = node.left;             node.left = null;             count --;             return leftNode;         }

node.right = removeMax(node.right);
return node;
}

// delete the node whose key value is key in the binary search tree rooted at node, recursive algorithm
// returns the root of the new binary search tree after deleting the node
Node remove(Node node, Key key){         if( node == null )             return null;         if( key.compareTo(node.key) < 0 ){             node.left = remove( node.left , key );             return node;         }         else if( key.compareTo(node.key) > 0 ){             node.right = remove( node.right, key );             return node;         }         else{ // key == node->key             // left node to be deleted When the subtree is empty             if( node.left == null ){

Node rightNode = node.right;
node.right = null;
count --;
return rightNode;
}

// The right subtree of the node to be deleted is empty
if( node.right == null ){                 Node leftNode = node.left;                 node.left = null;                 count--;                 return leftNode;             }             // The left and right subtrees of the node to be deleted are not empty             // Find the smallest node that is larger than the node to be deleted, that is, the smallest node of the right subtree of the node to be deleted             // Use this node to replace the position of the node to be deleted             Node successor = new Node(minimum(node.right));             count ++;             successor.right = removeMin(node.right);

successor.left = node.left;

node.left = node.right = null;
count --;

return successor;
}
}
}

### Guess you like

Origin blog.csdn.net/unbelievevc/article/details/131370130
Recommended
Ranking
Daily