# Operational Set of Binary Search Tree

```BinTree Insert( BinTree BST, ElementType X){
if( !BST){ //If empty, generate and return a binary search tree for a node
BST = (BinTree)malloc(sizeof(struct TNode));
BST->Left = BST->Right = NULL;
BST->Data = X;
}else{
if( X > BST->Data)
BST->Right = Insert( BST->Right, X);//Return the root node of the subtree to BST->Right
else if( X < BST->Data)
BST->Left = Insert( BST->Left, X);//Return root node of subtree to BST->Left
}
return BST;
}```
```Position Find( BinTree BST, ElementType X ){
while( BST){
if( X > BST->Data) BST = BST->Right;//Find in the right subtree
else if( X < BST->Data) BST = BST->Left;//Find in the left subtree
else break;//Find succeeded, jump out of the loop, return to the current node
}
return BST;
}```
```Position FindMin( BinTree BST){
while( BST){
if( BST->Left) BST = BST->Left;
else break;//Find left until there is no left subtree, which is the minimum
}
return BST;
}```
```Position FindMax( BinTree BST){
while( BST){
if( BST->Right) BST = BST->Right;
else break;
}
return BST;
}```
```BinTree Delete( BinTree BST, ElementType X ){
Position temp;
else{
if( X > BST->Data) BST->Right = Delete( BST->Right, X);//Recursive deletion to right subtree
else if( X < BST->Data) BST->Left = Delete( BST->Left, X);//Recursive deletion to left subtree
else{//BST is the node to be deleted
if( BST->Left && BST->Right){//The case where BST has two subnodes
temp = FindMin( BST->Right);//Find the smallest node in the right subtree
BST->Data = temp->Data;//Change the value of BST to temp, that is, delete
BST->Right = Delete( BST->Right, BST->Data);//Delete the smallest node in the right subtree
}else{//Deleted node has only one node or no node
temp = BST;
if( !BST->Left) BST = BST->Right;//Only right node or no child node
else BST = BST->Left;//Only left node
free(temp);
}
}
}
return BST;
}
```

If you want to delete a leaf node, you can delete it directly, then modify the pointer of its parent node and leave it empty.

If the node you want to delete has only one child node, you need to change the pointer of its parent node to point to the child node you want to delete before deleting.That is to change the value of BST.(The BST returned is the child of the parent node)

When the deleted node has two nodes, you can select the largest node in the left subtree or the smallest node in the right subtree to replace the node to be deleted, which ensures that the replaced node is larger than the left subtree and smaller than the right subtree.It is known that this node has at most one child node, otherwise it does not satisfy the maximum or minimum conditions.The selected node can be deleted by deleting a node with only one child node.

In the delete operation, the return value of the function is the root node, BST->Right = Delete (BST->Right, X), that is, the node of the subtree after the delete operation is assigned to BST->Right, which does not affect the value of BST.When the last operation of recursion completes a step-by-step return, the nodes of the left and right subtrees are updated and the root node of the tree is returned.

Posted on Tue, 07 Jul 2020 12:34:19 -0400 by jheitz