Chinaunix首页 | 论坛 | 博客
  • 博客访问: 587045
  • 博文数量: 68
  • 博客积分: 5070
  • 博客等级: 大校
  • 技术积分: 1312
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-11 14:20
文章分类

全部博文(68)

文章存档

2011年(3)

2010年(30)

2009年(17)

2008年(18)

我的朋友

分类: C/C++

2008-04-07 15:59:08

#include
using namespace std;
struct bitree
{
char data;
bitree *lchild;
bitree *rchild;
};

bitree *create()      //构造树
{
char ch;
cout<<"请输入节点(结束时用#表示):"<cin>>ch;
bitree *root,*p,*s[100];
int front=1;
   
while(ch!='#')
{
      p=NULL;
   
  
    p=new bitree;
    p->data=ch;
       p->lchild=NULL;
    p->rchild=NULL;
   

   s[front]=p;
   if(front==1) {root=s[front];front++;}
        else
   {  
   
     if(front%2==0) {s[front/2]->lchild=s[front];front++;}
     else if(front%2==1)
     {s[(front-1)/2]->rchild=s[front];front++;}
  
   }
   cin>>ch;
  
}
return root;
}

void preorder(bitree *root)       //先序遍历
{
bitree *p;
p=root;
if(p!=NULL)
{
   cout<data<<" ";
   preorder(p->lchild);
   preorder(p->rchild);
}
}
void inorder(bitree *root)      // 中序遍历
{
   bitree *p;
   p=root;
   if(p!=NULL)
   {inorder(p->lchild);
   cout<data<<" ";
   inorder(p->rchild);
   }

}

void postorder(bitree *root)      //后序遍历
{
bitree *p;
p=root;
if(p!=NULL)
{
   postorder(p->lchild);
   postorder(p->rchild);
   cout<data<<" ";
}
}

void main()
{
   bitree *root;
   root=create();
   preorder(root);
   cout<   inorder(root);
   cout<   postorder(root);
   cout<  
}

阅读(627) | 评论(0) | 转发(0) |
0

上一篇:网站按钮问题

下一篇:表达式转二叉树

给主人留下些什么吧!~~