#include<stdio.h>
#include<string.h>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
//int count=0;
typedef struct _Node
{
char data;
struct _Node *lchild;
struct _Node *rchild;
}Node,*Tree;
Node *CreateTree(_Node *Tree)
{
char str1;
Node *T;
str1=getchar();
if(str1==' ')
return NULL;
else
{
T=(Node *)malloc(sizeof(Node));
if(!T)
exit(0);
T->data=str1; //生成根节点
T->lchild=CreateTree(T->lchild);//生成左子树
T->rchild=CreateTree(T->rchild);//生成右子树
}
return T;
}
void frontvisit(_Node *T)//递归实现前序遍历
{
if(T!=NULL)
{
printf("%c ",T->data);
frontvisit(T->lchild);
frontvisit(T->rchild);
}
}
void midvisit(_Node *T)//递归实现中序遍历
{
if(T!=NULL)
{
midvisit(T->lchild);
printf("%c ",T->data);
midvisit(T->rchild);
}
}
void lastvisit(_Node *T)//递归实现后序遍历
{
if(T!=NULL)
{
lastvisit(T->lchild);
lastvisit(T->rchild);
printf("%c ",T->data);
}
}
int hightree(_Node *T)//求出树的高度
{
int h,l,r;
if(!T)
{
return 0;
}
l=hightree(T->lchild);
r=hightree(T->rchild);
h=(l>r?l:r)+1;
return h;
}
int c=0;//必须要定为全局变量,
int countleaf0(_Node *T)//求出树度为0节点的个数
{
//int count=0;
if(T)
{
if((!T->lchild)&&(!T->rchild))
c++;
c=countleaf0(T->lchild);
c=countleaf0(T->rchild);
}
return c;
}
int c1=0;
int countleaf1(_Node *T)//求出树度为1节点的个数
{
//int count=0;
if(T)
{
if((T->lchild)&&(!T->rchild)||(!T->lchild)&&(T->rchild))
c1++;
c1=countleaf1(T->lchild);
c1=countleaf1(T->rchild);
}
return c1;
}
int c2=0;
int countleaf2(_Node *T)//求出树度为2节点的个数
{
//int count=0;
if(T)
{
if((T->lchild)&&(T->rchild))
c2++;
c2=countleaf2(T->lchild);
c2=countleaf2(T->rchild);
}
return c2;
}
typedef struct
{
Tree *base;
Tree *top;
int stacksize;
}SqStack;
int InitStack(SqStack &S)//创建一个栈
{
S.base=(Tree *)malloc(sizeof(Tree));
if(!S.base)
exit(0);
S.top=S.base;
S.stacksize=100;
return 1;
}
int Push(SqStack &S,Tree e)//压入栈
{
if(!S.base)
return 0;
*S.top=e;
S.top++;
return 1;
}
int Pop(SqStack &S,Tree &e)//出栈
{
if(S.base==S.top)
return 0;
S.top--;
e=*S.top;
return 1;
}
int StackEmpty(SqStack S)//判断栈是否为空
{
if(S.top==S.base)
return 1;
else
return 0;
}
int midvisit1(_Node *T)//非递归实现中序遍历
{
SqStack S;
InitStack(S);
Tree p;
p=T;
while(p||!StackEmpty(S))
{
if(p)
{
Push(S,p);
p=p->lchild;
}
else
{
Pop(S,p);
printf("%c ",p->data);
p=p->rchild;
}
}
return 1;
}
int frontvisit1(_Node *T)//非递归实现前序遍历
{
SqStack s;
Tree P=T;
InitStack(s);
while(P!=NULL||!StackEmpty(s))
{
if(P!=NULL)
{
printf("%c ",P->data);
Push(s,P);
P=P->lchild;
}
else
{
Pop(s,P);
P=P->rchild;
}
}
return 1;
}
int lastvisit1(_Node *T)//非递归实现后序遍历
{
SqStack s,tag;
Tree f,m,n,P=T;
m=(Node *)malloc(sizeof(Node));
m->data=1;
m->lchild=NULL;
m->rchild=NULL;
n=(Node *)malloc(sizeof(Node));
n->data=2;
n->lchild=NULL;
n->rchild=NULL;
InitStack(s);
InitStack(tag);
while(P||!StackEmpty(s))
{
if(P)
{
Push(s,P);
Push(tag,m);
P=P->lchild;
}
else
{
Pop(s,P);
Pop(tag,f);
if(f==m)
{
Push(s,P);
Push(tag,n);
P=P->rchild;
}
else
{
printf("%c ",P->data);
P=NULL;
}
}
}
return 1;
}
int main()
{
_Node *T;
char str1[101],str2[101];
printf("请按先序序列把一棵树输入: \n");
T=CreateTree(T);
// getchar();
printf("递归实现这棵树的前序序列遍历: \n");
frontvisit(T);
printf("\n");
printf("非递归实现前序序列遍历\n");
frontvisit1(T);
printf("\n");
// printf("\n");
printf("递归实现这棵树的中序序列遍历: \n");
midvisit(T);
printf("\n");
printf("非递归实现中序序列遍历\n");
midvisit1(T);
printf("\n");
printf("递归实现这棵树的后序序列为: \n");
lastvisit(T);
printf("\n");
printf("非递归实现后序序列遍历: \n");
lastvisit1(T);
printf("\n");
printf("则这棵树度为0的节点数为: ");
printf("%d\n",countleaf0(T));
printf("则这棵树度为1的节点数为: ");
printf("%d\n",countleaf1(T));
printf("则这棵树度为2的节点数为: ");
printf("%d\n",countleaf2(T));
printf("则这棵树高度为: ");
printf("%d\n",hightree(T));
}