/*************************************** **********************************
This is a binary search tree that implements the following operations: Insert a node, construct a binary tree, delete a node, search,
Find the maximum value, the minimum value, and the frontend and successor of the specified node. Time complexity of all the preceding operations
Are O (h), where H is the height of the tree
The comment is very detailed. For details, refer to the code.
**************************************** *********************************/
# Include <stdio. h>
# Include <stdlib. h>
// Binary Search Tree node description
Typedef int keytype;
Typedef struct Node
{
Keytype key; // keyword
Struct node * left; // left child pointer
Struct node * right; // right child pointer
Struct node * parent; // pointer to the parent node
} Node, * pnode;
// Insert a node into the Binary Search Tree
// If it is inserted, the address of the root node may be changed, so the second-level pointer is passed.
Void inseart (pnode * root, keytype key)
{
// Initialize the insert Node
Pnode P = (pnode) malloc (sizeof (node ));
P-> key = key;
P-> left = p-> right = p-> parent = NULL;
// Directly act as the root node when the tree is empty
If (* root) = NULL ){
* Root = P;
Return;
}
// Insert the left child to the current node (* root)
If (* root)-> left = NULL & (* root)-> key ){
P-> parent = (* root );
(* Root)-> left = P;
Return;
}
// Insert it to the right child of the current node (* root)
If (* root)-> right = NULL & (* root)-> key <key ){
P-> parent = (* root );
(* Root)-> right = P;
Return;
}
If (* root)-> key)
Inseart (& (* root)-> left, key );
Else if (* root)-> key <key)
Inseart (& (* root)-> right, key );
Else
Return;
}
// Search for elements. Find the node pointer of the keyword returned. If no node pointer is found, null is returned.
Pnode search (pnode root, keytype key)
{
If (root = NULL)
Return NULL;
If (Key> root-> key) // find the right subtree
Return search (root-> right, key );
Else if (Key <root-> key) // find the left subtree
Return search (root-> left, key );
Else
Return root;
}
// Query the minimum keyword. If the tree is empty, null is returned.
Pnode searchmin (pnode root)
{
If (root = NULL)
Return NULL;
If (root-> left = NULL)
Return root;
Else // keep searching for the left child until there is no left child node
Return searchmin (root-> left );
}
// Search for the maximum keyword. If the tree is empty, null is returned.
Pnode searchmax (pnode root)
{
If (root = NULL)
Return NULL;
If (root-> right = NULL)
Return root;
Else // keep searching for the right child until there is no right child node
Return searchmax (root-> right );
}
// Find the precursor of a node
Pnode searchpredecessor (pnode P)
{
// Empty tree
If (P = NULL)
Return P;
// The largest one in the left subtree and the largest one in the left subtree
If (p-> left)
Return searchmax (p-> left );
// No left subtree. Search for the right subtree of a node.
Else {
If (p-> parent = NULL)
Return NULL;
// Look up for the Precursor
While (p ){
If (p-> parent-> right = P)
Break;
P = p-> parent;
}
Return p-> parent;
}
}
// Find the successor of a node
Pnode searchsuccessor (pnode P)
{
// Empty tree
If (P = NULL)
Return P;
// The smallest of the right subtree and the right subtree
If (p-> right)
Return searchmin (p-> right );
// No right subtree. Search for the left subtree of a node.
Else {
If (p-> parent = NULL)
Return NULL;
// Search for the successor
While (p ){
If (p-> parent-> left = P)
Break;
P = p-> parent;
}
Return p-> parent;
}
}
// Delete a node based on the keyword. If the node is deleted successfully, 1 is returned. Otherwise, 0 is returned.
// If you delete the root node, you need to change the address of the root node, so pass the second-level pointer
Int deletenode (pnode * root, keytype key)
{
Pnode Q;
// Find the node to be deleted
Pnode P = search (* root, key );
Keytype temp; // Save the value of the successor Node
// This keyword is not found
If (! P)
Return 0;
// 1. The deleted node is a leaf node and is deleted directly.
If (p-> left = NULL & P-> right = NULL ){
// There is only one element. After deletion, it becomes an empty tree.
If (p-> parent = NULL ){
Free (P );
(* Root) = NULL;
} Else {
// The deleted node is the left child of the parent node
If (p-> parent-> left = P)
P-> parent-> left = NULL;
Else // The deleted node is the right child of the parent node
P-> parent-> right = NULL;
Free (P );
}
}
// 2. The deleted node has only the left subtree.
Else if (p-> left &&! (P-> right )){
P-> left-> parent = p-> parent;
// If the deletion is a parent node, change the parent node pointer.
If (p-> parent = NULL)
* Root = p-> left;
// The deleted node is the left child of the parent node
Else if (p-> parent-> left = P)
P-> parent-> left = p-> left;
Else // The deleted node is the right child of the parent node
P-> parent-> right = p-> left;
Free (P );
}
// 3. The deleted node has only the right child
Else if (p-> Right &&! (P-> left )){
P-> right-> parent = p-> parent;
// If the deletion is a parent node, change the parent node pointer.
If (p-> parent = NULL)
* Root = p-> right;
// The deleted node is the left child of the parent node
Else if (p-> parent-> left = P)
P-> parent-> left = p-> right;
Else // The deleted node is the right child of the parent node
P-> parent-> right = p-> right;
Free (P );
}
// 4. The deleted node has both left and right children.
// The successor node of the node must have no left subtree (refer to the above to find the successor node function)
// Delete the successor node. The value of the successor node replaces this node.
Else {
// Find the successor of the node to be deleted
Q = searchsuccessor (P );
Temp = Q-> key;
// Delete the successor Node
Deletenode (root, Q-> key );
P-> key = temp;
}
Return 1;
}
// Create a binary search tree
Void create (pnode * root, keytype * keyarray, int length)
{
Int I;
// Insert a binary tree to each node
For (I = 0; I <length; I ++)
Inseart (root, keyarray [I]);
}
Int main (void)
{
Int I;
Pnode root = NULL;
Keytype nodearray [11] = {15, 6, 18, 3, 7, 17, 20, 2, 4, 13, 9 };
Create (& root, nodearray, 11 );
For (I = 0; I <2; I ++)
Deletenode (& root, nodearray [I]);
Printf ("% d \ n", searchpredecessor (Root)-> key );
Printf ("% d \ n", searchsuccessor (Root)-> key );
Printf ("% d \ n", searchmin (Root)-> key );
Printf ("% d \ n", searchmax (Root)-> key );
Printf ("% d \ n", search (root, 13)-> key );
Return 0;
}
Binary Search Tree