1, The concept of binary search tree
Binary search tree is also called binary sort tree. It is either an empty tree or a binary tree with the following properties:
1. If its left subtree is not empty, the values of all nodes on the left subtree are less than the values of the root node.
2. If its right subtree is not empty, the values of all nodes on the right subtree are greater than those of the root node.
3. Its left and right subtrees are also binary search trees.
Below is a binary search tree
2, Algorithm implementation of binary search tree
1. Create binary search tree
The way to create a binary search tree is to insert each node into the binary search tree in turn. The main idea is to find the appropriate leaf node location for the target node, and then insert the node as a leaf node.
Insert 4 into the tree on the left, starting from the root node. 4 is less than 5, so continue to search the left subtree. 4 is greater than 2, and 2 has no right child, so 4 is inserted into the tree as the right child of 2.
2. Delete a node of binary search tree
The deletion operation here is to replace the target node to be deleted with an appropriate child node to minimize the change of the overall operation. If the value of the target node is greater than the current node, point to the right subtree to find the target node; If the target node value is less than the current node, point to the left subtree to find the target node. If the target node is found, there are three cases:
1: The target node has no child nodes. You can delete the target node directly. As shown in the figure below:
2: The target node has a right node, which is replaced by the leftmost node of the right subtree (that is, the smallest node in the right subtree). The node is located in the lower position in the right subtree. Then you can recursively operate down from the position of the successor node to delete the successor node. As shown in the figure below:
3: The target node has only the left node. You can use the rightmost node of its left subtree to replace it (that is, the largest node in the left subtree), and then recursively delete the node downward. As shown in the figure below:
#include<iostream> #include<queue> using namespace::std; typedef struct Node { int data; Node* lchild, * rchild; }Btree; //Insert a node Btree* InsertNode(Btree* T, int k) { if (T == nullptr) { T = new Btree; T->data = k; T->lchild = NULL; T->rchild = NULL; return T; } if (k > T->data) { T->rchild=InsertNode(T->rchild, k); } else { T->lchild=InsertNode(T->lchild, k); } return T; } Btree* CreatTree(Btree* T, int a[],int len) { for (int i = 0; i < len; i++) { T=InsertNode(T, a[i]); //Insert data into binary search tree } return T; } //Preorder traversal void PreOrder(Btree* t) { if (t) { cout << t->data<<" "; //Output node data PreOrder(t->lchild);//Visit left child PreOrder(t->rchild);//Visit right child } } Btree* DeleteNode(Btree* T, int key) { Btree* Node = T; Btree* parent=new Btree; int flag=-1; //Find the node to delete while (Node) { if (Node->data == key) { break; } else if (key > Node->data) //If the value to be found is greater than the value of the node, traverse the subtree to the right { parent = Node; //Save parent node flag = 1; //Indicates that the deleted node is the right child of the parent node Node = Node->rchild; } else { parent = Node; //Save parent node flag = 0; //Indicates that the deleted node is the left child of the parent node Node = Node->lchild; //If the value to be found is less than the value of the node, it traverses the left subtree } } //The node to be deleted has no child nodes. Delete it directly if (Node->lchild == NULL && Node->rchild == NULL) { if (flag == -1) //If there is only one node, delete it directly and return a null pointer { return NULL; } else if (flag == 0)//Delete left child { parent->lchild = NULL; } else if (flag == 1)//Delete right child { parent->rchild = NULL; } } //The node to be deleted has a right child else if (Node->rchild != NULL) { Btree* MostLchild = Node->rchild; int temp=MostLchild->data; //Find the leftmost node in the right child, that is, the smallest node in the right child while (MostLchild->lchild) { MostLchild = MostLchild->lchild; temp = MostLchild->data; //Save the value of this node } //Delete first and then exchange. If you exchange first, there will be two identical nodes in the binary tree DeleteNode(T, MostLchild->data); //Delete the smallest node of the right child. This step is equivalent to exchanging the node to be deleted with the smallest node of the right child, and then delete the smallest node of the right child Node->data = temp; //Overwrite the value of the smallest node in the right subtree with the value of the node to be deleted } //The node to be deleted has no right child, only left child else { Btree* MostRchild = Node->lchild; int temp; //Find the rightmost node in the left child, that is, the largest node in the left child while (MostRchild->rchild) { MostRchild = MostRchild->rchild; temp = MostRchild->data; //Save the value of this node } DeleteNode(T, MostRchild->data); Node->data = temp; //Overwrite the value of the node to be deleted with the value of the largest node in the left subtree } return T; } int main() { int a[10] = { 5,3,7,1,4,6,8,0,2,9 }; Btree* T=NULL; Btree* BT=CreatTree(T, a,10); //Create binary search tree cout << "Preorder traversal:"; PreOrder(BT); //Preorder traversal binary tree cout << endl; BT=DeleteNode(BT,8); //Delete node with value of 8 cout << "After deletion:"; PreOrder(BT); //Preorder traversal binary tree }
Operation results: