//创建树
#include<iostream>
#include<malloc.h>
#define MAX 100
using namespace std;
typedef struct node
{
char data;
struct node *right,*left;
}btree;
void createTree(btree *&b,string s)
{
int top=-1;
btree *p,*stack[MAX];
int j=0;
int k;
b=NULL;
char c=s[j];
while(c!='/0')
{
switch(c)
{
case '(':top++;stack[top]=p;k=1;break;
case ',':k=2;break;
case ')':top--;break;
default:
p=(btree *)malloc(sizeof(btree));
p->data=c;
p->right=p->left=NULL;
if(b==NULL)
{
b=p;
}
else
{
switch(k)
{
case 1:stack[top]->left=p;break;
case 2:stack[top]->right=p;break;
}
}
}
j++;
c=s[j];
}
}
void printTree(btree *b)
{
if(b!=NULL)
{
cout<<b->data;
if(b->left!=NULL || b->right!=NULL)
{
cout<<"(";
printTree(b->left);
if(b->right!=NULL)
{
cout<<",";
printTree(b->right);
cout<<")";
}
}
}
}
void inorder_recursion(btree *b)
{
if(b!=NULL)
{
inorder_recursion(b->left);
cout<<b->data<<" ";
inorder_recursion(b->right);
}
}
void firsOrder(btree *b)
{
btree *stack[MAX],*p;
int top=-1;
if(b!=NULL)
{
top++;
stack[top]=b;
while(top>=0)
{
p=stack[top];
top--;
cout<<p->data<<" ";
if(p->right!=NULL)
{
top++;
stack[top]=p->right;
}
if(p->left!=NULL)
{
top++;
stack[top]=p->left;
}
}
}
}
void firsOrderS(btree *b)
{
if(b!=NULL)
{
cout<<b->data<<" ";
firsOrder(b->left);
firsOrder(b->right);
}
}
//我写的中序非递归遍历
void inorder(btree *b)
{
btree *p,*stack[MAX];
int top=-1;
int flag=0;
if(b!=NULL)
{
top++;
stack[top]=b;
while(top>=0)
{
p=stack[top];
while(p->left!=NULL && !flag)
{
top++;
p=p->left;
stack[top]=p;
}
flag=1;
p=stack[top];
top--;
cout<<p->data<<" ";
if(p->right!=NULL)
{
top++;
p=p->right;
stack[top]=p;
flag=0;
}
}
}
}
void psorder_recursion(btree *b)
{
if(b!=NULL)
{
psorder_recursion(b->left);
psorder_recursion(b->right);
cout<<b->data<<" ";
}
}
//这是我写的,不成功呀。
void psorder(btree *b)
{
btree *p,*stack[MAX];
int top=-1;
top++;
stack[top]=b;
int flag=0;
while(top>=0)
{
p=stack[top];
while(p->left!=NULL && !flag)
{
top++;
p=p->left;
stack[top]=p;
}
if(flag==0)
{
p=stack[top];
top--;
cout<<p->data<<" ";
}
if(p->right!=NULL)
{
top++;
p=p->right;
stack[top]=p;
flag=0;
}
else
{
p=stack[top];
top--;
cout<<p->data<<" ";
flag=1;
}
}
}
/*
//写不出来了。
void psorder_b(btree *b)
{
btree *p,*stack[MAX];
int top=-1;
int flag;
if(b!=NULL)
{
top++;
stack[top]=b;
p=stack[top];
do{
while(b)
{
top++;
stack[top]=b;
b=b->left;
}
p=NULL;
flag=1;
while(top!=-1 && flag)
{
p=stack[top];
top--;
cout<<p->data<<" ";
if(b->right==p)
{
top++;
p=p->right;
stack[top]=p;
flag=1;
}
else
{
p=p->right;
flag=0;
}
}
}while(top!=-1);
}
}
*/
void psorder_b(btree *b)
{
btree *p,*stack[MAX];
int top=-1;
int flag;
if(b!=NULL)
{
do{
while(b)
{
top++;
stack[top]=b;
b=b->left;
}
p=NULL;
flag=1;
while(top!=-1 && flag)
{
b=stack[top];
if(b->right==p)//p为空或已被访问过。
{
cout<<b->data<<" ";
top--;
p=b;
}
else
{
b=b->right;
flag=0;
}
}
}while(top!=-1);
}
}
void psorder1(btree *b)
{
btree *p,*stack[MAX];
int top=-1;
int flag;
if(b!=NULL)
{
do
{
while(b)
{
top++;
stack[top]=b;
b=b->left;
}
p=NULL;
flag=1;
while(top!=-1 && flag)
{
b=stack[top];
if(b->right==p)
{
cout<<b->data<<" ";
top--;
flag=1;
p=b;
}
else
{
b=b->right;
flag=0;
}
}
}while(top!=-1);
}
}
int main()
{
btree *b;
createTree(b,"a(b(c),d(e(,f),g))");
printTree(b);
cout<<endl;
inorder_recursion(b);
cout<<endl;
firsOrderS(b);
cout<<endl;
firsOrder(b);
cout<<endl;
inorder(b);
cout<<endl;
psorder_recursion(b);
cout<<endl;
// psorder(b);
cout<<endl;
psorder_b(b);
cout<<endl;
psorder1(b);
cout<<endl;
}