Binary sort tree, also known as binary search tree, has the following features:
(1) If the left subtree is not empty, the value of all nodes on the left subtree is smaller than the value of its root node;
(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
(3) Left and Right decision trees are also Binary Decision Trees;
However, you may find it strange to see the definition. If there is an equivalent element, we assume that the node value of the right subtree is greater than or equal to the root node.
Here we implement the following simple functions of a binary tree: (the Binary Tree node class is btreenode <t>)
Void insert (T Val); // insert data into a binary tree. Ensure that the inserted data is still a binary tree.
Btreenode <t> * search (T Val); // find the node with the value of Val
Btreenode <t> * searchminval (); // find the node with the smallest value
Btreenode <t> * searchmaxval (); // find the node with the largest value
Void Delete (T Val); // delete any node
1. First define a binary tree node class
Template <class T>
Class btreenode {
Public:
T val;
Btreenode <t> * left; // pointer to the left child
Btreenode <t> * right;
Btreenode <t> * parent; // pointer to the parent node
Btreenode (T tval ){
Val = tval;
Left = right = parent = NULL;
}
Bool operator = (btreenode <t> * BT) {// The overload operator compares the two nodes for Equality
Return Bt. Val = Val? True: false;
}
};
2. Define the binary search tree class bsorttree
# Include "btreenode. H"
Template <class T>
Class bsorttree {
PRIVATE:
Btreenode <t> * root; // root node pointer
Int size; // The total number of elements in the tree
Public:
Bsorttree (void ){
Root = NULL;
Size = 0;
}
~ Bsorttree (void ){}
Int size () {return size ;}
Void insert (T Val) {// insert node Element
If (root = NULL) {// The tree is empty
Btreenode <t> * pnode = new btreenode <t> (VAL );
Root = pnode;
Size ++;
Return;
}
Inserttree (root, Val); // insert an element when the tree is not empty
}
Btreenode <t> * search (T Val) {// search nodes
Return searchtree (root, Val );
}
Btreenode <t> * searchminval () {// find the minimum element
Searchtreeminval (Root );
}
Btreenode <t> * searchmaxval () {// find the maximum element
Searchtreemaxval (Root );
}
Void Delete (T Val) {// delete any node
Deletetreenode (root, Val );
}
PRIVATE:
// Delete a node in four cases
// 1. If it is a leaf, delete it directly.
// 2. If only the left subtree exists, delete the node and move it to the left subtree.
// 3. If only the right subtree is available, delete the node and move it to the right subtree.
// 4. if both left and right subtree exist, find the maximum node (m) in the left subtree of the node (p) to be deleted, assign the node value of M to P, and then delete M.
Void deletetreenode (btreenode <t> * proot, t Val) {// delete a node
Btreenode <t> * node = search (VAL); // The node to be deleted
If (node = NULL) // The node to be deleted does not exist
Return;
// 1. The deleted node is a leaf and can be deleted directly.
If (node-> left = NULL & node-> right = NULL ){
If (node-> parent = NULL) {// tree with only one node
Size = 0;
Root = NULL;
}
Else if (node-> parent-> left = node) // The deleted node is the left child
Node-> parent-> left = NULL;
Else // The node is the right child
Node-> parent-> right = NULL;
Delete node;
Size --;
Return;
}
// 2. The deleted node has only the left subtree.
If (node-> left! = NULL & node-> right = NULL ){
Node-> left-> parent = node-> parent;
If (node-> parent = NULL) // The deleted node is the root node.
Root = node-> left;
Else if (node-> parent-> left = node) // The deleted node is the left child
Node-> parent-> left = node-> left;
Else // The deleted node is the right child
Node-> parent-> right = node-> right;
Delete node;
Size --;
Return;
}
// 3. The deleted node only has the right subtree.
If (node-> right! = NULL & node-> left = NULL ){
Node-> right-> parent = node-> parent;
If (node-> parent = NULL) // The deleted node is the root node.
Root = node-> right;
Else if (node-> parent-> left = node) // The deleted node is the left child
Node-> parent-> left = node-> left;
Else // The deleted node is the right child
Node-> parent-> right = node-> right;
Delete node;
Size --;
Return;
}
// 4. There are both left and right nodes
If (node-> left! = NULL & node-> right! = NULL ){
Btreenode <t> * replacenode = searchtreeminval (node-> right); // You can also replace it with the maximum value of the Left subtree.
Node-> val = replacenode-> val;
Deletetreenode (root, replacenode-> Val );
}
}
Void inserttree (btreenode <t> * proot, t Val)
{
Btreenode <t> * pnode = new btreenode <t> (VAL );
If (proot-> left = NULL & Val <proot-> Val ){
Proot-> left = pnode;
Pnode-> parent = proot;
Size ++;
Return;
}
If (proot-> right = NULL & Val> = proot-> Val ){
Proot-> right = pnode;
Pnode-> parent = proot;
Size ++;
Return;
}
If (Val <proot-> Val)
Inserttree (proot-> left, Val );
Else
Inserttree (proot-> right, Val );
}
Btreenode <t> * searchtree (btreenode <t> * proot, t Val ){
If (proot = NULL)
Return NULL;
Else if (val = proot-> Val)
Return proot;
Else if (Val <proot-> Val)
Return searchtree (proot-> left, Val );
Else
Return searchtree (proot-> right, Val );
}
Btreenode <t> * searchtreeminval (btreenode <t> * proot ){
If (proot = NULL)
Return NULL;
Else if (proot-> left = NULL)
Return proot;
Else
Return searchtreeminval (proot-> left );
}
Btreenode <t> * searchtreemaxval (btreenode <t> * proot ){
If (proot = NULL)
Return NULL;
Else if (proot-> right = NULL)
Return proot;
Else
Return searchtreeminval (proot-> right );
}
};