链式二叉树的创建及遍历

#include<iostream> using namespace std; typedef struct node { char data; struct node *lchild,*rchild; }tree, bitree; void creat(bitree *&t) { char ch; cin>>ch; if(ch=='-') { t=NULL; return ; } else { t=new tree; t->data=ch; creat(t->lchild); creat(t->rchild); } } void Pre_print(bitree *&t)//前序遍历...... { if(t) { cout<<t->data; Pre_print(t->lchild); Pre_print(t->rchild); } } void Mid_print(bitree *&t)//中序遍历..... { if(t) { Mid_print(t->lchild); cout<<t->data; Mid_print(t->rchild); } else { return ; } } void Back_print(bitree *&t)//后序遍历..... { if(t) { Back_print(t->lchild); Back_print(t->rchild); cout<<t->data; } } int main() { bitree *t; int n; cin>>n; while(n--) { creat(t); Pre_print(t); cout<<endl; Mid_print(t); cout<<endl; Back_print(t); cout<<endl; } return 0; }

你可能感兴趣的:(struct,tree)