Implementation of binary search tree

Source: Internet
Author: User

Complexity O (LOGN)

1#include <queue>2#include <cstdio>3 using namespacestd;4 5 //represents the structure of a node6 structnode{7     intVal;8Node *lch,*RCH; 9 };Ten  One //Insert value x ANode *intsert (node *p,intx) - { -     if(p==NULL) { theNode *q=Newnode; -Q->val=x; -Q->lch=q->rch=NULL; -         returnQ; +     } -     Else{ +         if(X < p->val) AP->lch=intsert (p->lch,x); at         Else -P->rch=intsert (p->rch,x); -         returnp; -     } - } -  in //Find value x - BOOLFind (Node *p,intx) to { +     if(P==null)return false; -     Else if(x = = P->val)return true; the     Else if(x < P->val)returnFind (p->lch,x); *     Else returnFind (p->rch,x); $ }Panax Notoginseng  - //Delete value x theNode *remove (node *p,intx) { +     if(P==null)returnNULL; A     Else if(x < P->val) P->lch=remove (p->lch,x); the     Else if(x > P->val) p->rch=remove (p->rch,x); +     Else if(p->lch==NULL) { -Node *q=p->ch; $ delete p; $         returnQ; -     } -     Else if(p->lch->rch==NULL) { theNode *q=p->LCH; -Q->rch=p->rch;Wuyi delete p; the         returnQ; -     } Wu     Else{ -Node *Q; About          for(q=p->lch;q->rch->rch!=null;q=q->rch); $Node *r=q->rch; -Q->rch=r->LCH; -R->lch=p->LCH; -R->rch=p->rch; A delete p; +         returnR; the     } -     returnp; $}

Implementation of binary search 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.