Binary Search Tree (BST)

BST is a effective data structure for inserting,removing,searching. All of them cost log(n)

#include<bits/stdc++.h>
using namespace std;

typedef struct node{
    int value;
    node* lc;
    node* rc;
}Node,*Pnode;

Pnode rt = NULL;


void insert(Pnode p,int value){
    if(NULL==p){
        Pnode tmp = new Node;
        tmp->value = value;
        tmp->rc=tmp->lc = NULL;
        rt = tmp;
        return ;
    }
    else{
        if(p->value>value){
            if(NULL!=p->lc){
                insert(p->lc,value);
            }else{
                Pnode tmp = new Node;
                tmp->value = value;
                tmp->lc = NULL;
                tmp->rc = NULL;
                p->lc = tmp;
                return ;
            }
        }else if(p->value<value){
            if(NULL!=p->rc){
                insert(p->rc,value);
            }else{
                Pnode tmp = new Node;
                tmp->value = value;
                tmp->lc = NULL;
                tmp->rc = NULL;
                p->rc = tmp;
                return ;
            }
        }
    }
    return ;
}

Pnode search(Pnode p,int value){
    if(p){
        if(p->value==value){
            return p;
        }else if(p->value>value){
            return search(p->lc,value);
        }else{
            return search(p->rc,value);
        }
    }else{
        return NULL;
    }
}

void remove(Pnode p,int value){
    Pnode fa = rt;
    int flag = 1;
    while(p){
        if(p->value==value){
            flag = 0;
            break;
        }
    }
    if(flag){
        return; 
    }
    if(NULL==p->lc && NULL==p->rc){
        if(p==fa){ // BST has only one node
            rt = NULL;
        }else{
            if(fa->lc==p){
                fa->lc = NULL;
            }else{
                fa->rc = NULL;
            }
            delete p;
        }
        return ;
    }else if(NULL==p->lc){
        Pnode tmp1 = p;
        Pnode tmp2 = p->rc;
        while(tmp2->lc){
            p = tmp2;
            tmp2 = tmp2->lc;
            p = tmp2;
        }
        tmp1->value = tmp2->value;
        p->lc = tmp2->rc;
        delete tmp2;
    }else{
        Pnode tmp1 = p;
        Pnode tmp2 = p->lc;
        while(tmp2->rc){
            p = tmp2;
            tmp2 = tmp2->rc;
            p = tmp2;
        }
        tmp1->value = tmp2->value;
        p->rc = tmp2->lc;
        delete tmp2;
    }
    return;
}

void preorder_traversal(Pnode p){
    if(p){
        cout<<p->value<<' ';
        preorder_traversal(p->lc);
        preorder_traversal(p->rc);
    }
}

void inorder_traversal(Pnode p){
    if(p){
        inorder_traversal(p->lc);
        cout<<p->value<<' ';
        inorder_traversal(p->rc);
    }
}


int main(){
    while(true){
        cout<<"1:insert\t";
        cout<<"2:search\t";
        cout<<"3:delete"<<endl;
        int choice;
        cin>>choice;
        cout<<"value\t";
        int value;
        cin>>value;
        switch(choice){
            case 1:
                insert(rt,value);
                cout<<"preorder traversal:\t";
                preorder_traversal(rt);
                cout<<endl;
                cout<<"inorder traversal:\t";
                inorder_traversal(rt);
                cout<<endl;
                break;
            case 2:
                cout<<search(rt,value)<<endl;
                break;
            case 3:
                remove(rt,value);
                cout<<"preorder traversal:\t";
                preorder_traversal(rt);
                cout<<endl;
                cout<<"inorder traversal:\t";
                inorder_traversal(rt);
                cout<<endl;
                break;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值