#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct BiTNode
{
char data;
BiTNode *lchild,*rchild ;
}BiTNode ,*BiTree; //node and node pointer
struct stacktree
{
BiTree *base,*top;
int stacksize;
};
void initstack(stacktree &S) //initial stack 没有&时出现了单项传值的情况,s为另外开辟的空间
{
S.base=(BiTree *)malloc(sizeof(BiTree)*24);
if(!S.base) exit(0);
S.top=S.base;
S.stacksize=24;
}
BiTree gettop(stacktree &S)
{
return *(S.top-1);
}
void push(stacktree &S,BiTree &T1) //does extend size
{
*S.top++=T1; //*改变了top所指向的单元格,随后top++
}
BiTree pop(stacktree &S)
{
return *--S.top;
}
void preordertraverse1(BiTree &T)
{
cout<<T->data<<' '; //通过输出先序以及中序的地址,画出树、判断出不断输出同一个地址,但又没有出现任何把l放在r的现象,最后发现是误把r写出了l
if(T->lchild) preordertraverse1(T->lchild);
if(T->rchild) preordertraverse1(T->rchild);
}
void inordertreverse1(BiTree &T)
{
if(T->lchild) inordertreverse1(T->lchild);
cout<<T->data<<' ';
if(T->rchild) inordertreverse1(T->rchild);
}
void postordertraverse1(BiTree &T)
{
if(T->lchild) postordertraverse1(T->lchild);
if(T->rchild) postordertraverse1(T->rchild);
cout<<T->data<<' ';
}
void preordertraverse2(BiTree &T)
{
stacktree S;
initstack(S);
BiTree T1;
if(T)
{
T1=T;
while(S.base!=S.top||T1)
{
if(T1)
{
push(S,T1);
cout<<T1->data<<' ';
T1=T1->lchild;
}
else
{
T1=pop(S);
T1=T1->rchild;
}
}
}
else
{
cout<<"error"<<endl;
exit(0);
}
}
void inordertreverse2(BiTree &T)
{
stacktree S;
initstack(S);
BiTree T1;
if(T)
{
T1=T;
while(T||S.base!=S.top) //the next annotation's solution is change t to t1 because the while aways true and at end the pop has no BitNode to delete
{
if(T1)
{
push(S,T1);
T1=T1->lchild;
}
else
{
if(S.base!=S.top){ //setting if and else break because arise when t1==null and stack if empty ,the procedure still operation
T1=pop(S);
cout<<T1->data<<' ';
T1=T1->rchild;}
else break;
}
}
}
else
{
cout<<"error"<<endl;
exit(0);
}
}
void postordertraverse2(BiTree &T)
{
stacktree S1,S2;
initstack(S1);
initstack(S2);
BiTree T1;
if(T)
{
T1=T;
while(S1.base!=S1.top||T1)
{
if(T1)
{
push(S1,T1);
push(S2,T1);
T1=T1->rchild;
}
else
{
T1=pop(S1);
T1=T1->lchild;
}
}
}
else
{
cout<<"error"<<endl;
exit(0);
}
while (S2.base!=S2.top) //S2.base!=S2.top arise unknown problem
cout<<(*--S2.top)->data<<' ';
}
void createbitree_129(BiTree &T) //preorder traversal to create tree
{
int i=1;
BiTree T1;
char a[]="-+a *b -c d /e f ";
cout<<a;
T1=T =new BiTNode;// out site cout<<T1<<endl;
T1->data=a[0];
T1->lchild=NULL;
T1->rchild=NULL;
stacktree S;
initstack(S);
push(S,T1);
while(i!=23)
{
if(a[i]!=' ')
{ T1->lchild=new BiTNode;// test push cout<<i<<a[i]<<endl;
T1=T1->lchild;// out sit cout<<T1<<endl;
T1->data=a[i];
T1->rchild=NULL;
T1->lchild=NULL;// test cout<<T1->data<<endl;
push(S,T1);
i++;
}
else
{i++;//cout<<i<<a[i]<<endl;
if(i==23)break;
T1=pop(S); // cout<<T1<<endl; 通过对比地址分析传值没错,且多pop了一次,把break上移
//cout<< i<<T1->data<<endl;
// if(i==23)break; //当到了22还没停止则出现溢栈情况,具体不明待测
if(a[i]!=' ')
{ T1->rchild=new BiTNode;
T1=T1->rchild;// out sitcout<<T1<<endl;
T1->data=a[i];
T1->lchild=NULL;
T1->rchild=NULL; // test push cout<<i<<T1->data<<endl;
push(S,T1);
i++; // forget change i so that make twice push
}
}//cout<<(*S.base)->data<<endl; //'->' > '*'
//树未建立完善 preordertraverse1(T);cout<<endl;//通过观察树的图发现判断失误
}
}
int main()
{
BiTree T;
createbitree_129(T);
cout<<endl;
preordertraverse1(T);
cout<<endl;
inordertreverse1(T);
cout<<endl;
postordertraverse1(T);
cout<<endl;
preordertraverse2(T);
cout<<endl;
inordertreverse2(T);
cout<<endl;
postordertraverse2(T);
return 0;
}