#include<iostream> #include <stdlib.h> #include<stdio.h> using namespace std; struct BinNode{ char data; struct BinNode *lchild,*rchild; }; BinNode * T; void creatBinTree(BinNode * &T); void Preorder(BinNode * &T); void Inorder(BinNode * &T); void Posorder(BinNode * &T); void main(){ cout<<"创建一棵树以“#”代表空树"; creatBinTree(T); cout<<"先序遍历"; Preorder(T); cout<<endl; cout<<"中序遍历"; Inorder(T); cout<<endl; cout<<"后序遍历"; Posorder(T); cout<<endl; system("pause"); } void creatBinTree(BinNode * &T){ char ch; if((ch=getchar())==‘#‘) T=NULL; else{ T=new BinNode; T->data=ch; creatBinTree(T->lchild); creatBinTree(T->rchild); } } void Preorder(BinNode * &T){ if(T){ cout<<T->data; Preorder(T->lchild); Preorder(T->rchild); } else cout<<""; } void Inorder(BinNode * &T){ if(T){ Inorder(T->lchild); cout<<T->data; Inorder(T->rchild); } else cout<<""; } void Posorder(BinNode * &T){ if(T){ Posorder(T->lchild); Preorder(T->rchild); cout<<T->data; } else cout<<""; }
时间: 2024-10-13 05:40:57