#include <iostream> using namespace std; //二叉树的数据结构定义 typedef struct node{ char data; struct node *lchild,*rchild; }BTNode,*BTREE; void create(BTREE &T); void match(BTREE T); int main(int argc, const char * argv[]) { BTREE T; create(T); match(T); cout<<endl; return 0; } //先序遍历创建二叉树,输入0代表结点为空 void create(BTREE &T){ char ch; cout <<"请输入"<<endl; cin>>ch; cout<<endl; if(ch=='0') T==NULL; else{ T = new BTNode(); //T = (BTREE)malloc(sizeof(BTNode)); T->data = ch; create(T->lchild); create(T->rchild); } } //匹配规则 void match(BTREE T){ if(T) { cout<<T->data; if(T->lchild&&T->rchild) { cout<<"("; match(T->lchild); cout<<","; match(T->rchild); cout<<")"; } else if(T->lchild&&T->rchild==NULL) { cout<<"("; match(T->lchild); cout<<","; cout<<")"; } else if(T->lchild==NULL&&T->rchild) { cout<<"("; cout<<","; match(T->rchild); cout<<")"; } } }