#include<iostream.h>
#define OK 1;
#define ERROR 0;
#define MAXSIZE 100;
typedef struct Node{
int data;
struct Node *lchild;
struct Node *rchild;
}Node,*pNode;
typedef struct SqStack{
pNode stack[MAXSIZE];
int top;
}SqStack;
//先序遍历
void preOrderUnrec(pNode T,SqStack *s){
push(T,s);
pNode p=T;
while(s->top!=0&&p!=NULL){
while(s->stack[s->top]!=NULL){
p=s->stack[s->top]; //得到当前结点的指针
printf(p->data); //输出树节点的值
push(p->lchild,s); //将当前结点的左孩子压入栈中
}
pop(s); //第一次出栈,出空元素,指针下移
p=pop(s); //第二次出栈,出当前结点,指针下移
push(p->rchild,s); //压入当前结点的右孩子,指针指向右孩子
p=s->stack[s->top];
}
}
//中序遍历
void preOrderTracerse(pNode T,SqStack *s){
push(T,s);
pNode p=T;
while(s->top!=0&&p!=NULL){
while(s->stack[s->top]!=NULL){
p=s->stack[s->top];
push(p->lchild,s); //将当前结点的左孩子压入栈中
}
pop(s); //第一次出栈,出空元素,指针下移
p=pop(s); //第二次出栈,出当前结点,指针下移
printf(p->data);
push(p->rchild,s); //压入当前结点的右孩子,指针指向右孩子
p=s->stack[s->top];
}
}
//create a tree with recuresive method
void createTree(pNode *T){
int ch;
cin<<ch;
if(ch==0)
{
T=NULL;
}
else
{
T=(pNode)malloc(sizeof(Node));
T->data=ch;
createTree(&((*T)->lchild));
createTree(&((*T)->lchild));
}
}
//pop a Node
pNode pop(SqStack *s){
pNode* p;
if(s->top==0)
{
printf("%s","stack is empty");
return NULL;
}
else
{
s->top--;
p=s->stack[s->top];
return p;
}
}
//push a Node
void push(SqStack *s,pNode T){
if(s->top==MAXSIZE)
{
printf("%s","stack output");
}
else{
s->top++;
s->stack[s->top]=T;
}
}
#define OK 1;
#define ERROR 0;
#define MAXSIZE 100;
typedef struct Node{
int data;
struct Node *lchild;
struct Node *rchild;
}Node,*pNode;
typedef struct SqStack{
pNode stack[MAXSIZE];
int top;
}SqStack;
//先序遍历
void preOrderUnrec(pNode T,SqStack *s){
push(T,s);
pNode p=T;
while(s->top!=0&&p!=NULL){
while(s->stack[s->top]!=NULL){
p=s->stack[s->top]; //得到当前结点的指针
printf(p->data); //输出树节点的值
push(p->lchild,s); //将当前结点的左孩子压入栈中
}
pop(s); //第一次出栈,出空元素,指针下移
p=pop(s); //第二次出栈,出当前结点,指针下移
push(p->rchild,s); //压入当前结点的右孩子,指针指向右孩子
p=s->stack[s->top];
}
}
//中序遍历
void preOrderTracerse(pNode T,SqStack *s){
push(T,s);
pNode p=T;
while(s->top!=0&&p!=NULL){
while(s->stack[s->top]!=NULL){
p=s->stack[s->top];
push(p->lchild,s); //将当前结点的左孩子压入栈中
}
pop(s); //第一次出栈,出空元素,指针下移
p=pop(s); //第二次出栈,出当前结点,指针下移
printf(p->data);
push(p->rchild,s); //压入当前结点的右孩子,指针指向右孩子
p=s->stack[s->top];
}
}
//create a tree with recuresive method
void createTree(pNode *T){
int ch;
cin<<ch;
if(ch==0)
{
T=NULL;
}
else
{
T=(pNode)malloc(sizeof(Node));
T->data=ch;
createTree(&((*T)->lchild));
createTree(&((*T)->lchild));
}
}
//pop a Node
pNode pop(SqStack *s){
pNode* p;
if(s->top==0)
{
printf("%s","stack is empty");
return NULL;
}
else
{
s->top--;
p=s->stack[s->top];
return p;
}
}
//push a Node
void push(SqStack *s,pNode T){
if(s->top==MAXSIZE)
{
printf("%s","stack output");
}
else{
s->top++;
s->stack[s->top]=T;
}
}