#include<stdio.h>
#include<string.h>
#include<malloc.h>//in order to include the malloc function
#include <stdlib.h>//in order to include the exit function
#define OVERFLOW 1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
//-----------------Binary Tree Node-----------------------
typedef struct BiTNode{
char name[20];
struct BiTNode *lchild, *rchild;
}BiTNode,*BiTree;
//-----------------Binary Tree Node------------------------------------------
//-----------------SqStack Node----------------------------------------------
typedef struct{
BiTree *base;
BiTree *top;
int stacksize;
}SqStack;
//because the element in the stack is pointer,so we use the pointer of pointer
//-----------------SqStack Node----------------------------------------------
//------------------quote the functions--------------------------------------
SqStack InitStack(void);
BiTree GetTop(SqStack *);
int Push(SqStack *,BiTree );
int StackEmpty(SqStack *);
BiTree Pop(SqStack *);
int InOrder(BiTree,SqStack *);
//------------------quote the functions--------------------------------------
//-------------------------MAIN FUNCTION-------------------------------------
int main(void){
// BiTNode a1,b1,c1,d1,e1,f1,g1;
BiTree head,a,b,c,d,e,f,g;//a is the root
// a=&a1;b=&b1;c=&c1;d=&d1;e=&e1;f=&f1;g=&g1;
// head->lchild=NULL;head->rchild=a;//the head pointer point to the root
head=(BiTree)malloc(sizeof(BiTNode));
a=(BiTree)malloc(sizeof(BiTNode));
b=(BiTree)malloc(sizeof(BiTNode));
c=(BiTree)malloc(sizeof(BiTNode));
d=(BiTree)malloc(sizeof(BiTNode));
e=(BiTree)malloc(sizeof(BiTNode));
f=(BiTree)malloc(sizeof(BiTNode));
g=(BiTree)malloc(sizeof(BiTNode));
strcpy(a->name,"Aname");strcpy(b->name,"Bname");strcpy(c->name,"Cname");
strcpy(d->name,"Dname");strcpy(e->name,"Ename");strcpy(f->name,"Fname");
strcpy(g->name,"Gname");
a->lchild=b;a->rchild=NULL;
b->lchild=c;b->rchild=d;
c->lchild=NULL;c->rchild=NULL;
d->lchild=e;d->rchild=f;
e->lchild=NULL;e->rchild=g;
f->lchild=NULL;f->rchild=NULL;
g->lchild=NULL;g->rchild=NULL;
SqStack s,*sp;
s=InitStack();
sp=&s;
InOrder(a,sp);
return 0;
}
//-------------------------MAIN FUNCTION-------------------------------------
//---------------------------InOrderTraverse----------------------------------
int InOrder(BiTree T,SqStack *s){
Push(s,T);//put the pointer of root in the stack
BiTree p;
while(!StackEmpty(s)){
while(p=GetTop(s)) Push(s,p->lchild); //GetTop function returns p;go to the end in the lest diredtion
p=Pop(s);//in the last step above, a null pointer has been put in
if(!StackEmpty(s)){
p=Pop(s);
if(!printf("%s\n",p->name)) return 1;
Push(s,p->rchild);
}//if
}//while
return 0;
}
//---------------------------InOrderTraverse----------------------------------
//--------------------------some execuation about the stack--------------------
SqStack InitStack(void){
SqStack s;
s.base=(BiTree *)malloc(STACK_INIT_SIZE*sizeof(BiTree));
if(!s.base) exit(OVERFLOW);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return s;
}
BiTree GetTop(SqStack *s){
BiTree e;
// if (s.top==s.base) return 1;
e=*(s->top-1);
return e;
}
int Push(SqStack *s,BiTree e){
if(s->top-s->base>=s->stacksize){
s->base=(BiTree *)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(BiTree));
if(!s->base) exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++ =e;
return 0;
}
int StackEmpty(SqStack *s){
if(s->top==s->base) return 1;
else return 0;
}
BiTree Pop(SqStack *s){
BiTree e;;
// if(s.top=s.base) return 1;
e=*--(s->top);
return e;
}
//--------------------------some execuation about the stack--------------------
#include<string.h>
#include<malloc.h>//in order to include the malloc function
#include <stdlib.h>//in order to include the exit function
#define OVERFLOW 1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
//-----------------Binary Tree Node-----------------------
typedef struct BiTNode{
char name[20];
struct BiTNode *lchild, *rchild;
}BiTNode,*BiTree;
//-----------------Binary Tree Node------------------------------------------
//-----------------SqStack Node----------------------------------------------
typedef struct{
BiTree *base;
BiTree *top;
int stacksize;
}SqStack;
//because the element in the stack is pointer,so we use the pointer of pointer
//-----------------SqStack Node----------------------------------------------
//------------------quote the functions--------------------------------------
SqStack InitStack(void);
BiTree GetTop(SqStack *);
int Push(SqStack *,BiTree );
int StackEmpty(SqStack *);
BiTree Pop(SqStack *);
int InOrder(BiTree,SqStack *);
//------------------quote the functions--------------------------------------
//-------------------------MAIN FUNCTION-------------------------------------
int main(void){
// BiTNode a1,b1,c1,d1,e1,f1,g1;
BiTree head,a,b,c,d,e,f,g;//a is the root
// a=&a1;b=&b1;c=&c1;d=&d1;e=&e1;f=&f1;g=&g1;
// head->lchild=NULL;head->rchild=a;//the head pointer point to the root
head=(BiTree)malloc(sizeof(BiTNode));
a=(BiTree)malloc(sizeof(BiTNode));
b=(BiTree)malloc(sizeof(BiTNode));
c=(BiTree)malloc(sizeof(BiTNode));
d=(BiTree)malloc(sizeof(BiTNode));
e=(BiTree)malloc(sizeof(BiTNode));
f=(BiTree)malloc(sizeof(BiTNode));
g=(BiTree)malloc(sizeof(BiTNode));
strcpy(a->name,"Aname");strcpy(b->name,"Bname");strcpy(c->name,"Cname");
strcpy(d->name,"Dname");strcpy(e->name,"Ename");strcpy(f->name,"Fname");
strcpy(g->name,"Gname");
a->lchild=b;a->rchild=NULL;
b->lchild=c;b->rchild=d;
c->lchild=NULL;c->rchild=NULL;
d->lchild=e;d->rchild=f;
e->lchild=NULL;e->rchild=g;
f->lchild=NULL;f->rchild=NULL;
g->lchild=NULL;g->rchild=NULL;
SqStack s,*sp;
s=InitStack();
sp=&s;
InOrder(a,sp);
return 0;
}
//-------------------------MAIN FUNCTION-------------------------------------
//---------------------------InOrderTraverse----------------------------------
int InOrder(BiTree T,SqStack *s){
Push(s,T);//put the pointer of root in the stack
BiTree p;
while(!StackEmpty(s)){
while(p=GetTop(s)) Push(s,p->lchild); //GetTop function returns p;go to the end in the lest diredtion
p=Pop(s);//in the last step above, a null pointer has been put in
if(!StackEmpty(s)){
p=Pop(s);
if(!printf("%s\n",p->name)) return 1;
Push(s,p->rchild);
}//if
}//while
return 0;
}
//---------------------------InOrderTraverse----------------------------------
//--------------------------some execuation about the stack--------------------
SqStack InitStack(void){
SqStack s;
s.base=(BiTree *)malloc(STACK_INIT_SIZE*sizeof(BiTree));
if(!s.base) exit(OVERFLOW);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return s;
}
BiTree GetTop(SqStack *s){
BiTree e;
// if (s.top==s.base) return 1;
e=*(s->top-1);
return e;
}
int Push(SqStack *s,BiTree e){
if(s->top-s->base>=s->stacksize){
s->base=(BiTree *)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(BiTree));
if(!s->base) exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++ =e;
return 0;
}
int StackEmpty(SqStack *s){
if(s->top==s->base) return 1;
else return 0;
}
BiTree Pop(SqStack *s){
BiTree e;;
// if(s.top=s.base) return 1;
e=*--(s->top);
return e;
}
//--------------------------some execuation about the stack--------------------