Implementation of Binary search tree C + + code

Source: Internet
Author: User

No bugs found, please indicate if found.

#include <iostream>using namespacestd;//define the nodes of a binary search treestructnode{intdata; Node*lc,*rc,*parent;};//Middle sequence Traversal binary search treevoidShow (Node *Now ) {    if(Now==null)return; Show ( now-LC); cout<<now->data<<Endl; Show ( now-RC);}//Insert a node with a value of Val into a two-fork search tree//Why do you want to pass a pointer to the root node pointer instead of a pointer to the root node directly? Because the pointer to the root node may be empty, you need to open up memory here. node* Insert (Node **rt,intval) {Node*now=*rt;//move on the tree with the now pointerNode *par=null;//father who records the now pointer     while(now!=NULL) {par=Now ; if(Val<now->data)//if Val is less than the current node, the value description should be inserted into record.Now=now->LC; Else Now=now->rc;//Otherwise, insert it into the right sub-tree} Node*newnode=NewNode;//opening up new nodes.Newnode->data=Val; NewNode->lc=newnode->rc=NULL; if(Par==null)//when this is an empty tree{NewNode->parent=NULL; *rt=newnode;//modifying root nodes directly with pointers    }    Else    {        if(val<par->data) par->lc=NewNode; Elsepar->rc=NewNode; NewNode->parent=par; }    returnNewNode;}//on a tree with now root nodes, look for a node with a Val valuenode* Search (Node *now,intval) {     while(now!=null&&now->data!=val) {        if(val<now->data) now=now->LC; Else Now=now->RC; }    returnNow ;}//in the tree with now root node, look for the maximum and minimum valuesnode* Maximun (Node *Now ) {     while(now->rc!=NULL) { Now=now->RC; }    returnNow ;} Node* MINIMUN (Node *Now ) {     while(now->lc!=NULL) now=now->LC; returnNow ;}//looking for the former and successor of the now node.//seek forward: If Now's left subtree is non-empty, returns the maximum value node of the left subtree. Otherwise, if now is the father's left child, then continue upward until now is no longer the father's left child, returning to the parent node. node* predecessor (Node *Now ) {    if(now->lc!=NULL)returnMaximun (now->LC); Node*par=now->parent;  while(par!=null&&now==par->LC) { now=par; Par=par->parent; }    returnpar;} Node* Successor (Node *Now ) {    if(now->rc!=NULL)returnMinimun (now->RC); Node*par=now->parent;  while(par!=null&&now==par->RC) { Now=par; Par=par->parent; }    returnpar;}//for a tree with T as its root node, the subtree used for the root node of V replaces the subtree with the root node of U, and completes the binding between the Father and V of U.voidTransplant (Node **t,node *u,node *4) {    if(u->parent==NULL) (*T) =v; Else if(u==u->parent->LC) U->parent->lc=v; Else if(u==u->parent->RC) U->parent->rc=W; if(v!=NULL) v->parent=u->parent;}//Delete a given node.voidErase (Node **t,node *Now ) {Node*temp=Now ; if(now->lc==NULL) {Transplant (T,now,now-RC); }    Else if(now->rc==NULL) {Transplant (T,now,now-LC); }    Else{Node* nextnode=successor (now); if(nextnode!=now->RC) {Transplant (T,nextnode,nextnode-RC); NextNode->rc=now->RC; now->rc->parent=NextNode;        } transplant (T,now,nextnode); NextNode->lc=now->LC; now->lc->parent=NextNode; } Delete temp;intMain () {Node*root=NULL;  while(1) {cout<<"1. Inserting nodes"<<Endl; cout<<"2. Delete Nodes"<<Endl; cout<<"3. Middle Sequence Traversal"<<Endl; cout<<"4. Enquiry"<<Endl; cout<<"5. Query root"<<Endl; intp; CIN>>p; if(p==1) {cout<<"Enter the value of the element you want to interpolate:"<<Endl; intT; CIN>>T; Insert (&root,t); cout<<"Insert Success! "<<Endl; }        Else if(p==2) {cout<<"Enter the value of the element you want to delete:"<<Endl; intT; CIN>>T; Node*q=search (root,t); if(q==null) cout<<"The value does not exist! "<<Endl; Else{Erase (&root,q); cout<<"Delete Success! "<<Endl; }        }        Else if(p==3) {cout<<"======"<<Endl;            Show (root); cout<<"======"<<Endl; }        Else if(p==4) {cout<<"Enter the value of the element you want to query:"<<Endl; intT; CIN>>T; Node*q=search (root,t); if(q==null) cout<<"The value does not exist! "<<Endl; Else{Node*a=predecessor (q), *b=successor (q); if(a!=NULL) cout<<"Pre-succession:"<<a->data<<Endl; if(b!=NULL) cout<<"successor:"<<b->data<<Endl; }        }        Else if(p==5)        {            if(root==null) cout<<"the root is empty! "<<Endl; Elsecout<<"Root:"<<root->data<<Endl; }    }    return 0;}

Related Article

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.