test

#include<bits/stdc++.h>
using namespace std;
class BSTNode{
    public:
        double x,y;
        string name;
        BSTNode* lc;
        BSTNode* rc;
        BSTNode* par;
    public:
        BSTNode(){
            lc = NULL;
            rc = NULL;
            par = NULL;
        }
        BSTNode(string _s ,double _x,double _y,BSTNode* fa){
            lc = NULL;
            rc = NULL;
            par = fa;
            name = _s;
            x = _x;
            y = _y;
        }
        void SetLeft(string _s,double _x,double _y){
            BSTNode* tem = new BSTNode(_s, _x,_y,this);
            lc = tem;
        }
        void SetRight(string _s,double _x,double _y){
            BSTNode* tem = new BSTNode( _s, _x, _y,this);
            rc = tem;
        }
        void insert(string _s,double _x,double _y){
            if(_s < name){
                if(lc == NULL) SetLeft( _s, _x, _y);
                else lc->insert( _s, _x, _y);
            }
            else{
                if(rc == NULL) SetRight( _s, _x, _y);
                else rc->insert(_s, _x, _y);
            }
        }
        BSTNode* getmin(){
            if(lc == NULL) return this;
            else return lc->getmin();
        }
        void dele(){
            if(rc == NULL){
                if(lc == NULL){
                    if(this == par->lc) par->lc = NULL;
                    else par->rc = NULL;
                    delete this;
                    return;        
                }
                else{
                    BSTNode* tem = lc;
                    tem->par = par;
                    if(this == par->lc) par->lc = tem;
                    else par->rc = tem;
                    delete this;
                    return; 
                }
            }
            else{
                BSTNode* tem = rc->getmin();
                name = tem->name;
                x = tem->x;
                y = tem->y;
                tem->dele();
            }
        }
        bool findname(string fname,BSTNode*& ans ){
//            cerr<<fname<<"GG"<<name<<endl;
            if(fname == name){
                ans = this;
//                cerr<<"OK"<<ans->x<<' '<<ans->y<<endl;
                return 1;
            }
            bool ok = 0;
            if(lc != NULL && fname < name) ok =  lc->findname(fname,ans);
            if(ok) return 1;
            if(rc != NULL ) ok = rc->findname(fname,ans);
            return ok;
        }
        bool findxy(double fx,double fy,BSTNode*& ans){
            if(x == fx && y == fy){
                ans = this;
                return 1;
            }
            else{
                if(lc != NULL && lc->findxy(fx,fy,ans) ) return 1;
                if(rc != NULL && rc->findxy(fx,fy,ans) ) return 1;
             }
             return 0;
        }
        void delename(string delname){
            BSTNode* ans;
            findname(delname,ans);
            ans->dele();
        }
        bool delexy(double dx,double dy){
            if(x == dx && y == dy){
                dele();
                return 1;
            }
            else{
                if(lc != NULL && lc->delexy(dx,dy) ) return 1;
                if(rc != NULL && rc->delexy(dx,dy) ) return 1;
             }
             return 0;
        }
        void print(double cx,double cy,double r){
            if( (cx-x)*(cx - x) + (cy-y)*(cy - y) < r*r){
                cout<<name<<' '<<x<<' '<<y<<endl;
            }
            if(lc != NULL) lc->print(cx,cy,r);
            if(rc != NULL) rc->print(cx,cy,r);
        }
        void show(){
            cout<<name<<' '<<x<<' '<<y<<endl;
            return;
        }
};
int main(){
    BSTNode* root;
    bool has = 0;
    while(1){
         int op;
            printf("\ninsert enter 1\ndelete enter 2\nfind enter 3\nprint enter 4\n");
            scanf("%d",&op);
            switch(op){
                case 1:{
                    cout<<"name and xy"<<endl;
                    string s; double xx,yy;
                    cin>>s>>xx>>yy;
                    if(!has){
                        has = 1;
                        root = new BSTNode(s,xx,yy,NULL);
                    }
                    else{
                        root->insert(s,xx,yy);
                    }
                    break;
                }
                case 2:{
                    int ty;
                    printf("key is name enter 1\nkey is ordinate enter 2\n");
                    scanf("%d",&ty);
                    if(ty == 1){
                        string delete_name;
                        printf("enter delete city's name\n");
                        cin>>delete_name;
                        root->delename(delete_name);
                    }
                    else{
                        int tx,ty;
                        printf("enter delete city's x and y\n");
                        cin>>tx>>ty;
                        root->delexy(tx,ty);
                      }        
                    break;
                }
                case 3:{
                    int ty;
                    printf("key is name enter 1\nkey is ordinate enter 2\n");
                    scanf("%d",&ty);
                    if(ty == 1){
                        string find_name;
                        printf("enter find city's name\n");
                        cin>>find_name;
                        BSTNode* ans;
                        if(!(root->findname(find_name,ans))) cout<<"NOT FOUND"<<endl;
                        else{
                            ans->show();
                        }
                    }
                    else{
                        int tx,ty;
                        printf("enter find city's x and y\n");
                        cin>>tx>>ty;
                        BSTNode* ans;
                        if(!root->findxy(tx,ty,ans)) cout<<"NOT FOUND"<<endl;
                        else{
                            ans->show();
                        }
                    }
                    break;
                }
                case 4:{
                    cout<<"cx and xy and r"<<endl;
                    double cx,cy,r;
                    cin>>cx>>cy>>r;
                    root->print(cx,cy,r);
                    break;
                }
            }
    }
    return 0;
}
View Code

 

posted @ 2019-10-26 11:13  小布鞋  阅读(160)  评论(0编辑  收藏  举报