您的位置:首页 > 理论基础 > 数据结构算法

数据结构-实验四 二叉树

2016-10-27 12:55 295 查看
数据结构- 二叉树递归:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<utility>

using namespace std;

typedef struct BiTNode{
char date;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

void Pre_CreateBiTree(BiTree &T)
{
char ch;
scanf("%c",&ch);
if(ch=='#') T=NULL;
else {
T=new BiTNode;
T->date=ch;
Pre_CreateBiTree(T->lchild);
Pre_CreateBiTree(T->rchild);
}
}

void Pre_Traverse(BiTree T)
{
if(T) {
printf("%c",T->date);
Pre_Traverse(T->lchild);
Pre_Traverse(T->rchild);
}
}

void In_Traverse(BiTree T)
{
if(T) {
In_Traverse(T->lchild);
printf("%c",T->date);
In_Traverse(T->rchild);
}
}

void Post_Traverse(BiTree T)
{
if(T) {
Post_Traverse(T->lchild);
Post_Traverse(T->rchild);
printf("%c",T->date);
}
}

void Lever_Traverse(BiTree T)
{
queue<BiTNode*> Q;
if(T) Q.push(T);
while(!Q.empty()) {
BiTree tem=Q.front(); Q.pop();
printf("%c",tem->date);
if(tem->lchild) Q.push(tem->lchild);
if(tem->rchild) Q.push(tem->rchild);
}
}

void PreorderTraverse(BiTree root)
{
stack<BiTNode*> S;
BiTree top=root;
BiTNode *tem=new BiTNode;
while(top||!S.empty())
{
if(top) {
printf("%c",top->date);
S.push(top);
top=top->lchild;
}
else {
tem=S.top(); S.pop();
top=tem->rchild;
}
}
}

void InorderTraverse(BiTree root)
{
stack<BiTNode*> S;
BiTree p=root;
BiTNode* q=new BiTNode;
while(p||!S.empty()) {
if(p) {
S.push(p); p=p->lchild;
}
else {
q=S.top(); S.pop();
printf("%c",q->date);
p=q->rchild;
}
}
}

void PostoderTraverse(BiTree root)
{
stack<pair<BiTNode*,bool> > S;
BiTree to=root;
BiTNode *tem=new BiTNode;
while(to||!S.empty()) {
if(to) {
pair<BiTNode*,bool> P(to,false);
S.push(P);
}
else {
pair<BiTNode*,bool> P2=S.top(); S.pop();
if(!P2.second) {
P2.second=true; S.push(P2);
to=P2.first->rchild;
}
else {
printf("%c",P2.first->date);
}
}
}
}
int Count_Leaf(BiTree T)
{
if(!T) return 0;
else if(T->lchild||T->rchild) return Count_Leaf(T->lchild)+Count_Leaf(T->rchild);
else return 1;
}
void Exchange_LR(BiTree &T)
{
if(T) {
// swap(T->lchild,T->rchild);
Exchange_LR(T->lchild);
Exchange_LR(T->rchild);
swap(T->lchild,T->rchild);
}
}
void Exchange_LR_Mid(BiTree &T)
{
if(T) {
Exchange_LR(T->lchild);
swap(T->lchild,T->rchild);
Exchange_LR(T->lchild);
}
}
int main()
{
freopen("stdin.txt","r",stdin);
freopen("stdout.txt","w",stdout);
BiTree root=NULL;

printf("请输入创建的二叉树(先序序列): ");
Pre_CreateBiTree(root);

printf("\n二叉树叶子结点数目: %d\n", Count_Leaf(root));

printf("\n原二叉树: ");
printf("\n先序遍历: "); Pre_Traverse(root);
printf("\n中序遍历: "); In_Traverse(root);
printf("\n后序遍历: "); Post_Traverse(root);
printf("\n按层遍历: "); Lever_Traverse(root);

Exchange_LR_Mid(root);

printf("\n左右子树交换之后的二叉树:\n");
printf("\n先序遍历: "); Pre_Traverse(root);
printf("\n中序遍历: "); In_Traverse(root);
printf("\n后序遍历: "); Post_Traverse(root);
printf("\n按层遍历: "); Lever_Traverse(root);

return 0;
}

非递归:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<utility>

using namespace std;

typedef struct BiTNode{
char date;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

void Pre_CreateBiTree(BiTree &T)
{
char ch;
scanf("%c",&ch);
if(ch=='#') T=NULL;
else {
T=new BiTNode;
T->date=ch;
Pre_CreateBiTree(T->lchild);
Pre_CreateBiTree(T->rchild);
}
}
void PreorderTraverse(BiTree root)
{
stack<BiTNode*> S;
BiTree top=root;
BiTNode *tem=new BiTNode;
while(top||!S.empty())
{
if(top) {
printf("%c",top->date);
S.push(top);
top=top->lchild;
}
else {
tem=S.top(); S.pop();
top=tem->rchild;
}
}
}

void InorderTraverse(BiTree root)
{
stack<BiTNode*> S;
BiTree p=root;
BiTNode* q=new BiTNode;
while(p||!S.empty()) {
if(p) {
S.push(p); p=p->lchild;
}
else {
q=S.top(); S.pop();
printf("%c",q->date);
p=q->rchild;
}
}
}

void PostorderTraverse(BiTree root)
{
stack<pair<BiTNode*,bool> > S;
BiTree top=root;
BiTNode *tem=new BiTNode;
while(top||!S.empty()) {
if(top) {
pair<BiTNode*,bool> P(top,false);
S.push(P);
top=top->lchild;
}
else {
pair<BiTNode*,bool> P2=S.top(); S.pop();
if(!P2.second) {
P2.second=true; S.push(P2);
top=P2.first->rchild;
}
else {
printf("%c",P2.first->date);
}
}
}
}
//
//void PostorderTraverse(BiTree root)
//{
// stack<BiTNode*> S;
// BiTree top=root,pre=NULL;
// S.push(top);
// while(!S.empty()) {
// top=S.top();
// if( (!top->lchild&&!top->rchild) || (pre&&(pre==top->lchild||pre==top->rchild)) )
// {
// S.pop(); printf("%c",top->date); pre=top;
// }
// else {
// if(top->rchild) S.push(top->rchild);
// if(top->lchild) S.push(top->lchild);
// }
// }
//}
int main()
{
freopen("stdin.txt","r",stdin);
freopen("stdout.txt","w",stdout);
BiTree root=NULL;

printf("请输入创建的二叉树(先序序列): ");
Pre_CreateBiTree(root);

printf("\n非递归先序遍历二叉树序列: ");
PreorderTraverse(root);

printf("\n非递归中序遍历二叉树序列: ");
InorderTraverse(root);

printf("\n非递归后序遍历二叉树序列: ");
PostorderTraverse(root);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: