二叉树问题

#include<iostream.h>
#include<malloc.h>
#define maxnode 1000
typedef  char elemtype;
typedef struct bintnode
{
   elemtype data;
   struct bintnode *lchild,*rchild,*parent;
}bintnode,*bintree;
void createbintree(bintree *t)     //构建一个二叉树链表
{
  char ch;  
  cin>>ch;
  if(ch=='0') *t='/0';
    else
    {
      *t=(bintnode*)malloc(sizeof(bintnode));
      (*t)->data=ch;
      createbintree(&(*t)->lchild);
      createbintree(&(*t)->rchild);
    }
}
void preorderout(bintree t)      //先序序列的输出
{
    if(t)
    {
    cout<<t->data;
        preorderout(t->lchild);
        preorderout(t->rchild);
    }
}
void inorderout(bintree t)      //中序序列的输出
{
    if(t)
 {
         inorderout(t->lchild);
         cout<<t->data;
         inorderout(t->rchild);
 }
}
void postorderout(bintree t)    //后序序列的输出
{
    if(t)
 {
  postorderout(t->lchild);
        postorderout(t->rchild);
  cout<<t->data;
 }
}
void leveorder(bintree t)   //层次遍历的输出
{
     bintree queue[maxnode];
     int front,rear;
     if(t=='/0') return;
        front=-1;
        rear=0;
        queue[rear]=t;
     while(front!=rear)
  {
        front++;
        cout<<queue[front]->data;
        if(queue[front]->lchild!='/0')
  {
           rear++;
           queue[rear]=queue[front]->lchild;
  }
       if(queue[front]->rchild !='/0')
    {
           rear++;
           queue[rear]=queue[front]->rchild ;
    }
 }
}
//主函数
void  main()
{
    bintree bt;
 cout<<"输入二叉树:";
    createbintree(&bt);
    cout<<"输出先序遍历:";
    preorderout(bt);
 cout<<endl;
 cout<<"输出中序遍历:";
    inorderout(bt);
 cout<<endl;
 cout<<"输出后序遍历:";
    postorderout(bt);
 cout<<endl;
 cout<<"层次遍历:";
 leveorder(bt);
 cout<<endl;
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值