您的位置:首页 > 其它

二叉树递归,非递归,前中后遍历

2013-11-26 15:38 239 查看
又是一个数据结构的实验。这个实验要求实现二叉树的前,中,后序递归,非递归遍历。

一开始,想了一个最普遍的做法,其实也是实验题目上的建议。比如,输入1 2 0 0 3 0 0,0表示节点为空。

后来在网上逛了逛,发现这种输入形式的二叉树比较清晰,比如1(2,3) 显然,2是1的左孩子,3是1的右孩子。其实实现的办法也不是很复杂,定义一个字符数组s[100],再定义两个栈,一个用来放节点数据,一个用来放“(”,“,”,“)”,

输出的时候,再加一些判断就可以。下面是源程序,里面有二叉树的前,中,后序递归,非递归遍历的功能,也暂时达到了实验的要求,过段日子打算把左右孩子节点交换,求二叉树节点个数,高度也加进去。

运行结果:





源程序:

#include<iostream>
#include<string.h>
#include<stack>
char s[100];
using namespace std;
//定义树的节点结构
typedef struct node
{
char data;           //节点数据
struct node *lchild; //指向左孩子的指针
struct node *rchild; //指向右孩子的指针
}Bin_Tree;

typedef struct node1
{
Bin_Tree *btnode;
bool isFirst;
}BTNode;
//创建二叉树,s为A(B(C,D))结构的字符串
Bin_Tree *Creat_Tree(char *s,Bin_Tree *&root)
{
int i;
bool isRight=false;
stack<Bin_Tree*> s1;          //创建BinTree类型栈存放结点
stack<char> s2;              //创建char类型栈存放分隔符
Bin_Tree *p;
Bin_Tree *temp;
root->data=s[0];
root->lchild=NULL;
root->rchild=NULL;
s1.push(root);               //根节点进栈
i=1;
while(i<strlen(s))
{
if(s[i]=='(')
{
s2.push(s[i]);       //如果输入是"(",就将其放入s2
isRight=false;
}
else
if(s[i]==',')
{
isRight=true;
}
else
if(s[i]==')')
{
s1.pop();
s2.pop();
}
else
if(isalpha(s[i]))
{
p=(Bin_Tree *)malloc(sizeof(Bin_Tree));
p->data=s[i];
p->lchild=NULL;
p->rchild=NULL;
temp=s1.top();
if(isRight==true)
{
temp->rchild=p;
cout<<temp->data<<"的右孩子是"<<s[i]<<endl;
}
else
{
temp->lchild=p;
cout<<temp->data<<"的左孩子是"<<s[i]<<endl;
}
if(s[i+1]=='(')
s1.push(p);
}
i++;
}
return root;
}
//显示树形结构
//-----------------------------------------
void Display_Tree(Bin_Tree *root)
{
if(root!=NULL)
{
cout<<root->data;
if(root->lchild!=NULL)
{
cout<<'(';
Display_Tree(root->lchild);
}
if(root->rchild!=NULL)
{
cout<<',';
Display_Tree(root->rchild);
cout<<')';
}
}
}
//***************************************
//递归前,中,后序遍历
//***************************************
//递归前序遍历
void preorder(Bin_Tree *root)
{
if(root!=NULL)
{
cout<<root->data<<" ";
preorder(root->lchild);
preorder(root->rchild);
}
}
//递归中序遍历
void inorder(Bin_Tree *root)
{
if(root!=NULL)
{
inorder(root->lchild);
cout<<root->data<<" ";
inorder(root->rchild);
}
}
//递归后序遍历
void postorder(Bin_Tree *root)
{
if(root!=NULL)
{
postorder(root->lchild);
postorder(root->rchild);
cout<<root->data<<" ";
}
}
//***************************************
//非递归前,中,后序遍历
//***************************************
//非递归前序遍历
void pre_order(Bin_Tree *root)
{
stack<Bin_Tree*> s;
Bin_Tree *p=root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
cout<<p->data<<" ";
s.push(p);
p=p->lchild;
}
if(!s.empty())
{
p=s.top();
s.pop();
p=p->rchild;
}
}
}
//非递归中序遍历
void in_order(Bin_Tree *root)
{
stack<Bin_Tree*> s;
Bin_Tree *p=root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
s.push(p);
p=p->lchild;
}
if(!s.empty())
{
p=s.top();
cout<<p->data<<" ";
s.pop();
p=p->rchild;
}
}
}
//非递归后序遍历
void post_order(Bin_Tree *root)
{
stack<BTNode*> s;
Bin_Tree *p=root;
BTNode *temp;
while(p!=NULL||!s.empty())
{
while(p!=NULL)              //沿左孩子一直往下找,直至出现没有左孩子的结点
{
BTNode *btn=(BTNode *)malloc(sizeof(BTNode));
btn->btnode=p;
btn->isFirst=true;
s.push(btn);
p=p->lchild;
}
if(!s.empty())
{
temp=s.top();
s.pop();
if(temp->isFirst==true)     //表示是第一次出现在栈顶
{
temp->isFirst=false;
s.push(temp);
p=temp->btnode->rchild;
}
else                        //第二次出现在栈顶
{
cout<<temp->btnode->data<<" ";
p=NULL;
}
}
}
}

//功能界面函数
//*****************************************************************
void Menu()
{
int n;
loop:
cout<<endl<<"输入数字选择功能!"<<endl<<endl;
cout<<"1.创建二叉树"<<endl;
cout<<"2.显示二叉树"<<endl;
cout<<"3.递归实现前,中,后遍历"<<endl;
cout<<"4.非递归实现前,中,后遍历"<<endl;
cout<<"5.退出程序"<<endl;
cout<<endl<<"选择功能: ";
cin>>n;
if(n==1)
{
cout<<"请输入二叉树(注意!A(B(C,D))结构输入)"<<endl;
cin>>s;
Bin_Tree *root=(Bin_Tree *)malloc(sizeof(Bin_Tree));
Creat_Tree(s,root);
goto loop;
}
else
if(n==2)
{
Bin_Tree *root=(Bin_Tree *)malloc(sizeof(Bin_Tree));
root=Creat_Tree(s,root);
cout<<endl<<"所建二叉树的结构是:";
Display_Tree(root);
cout<<endl;
goto loop;
}
else
if(n==3)
{
Bin_Tree *root=(Bin_Tree *)malloc(sizeof(Bin_Tree));
root=Creat_Tree(s,root);
cout<<endl<<"递归前序遍历:";
preorder(root);
cout<<endl<<"递归中序遍历:";
inorder(root);
cout<<endl<<"递归后序遍历:";
postorder(root);
cout<<endl;
goto loop;
}
else
if(n==4)
{
Bin_Tree *root=(Bin_Tree *)malloc(sizeof(Bin_Tree));
root=Creat_Tree(s,root);
cout<<endl<<"非递归前序遍历:";
pre_order(root);
cout<<endl<<"非递归前序遍历:";
in_order(root);
cout<<endl<<"非递归前序遍历:";
post_order(root);
cout<<endl;
goto loop;
}
else
if(n==5)
{
exit(0);
}
else
{
cout<<endl<<"不在功能选择范围之内!请重新输入!"<<endl<<endl;
goto loop;
}
}
//主函数
//************************************************************************
int main()
{
Menu();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: