建立二叉树,并利用递归方法实现先序、中序、后序遍历。
发布网友
发布时间:2022-05-01 17:09
我来回答
共1个回答
热心网友
时间:2022-04-18 21:39
#include
#include
#include
#include
#include
using namespace std;
struct node;
typedef node *tree;
struct node{
char data;
tree lchild,rchild;
};
tree bt;
void build(tree &bt){
char ch;
ch=getchar();
if(ch!='.'){
bt=new node;
bt->data=ch;
build(bt->lchild);
build(bt->rchild);
}
else
bt=NULL;
}
void prework(){
ios::sync_with_stdio(false);
//freopen("data.in","r",stdin);
build(bt); //建树
}
void preorder(tree bt){
if(bt){
cout<data;
preorder(bt->lchild);
preorder(bt->rchild);
}
}
void midorder(tree bt){
if(bt){
preorder(bt->lchild);
cout<data;
preorder(bt->rchild);
}
}
void backorder(tree bt){
if(bt){
preorder(bt->lchild);
cout<data;
preorder(bt->rchild);
}
}
void mainwork(){
preorder(bt);
cout<<endl;
midorder(bt);
cout<<endl;
backorder(bt);
}
int main(){
prework();
mainwork();
return 0;
}
//我这里输入的东西是要求叶子节点的子节点为'.'
但仍无铃声,则送维修店维修。三无受话现象: