二叉排序树完整程序

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct node
{
    int date;
    struct node *lchild,*rchild;
}BStree;
void Insert(BStree **bst,int key)
{
    BStree *s;
    if(*bst==NULL)
    {
        s=(BStree *)malloc(sizeof(BStree));
        s->date=key;
        s->lchild=NULL;
        s->rchild=NULL;
        *bst=s;
    }
    else if(key<(*bst)->date)
    {
        Insert(&((*bst)->lchild),key);
    }
    else if(key>(*bst)->date)
    {
        Insert(&((*bst)->rchild),key);
    }
}
void CreateBStree(BStree **bst)
{
    int key;
    *bst=NULL;
    while(cin>>key&&key)
    {
        Insert(bst,key);
    }
}
void Inorder(BStree *bst)
{
    if(bst!=NULL)
    {
        Inorder(bst->lchild);
        cout<<bst->date<<" ";
        Inorder(bst->rchild);
    }
}
BStree *Search(BStree *bst,int key)//递归方式
{
    if(bst==NULL) return NULL;
    else if(key==bst->date) return bst;
    else if(key<bst->date) return Search(bst->lchild,key);
    else if(key>bst->date) return Search(bst->rchild,key);
}
BStree *Search1(BStree *bst,int key)//非递归方式
{
    BStree *t;
    t=bst;
    while(t)
    {
        if(key==t->date) return t;
        else if(key<t->date) t=t->lchild;
        else if(key>t->date) t=t->rchild;
    }
    return NULL;
}
void Delete(BStree *bst,int key)
{
    BStree *p,*f,*q,*s;
    p=bst;//p为要删除的节点
    f=NULL;//f为p的父节点
    while(p)//找到key的位置(即p的位置)
    {
        if(key==p->date) break;
        f=p;
        if(key<p->date) p=p->lchild;
        else if(key>p->date) p=p->rchild;
    }
     if(p==NULL) return;//判断空树
     if(p->lchild==NULL)
     {
         if(f==NULL) p=p->rchild;
         else if(f->lchild==p) f->lchild=p->rchild;
         else if(f->rchild==p) f->rchild=p->rchild;
         free(p);
     }
     else
     {
         q=p;s=p->lchild;
         while(s->rchild)
         {
             q=p;
             s=s->rchild;
         }
         if(q==p) p->lchild=s->lchild;//如果p的左孩子没有右孩子,则直接把p的左孩子往上移
         else q->rchild=s->lchild;
         p->date=s->date;
         free(s);
     }
}
void menu()
{
    cout<<"------------------------------"<<endl;
    cout<<"1-----------------Create BStree"<<endl;
    cout<<"2-----------------Insert element"<<endl;
    cout<<"3-----------------Inorder tree"<<endl;
    cout<<"4-----------------Search element"<<endl;
    cout<<"5-----------------delete element"<<endl;
    cout<<"0-----------------Exit"<<endl;
    cout<<"Input sequence number corresponding function ";
}
int main()
{
    std::ios::sync_with_stdio(false);
    BStree *bst;
    bst=(BStree *)malloc(sizeof(BStree));
    int chosen;
    menu();
    cin>>chosen;
    while(1)
    {

        int key;
        switch(chosen)
        {
            case 1:
                cout<<"Enter the element to create the tree(input 0 end) ";
                CreateBStree(&bst);
                break;
            case 2:
                cout<<"Enter the element to be inserted(input 0 ends) ";
                while(cin>>key&&key)
                {
                    Insert(&bst,key);
                }
                break;
            case 3:
                cout<<"The middle sequence is ";
                Inorder(bst);
                cout<<endl;
                break;
            case 4:
                cout<<"Enter the element you are looking for ";
                cin>>key;
                BStree *ans;
                ans=Search1(bst,key);
                if(ans==NULL) cout<<"NO"<<endl;
                else cout<<ans->date<<endl;
                break;
            case 5:
                cout<<"Enter the deleted element ";
                cin>>key;
                Delete(bst,key);
                break;
            case 0:
                break;
        }
        if(chosen==0) break;
        menu();
        cin>>chosen;
    }


    Inorder(bst);
    cout<<endl;
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值