8607 实现二叉排序树的各种算法(1)

#include<iostream>
using namespace std;
int a[200];

typedef struct node//二叉排序树结构体
{
    int data;
    struct node *lchild,*rchild;
}node,*bst;

typedef struct//队列结构体
{
    bst *base;
    int head;
    int tail;
}queue;

typedef struct
{
    bst *base;
    bst *top;
    int stacksize;
}stack;


int initialstack(stack &s)
{
   s.base=new bst[200];
   if(s.base==NULL) return 0;
   s.stacksize=200;
   s.top=s.base;
   return 1;
}

int push(stack &s,bst e)
{
   if(s.top-s.base==s.stacksize) return 0;
   *s.top++=e;
   return 1;
}

int pop(stack &s,bst &e)
{
    if(s.top==s.base) return 0;
    e=*--s.top;
    return 1;
}
int initialqueue(queue &q)//队列初始化
{
    q.base=new bst[200];
    if(q.base==NULL) return 0;
    q.head=q.tail=0;
    return 1;
}

int in(queue &q,bst e)//队列入队
{
    if((q.tail+1)%200==q.head) return 0;
    q.base[q.tail]=e;
    q.tail=(q.tail+1)%200;
    return 1;
}

int out(queue &q,bst &e)//队列出队
{
    if(q.head==q.tail) return 0;
    e=q.base[q.head];
    q.head=(q.head+1)%200;
    return 1;
}

void ins(bst &t,int n)//二叉排序树插入
{
    if(t==NULL){t=new node;
    t->data=n;t->lchild=t->rchild=NULL;//cout<<t->data;
    return ;
    }

    else if(n<t->data) return ins(t->lchild,n);


    else if(n>t->data) return ins(t->rchild,n);

}

void prego(bst t)//先序遍历
{
    if(t)
    {
        cout<<t->data<<" ";
        prego(t->lchild);
        prego(t->rchild);
    }
}
void midgo(bst t)//中序遍历
{
    if(t)
    {
        midgo(t->lchild);
        cout<<t->data<<" ";
        midgo(t->rchild);
    }
}
void reargo(bst t)//后序遍历
{
    if(t)
    {
        reargo(t->lchild);
        reargo(t->rchild);
        cout<<t->data<<" ";
    }
}
int search(bst t,int n)//利用二叉排序树二分查找
{
    if(t==NULL) return 0;
    else
    {
        if(n==t->data) return 1;
        else if(n>t->data) return search(t->rchild,n);
        else if(n<t->data) return search(t->lchild,n);
    }
}

void levelgo(bst t,queue &q)//层次遍历
{
    in(q,t);
     while(q.head!=q.tail)
     {
        bst e;
        out(q,e);
        cout<<e->data<<" ";
        if(e->lchild!=NULL) in(q,e->lchild);
        if(e->rchild!=NULL) in(q,e->rchild);
     }
     cout<<endl;
}

void norecurmid(bst t,stack &s)//非递归中序遍历
{
    while(t!=NULL||s.base!=s.top)//只有遍历完并且栈空了才结束,单独一个栈为空是不够的,因为一开始就是空栈,单独一个t为NULL也是不够的,因为这只代表你遍历完一条分支,此时栈是非空的
    {
        if(t)
        {
            push(s,t);
            t=t->lchild;
        }
        else
        {
            pop(s,t);
            cout<<t->data<<" ";
            t=t->rchild;
        }
    }
    cout<<endl;
}

int main()
{
    bst t=NULL;
    int n;cin>>n;

    for(int i=0;i<n;i++) {
        cin>>a[i];

        ins(t,a[i]);
    }

    prego(t);cout<<endl;
    midgo(t);cout<<endl;
    reargo(t);cout<<endl;


    int m;
    cin>>m;cout<<search(t,m)<<endl;
    cin>>m;cout<<search(t,m)<<endl;


    int cha;
    cin>>cha;ins(t,cha);
    prego(t);cout<<endl;
    midgo(t);cout<<endl;
    reargo(t);cout<<endl;

    stack s;
    initialstack(s);
    norecurmid(t,s);


    queue q;
    initialqueue(q);
    levelgo(t,q);

    
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值