#include<stdlib.h>
#define STACKINITSIZE 100 //栈大小
#define STACKINCREASESIZE 20 //栈单次扩容量
//节点声明,数据域、左孩子指针、右孩子指针
typedef struct node{
char data;
struct node *lchild,*rchild;
}BiTNode,*BiTree;
//用于非递归遍历的栈存储的声明,栈底指针、栈顶指针、栈大小
typedef struct stack
{
BiTree * base,* top;
int stacksize;
}Sqstack;
/*****************栈的操作定义********************/
//初始化栈
void InitStack( Sqstack &s )
{
s.base = (BiTree *)malloc(STACKINITSIZE*sizeof(BiTree));
if ( !s.base )
{
printf("InitStack内存分配出错\n");
exit(0);
}
s.top = s.base;
s.stacksize = STACKINITSIZE;
}
//元素入栈
void Push( Sqstack &s, BiTree e )
{
if ( s.top - s.base >= s.stacksize )
{
s.base = (BiTree *)realloc(s.base,(s.stacksize+STACKINCREASESIZE)*sizeof(BiTree)); //动态内存分配,栈大小不足时,自动申请扩大空间。
if ( !s.base )
{
printf("Push内存分配出错\n");
exit(0);
}
s.top = s.base + s.stacksize;
s.stacksize += STACKINCREASESIZE;
}
*s.top++ = e; //元素入栈
}
//获得栈顶元素
void GetTop( Sqstack s, BiTree &e )
{
e = *(s.top - 1);
}
//弹出栈顶元素
void Pop( Sqstack &s, BiTree &e )
{
if ( s.top == s.base )
{
printf("栈为空\n");
return ;
}
e = *(--s.top);
}
//判断栈是否为空,为空返回1,否则返回0
int StackEmpty( Sqstack s )
{
if ( s.top == s.base )
return 1;
return 0;
}
/*****************栈的操作定义********************/
/*建立二叉树(先序)*/
BiTree CreateBiTree(){
char ch;
BiTree T;
ch=getchar();
if(ch=='.')
T=NULL;
else{
T = (BiTree)malloc(sizeof(BiTNode));
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T;//返回根节点
}
/*先序遍历二叉树*/
void PreOrder(BiTree root){
if(root){
printf("%c ",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
/*中序遍历二叉树*/
void InOrder(BiTree root){
if(root){
InOrder(root->lchild);
printf("%c ",root->data);
InOrder(root->rchild);
}
}
/*后序遍历二叉树*/
void PostOrder(BiTree root){
if(root){
PostOrder(root->lchild);
PostOrder(root->rchild);
printf("%c ",root->data);
}
}
/*按层次遍历二叉树*/
void LevelOrder(BiTree root){
int front,rear;
BiTree p;
BiTree *Queen;
Queen=(BiTree *)malloc(sizeof(BiTree *)*1000);
front=rear=0;
Queen[++rear]=root;
while(front != rear)
{
p=Queen[++front];
printf("%c ",p->data);
if(p->lchild) Queen[++rear]=p->lchild;
if(p->rchild) Queen[++rear]=p->rchild;
}
free(Queen); //释放队列空间
}
/*非递归中序遍历二叉树*/
void InOrder1(BiTree root)
{
BiTree p = root;
Sqstack s;
InitStack(s);
while ( p || !StackEmpty(s) )
{
if ( p )
{
Push(s,p);
p = p->lchild;
}
else
{
Pop(s,p);
printf("%c ",p->data);
p = p->rchild;
}
}
}
/*非递归后序遍历二叉树*/
void PostOrder1(BiTree root){
BiTree p=root,q=NULL;
Sqstack s;
InitStack(s);
while(p!=NULL|| (!StackEmpty(s)) )
{
while(p!=NULL)
{
Push(s,p);
p = p->lchild;
}
if(!StackEmpty(s))
{
GetTop(s,p);
if((p->rchild==NULL)||(p->rchild==q))
{
printf("%c ",p->data);
Pop(s,p);
q=p;
p=NULL;
}
else
p=p->rchild;
}
}
}
/*逆时针旋转90度打印二叉树*/
void PrintTree(BiTree bt,int nLayer){
int i;
if(bt==NULL) return;
PrintTree(bt->rchild,nLayer+1);
for(i=0;i<nLayer;i++)
printf(" ");
printf("%c\n",bt->data);
PrintTree(bt->lchild,nLayer+1);
}
int main(void){
BiTree root;
printf("请前序创建二叉树:(若孩子节点为NULL,则以字符“.”代替)\n");
root = CreateBiTree();//建立二叉树
printf("\n您输入的二叉树为:(逆时针旋转90度)\n\n");
PrintTree(root,0);
printf("\n递归前序遍历: ");
PreOrder(root);
printf("\n递归中序遍历: ");
InOrder(root);
printf("\n递归后序遍历: ");
PostOrder(root);
printf("\n\n按照层次遍历: ");
LevelOrder(root);
printf("\n\n非递归中序遍历: ");
InOrder1(root);
printf("\n非递归后序遍历: ");
PostOrder1(root);
printf("\n\n");
return 0;
}