#include<iostream.h>
#define max 50
struct node
{
char data;
node *left,*right;
int ltag,rtag;
};
void creat(node * &head,char st1[])
{
node *stt[max],*p;
int top=-1,k,j=0;
char m;
head=NULL;
m=st1[j];
while(m!='\0')
{
if(m=='(')
{
top++;
stt[top]=p;
k=1;
}
else if(m==')')
{
top--;
}
else if(m==',')
{
k=2;
}
else
{
p=new node;
p->data=m;
p->ltag=0;
p->rtag=0;
p->left=NULL;
p->right=NULL;
if(head==NULL)
{
head=p;
}
else
{
if(k==1)
{
stt[top]->left=p;
}
if(k==2)
{
stt[top]->right=p;
}
}
}
j++;
m=st1[j];
}
}
void creattree(node * &head)
{
//root=NULL;
node *p,*q;
p=head;
q=NULL;
node *stt1[max];
int top=-1;
while(1)
{
while(p!=NULL)
{
top++;
stt1[top]=p;
p=p->left;
}
if(top<0)
{
return;
}
else
{
p=stt1[top];
top--;
if(q!=NULL)
{
if(q->right==NULL)
{
q->rtag=1;
q->right=p;
}
if(p->left==NULL)
{
p->ltag=1;
p->left=q;
}
}
q=p;
p=p->right;
}
}
}
void disp(node * &head)
{
if(head->ltag==0)
{
disp(head->left);
}
if(head->rtag==0)
{
disp(head->right);
}
cout<<head->data<<endl;
}
void main()
{
node *head;
char tr[max];
cout<<"输入广义表表达式:";
cin>>tr;
creat(head,tr);
//qingling(head);
cout<<89;
creattree(head);
cout<<4545;
cout<<head->ltag<<","<<head->rtag;
// disp(head);
}
c++编写的二叉树遍历程序
需积分: 10 87 浏览量
2008-12-28
16:37:39
上传
评论
收藏 208KB RAR 举报
wwwu_06
- 粉丝: 3
- 资源: 1
最新资源
- 基于Typescript的兔子饭店经营类游戏源码设计免费送cocoscreator
- 基于Java的web快速开发数据权限管理脚手架wonder-server设计源码
- 基于Apache Log4cxx的C++日志库设计源码
- 基于Vue3的likeadmin免费任意商用管理后台设计源码
- 基于JavaScript的Chrome扩展WeNote分享插件设计源码
- 基于C++的中泰EM9108S动态库开发示例源码
- gxlx2-p291-1g.dts和gxlx2-p291-1g.dtb
- STM32WBxx Keil芯片包
- 基于CNN+Bi-LSTM+Attention 的自动对对联系统
- 基于Java的IndexBar Android字母索引栏设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈