Binary Search Tree is a tree-based search algorithm.
Its generation method is as follows:
The value of the Left subtree of each tree is always smaller than the value of the parent node, and the value of the right subtree is always greater than the value of the parent node...
The following code inserts an algorithm:
Struct Node
{
Int data;
Struct node leftchild;
Struct node rightchild;
};
Struct node * root = NULL; // Root Node
Void insert (INT element)
{
Struct node * prenode;
Struc node * currentnode = root;
While (currentnode! = NULL)
{
Prenode = currentnode;
If (element> currentnode-> data)
{
Currentnode = currentnode-> rightchild;
}
Else
{
Currentnode = currentnode-> leftchild;
}
Struct node * pnew = new struct node;
Pnew-> DATA = element;
Pnew-> leftchild = pnew-> rightchild = NULL; // This step initializes the NULL pointer operation, which is equivalent to '/0' in the string operation'
If (root = NULL)
{
Root = pnew;
}
Else
{
If (element> prenode-> data)
{
Prenode-> rightchild = pnew;
}
Else
{
Prenode-> leftchild = pnew;
}
}
}
}
// The idea of this algorithm is to find the Insertion Location through continuous loops, find the correct location, and determine whether the newly added node is a left or right subnode, then generate a new tree
Although the tree structure is three-dimensional in form, it is amazing that we use a processing method similar to a linear structure when accessing the tree due to the idea of generating the tree.
Void find_node (INT element)
{
Struct node * PTR = root;
While (PTR! = NULL & PTR-> data! = Element)
{
If (element> PTR-> info)
{
PTR = PTR-> rightchild;
}
Else
{
PTR = PTR-> leftchild;
}
}
If (PTR = NULL)
{
Return false;
}
Else
{
Return true;
}
}
// For a data structure, four operations are required: add, delete, modify, and query. now we have implemented, add and query functions (change the search function to the repleace function), and delete the function poorly.
// For a search Binary Tree: deleting a node is not that easy;
When deleting a node, there are three situations;
1. the node to be deleted is the leaf node leaf.
2. the node to be deleted also has a left or right subtree.
3. the node to be deleted has both left and right Subtrees.
In the first case, simply delete it.
In the second case, delete the node and replace the non-empty subtree of the node to replace the deleted location.
In the third case, there are two options:
1. Find the maximum value in the left subtree of the node to be deleted to replace the node to be deleted. In this way, a binary tree will still be generated.
2. Find the minimum value from the right subtree of the node to be deleted to replace the node to be deleted.
I choose from the first case;
Bool delete_node (INT element)
{
// First find the node
Struct * currentnode = root;
Struct * prenode = NULL;
While (currentnode! = NULL & currentnode-> data! = Element)
{
Prenode = currentnode;
If (element> currentnode-> data)
{
Currentnode = currentnode-> rightchild;
}
Else
{
Currentnode = currentnode-> leftchild;
}
}
If (current = NULL)
{
Return false;
}
If (currentnode-> left! = NULL & currentnode-> right! = NULL) // If neither left nor right subtree is empty,
{
Struct node * subcurrentnode = currentnode-> leftchild;
Struct node * subptrnode = NULL;
While (subcurrentnode-> rightchild! = NULL)
{
Subcurrentnode = subcurrentnode-> rightchild;
}
Currentnode-> DATA = subcurrent-> data;
Currentnode = subcurrentnode;
Prenode = subptrnode;
}
Struct node * temp;
Temp = (currentnode-> left = NULL )? Currentnode-> rightchild: Current-> leftchild;
If (currentnode = root)
{
Root = C;
}
Else
{
If (currentnode = prenode-> left)
{
Prenode-> left = C;
}
Else
{
Prenode-> right = C;
}
}
}