1 establish binary search tree
typedef int ElemType; struct node { ElemType data; node* lchild; node* rchild; };
2 building trees
2.1 new node
//New node node* newNode(int v){ node* Node = new node; Node->data = v; Node->lchild = Node->rchild = NULL; return Node; }
2.2 insertion node
//Insert Knot void Insert(node* &root, int x){ if(root == NULL){//Empty tree search failed, i.e. insert location root = newNode(x);//New node, weight x return; } if(root->data == x){//Description node already exists return; }else if(x < root->data){ Insert(root->lchild, x); }else{ Insert(root->rchild, x); } }
2.3 establishing trees
//Establishment of binary search tree node* Create(int data[], int n){ node* root = NULL; for (int i = 0; i < n; ++i) { Insert(root, data[i]); } return root; } //For the same set of data, the order of insertion is different, and the binary search tree is also different
3 delete node
3.1 preparation before deletion
- Find the maximum weight node in the tree with root as the root node
//Find the maximum weight node in the tree with root as the root node node* findMax(node* root){ while(root->rchild != NULL){ root = root->rchild; } return root; }
- //Find the minimum weight node in the tree with root as the root node
//Find the minimum weight node in the tree with root as the root node node* findMin(node* root){ while(root->lchild != NULL){ root = root->lchild; } return root; }
3.2 delete node
void Delete(node* &root, int x){ if (root == NULL) { return; } if(root->data == x){//Find the node to delete if(root->lchild == NULL && root->rchild == NULL){//If it is a leaf node, delete it directly root = NULL; }else if(root->lchild != NULL){//Left subtree is not empty node* pre = findMax(root->lchild);//Find the root precursor node root->data = pre->data; //Overwrite root with precursor Delete(root->lchild, pre->data); //Delete node pre in left subtree //Delete the precursor method 2: replace the precursor pre'S child node (which must be the left child) with the precursor pre'S child node (which must be the right child node) to become the pre'S parent node S }else{ //Right subtree is not empty node* next = findMin(root->rchild);//Find the successor of root root->data = next->data; //Cover root with successor Delete(root->rchild, next->data); //Delete node next in right subtree //Delete the following method 2: replace next with the next child node (must be the right child) to become the next parent's left child node } }else if(root->data > x){ Delete(root->lchild, x);//Delete x to left subtree }else{ Delete(root->rchild, x);//Delete x to right subtree } }
4 test examples
#include <cstdio> #include <queue> using std::queue; typedef int ElemType; struct node { ElemType data; node* lchild; node* rchild; }; //lookup void Search(node* root, int x){ if(root == NULL){ //Empty tree, failed to find printf("searcjh fail!\n"); return; } if(root->data == x){//Found successfully, visit printf("%d\n", root->data); }else if(x < root->data){//If x is smaller than the root node, X is in the left subtree Search(root->lchild, x); }else{ // If x is larger than the root node, X is in the right subtree Search(root->rchild, x); } } //New node node* newNode(int v){ node* Node = new node; Node->data = v; Node->lchild = Node->rchild = NULL; return Node; } //Insert Knot void Insert(node* &root, int x){ if(root == NULL){//Empty tree search failed, i.e. insert location root = newNode(x);//New node, weight x return; } if(root->data == x){//Description node already exists return; }else if(x < root->data){ Insert(root->lchild, x); }else{ Insert(root->rchild, x); } } //Establishment of binary search tree node* Create(int data[], int n){ node* root = NULL; for (int i = 0; i < n; ++i) { Insert(root, data[i]); } return root; } //For the same set of data, the order of insertion is different, and the binary search tree is also different //Find the maximum weight node in the tree with root as the root node node* findMax(node* root){ while(root->rchild != NULL){ root = root->rchild; } return root; } //Find the minimum weight node in the tree with root as the root node node* findMin(node* root){ while(root->lchild != NULL){ root = root->lchild; } return root; } void Delete(node* &root, int x){ if (root == NULL) { return; } if(root->data == x){//Find the node to delete if(root->lchild == NULL && root->rchild == NULL){//If it is a leaf node, delete it directly root = NULL; }else if(root->lchild != NULL){//Left subtree is not empty node* pre = findMax(root->lchild);//Find the root precursor node root->data = pre->data; //Overwrite root with precursor Delete(root->lchild, pre->data); //Delete node pre in left subtree //Delete the precursor method 2: replace the precursor pre'S child node (which must be the left child) with the precursor pre'S child node (which must be the right child node) to become the pre'S parent node S }else{ //Right subtree is not empty node* next = findMin(root->rchild);//Find the successor of root root->data = next->data; //Cover root with successor Delete(root->rchild, next->data); //Delete node next in right subtree //Delete the following method 2: replace next with the next child node (must be the right child) to become the next parent's left child node } }else if(root->data > x){ Delete(root->lchild, x);//Delete x to left subtree }else{ Delete(root->rchild, x);//Delete x to right subtree } } const int MAXN = 110; int pre[MAXN] = {5,1,0,3,2,4,8,6,7,9}; int in[MAXN] = {0,1,2,3,4,5,6,7,8,9}; //Create a binary tree in front and middle order node* Create2(int preL, int preR, int inL, int inR){ if(preL > preR){ return NULL; } node* root = new node; root->data = pre[preL]; int k; for (k = inL; k <= inR; ++k) { if(in[k] == root->data){ break; } } int numLeft = k - inL; root->lchild = Create2(preL + 1, preL + numLeft, inL, k - 1); root->rchild = Create2(preL + numLeft + 1, preR, k + 1, inR); return root; } //Sequential traversal void layorder(node* root){ queue<node*> q; q.push(root); while(!q.empty()){ node* now = q.front(); q.pop(); printf("%d\n", now->data); if(now->lchild != NULL){ q.push(now->lchild); } if(now->rchild != NULL){ q.push(now->rchild); } } } int main(int argc, char const *argv[]) { node* root; root = Create2(0, 9, 0, 9); Delete(root, 5); layorder(root); return 0; }