共计 660 个字符,预计需要花费 2 分钟才能阅读完成。
#include<iostream> | |
using namespace std; | |
typedef struct BiNode{ | |
char data; | |
struct BiNode *lchild,*rchild; | |
}BiTNode,*BiTree; | |
// 先序建设二叉链表 | |
void CreateBiTree(BiTree &T){ | |
char ch; | |
cin >> ch; | |
if(ch=='#') T=NULL; | |
else{ | |
T=new BiTNode; | |
T->data=ch; | |
CreateBiTree(T->lchild); | |
CreateBiTree(T->rchild); | |
} | |
} | |
// 先序遍历 | |
void Pre(BiTree T){if(T){ | |
cout<<T->data; | |
Pre(T->lchild); | |
Pre(T->rchild); | |
} | |
} | |
// 中序遍历 | |
void In(BiTree T){if(T){In(T->lchild); | |
cout<<T->data; | |
In(T->rchild); | |
} | |
} | |
// 后序遍历 | |
void Post(BiTree T){if(T){Post(T->lchild); | |
Post(T->rchild); | |
cout<<T->data; | |
} | |
} | |
int main(){ | |
BiTree tree; | |
cout<<"先序建设二叉链表:"; | |
CreateBiTree(tree); | |
cout<<"先序遍历的后果为:"; | |
Pre(tree); | |
cout<<endl; | |
cout<<"中序遍历的后果为:"; | |
In(tree); | |
cout<<endl; | |
cout<<"后序遍历的后果为:"; | |
Post(tree); | |
cout<<endl; | |
} |
正文完