Full code: INSERT, Find, delete
struct BST {int val; BST *lch, *rch; BST *insert (BST *p, int x) {if (p = = NULL) {BST *t = new bst;//new out is not a pointer to null T->val = X T->lch = T->rch = NULL; return t; } if (x <= p->val) P->lch = insert (P->lch, x); else P->rch = insert (P->rch, x); return p; } bool Find (BST *p, int x) {if (x = = P->val) return true; else if (p = = NULL) return false; else if (x <= p->val) {return find (P->lch, x); } else {return find (P->rch, x); }} BST *remove (BST *p, int x) {//Returns the address of the new node after being deleted if (p = = null) return null; else if (x <= p->val) P->lch = Remove (P->lch, x); else if (x > P->val) p->rch = Remove (p->rch, x); else if (P->lch = = NULL) {//If the node that needs to be deleted has no left son, bring it to the right son. BST *t = p->rch; Delete P return t; } else if (P->lch->rch = = NULL) {//If the left son of the node that needs to be deleted does not have a right son, then bring up the left son BST *t = p->lch; T->rch = p->rch; Delete p; return t; } else {//The above two conditions are not satisfied, the left son descendants of the most important point of the node to be put up BST *t = p->lch; while (t->rch->rch! = NULL) T = t->rch; BST *r = t->rch; T->rch = r->lch; R->lch = p->lch; R->rch = p->rch; Delete p; return R; } return p; }}bst;
Binary search tree (sort binary tree)