Binary search tree (sort binary tree)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.