#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include<algorithm>
#define MaxSize 100
using namespace std;
typedef struct node
{
char data;
struct node * lchild;
struct node * rchild;
}BTNode;
typedef struct
{
BTNode *data[MaxSize];
int front,rear;
}SqQueue;
typedef struct
{
BTNode *data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void InitQueue(SqQueue *&q)
{ q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,BTNode * e)
{
if (s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool QueueEmpty(SqQueue *q)
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,BTNode * e)
{ if ((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,BTNode *&e)
{ if (q->front==q->rear)
return false;
q->front=(q->front+1)%MaxSize;
e=q->data[q->front];
return true;
}
bool Pop(SqStack *&s,BTNode * &e)
{
if (s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,BTNode *&e)
{
if (s->top==-1)
return false;
e=s->data[s->top];
return true;
}
void CreateBTree(BTNode *&b,char *str)
{
BTNode *St[MaxSize],*p;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case'(':
top++;
St[top]=p;
k=1;
break;
case')':
top--;
break;
case',':
k=2;
break;
default:
p=(BTNode *)malloc(sizeof(BTNode));
p->data = ch;
p->lchild = p->rchild = NULL;
if(b==NULL)
{
b=p;
}
else
{
switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
int InOrderFX(BTNode *b)
{
BTNode *p;
int count=0;
SqStack *st;
InitStack(st);
p=b;
while(!StackEmpty(st) || p!=NULL)
{
while(p!=NULL)
{
Push(st,p);
p=p->lchild;
}
if(!StackEmpty(st))
{
Pop(st,p);
count++;
p=p->rchild;
}
}
DestroyStack(st);
return count;
}
int constcount1=0;
int DispLeaf(BTNode *b)
{
if(b!=NULL)
{
if(b->lchild==NULL&&b->rchild==NULL)
{
constcount1++;
}
DispLeaf(b->lchild);
DispLeaf(b->rchild);
}
}
int L=1;
void findlevel(BTNode *p,char x)
{
if(p!=NULL)
{
if(p->data==x) printf("层号:%d\n",L);
++L;
findlevel(p->lchild,x);
findlevel(p->rchild,x);
--L;
}
}
int deepth = 0;
int width[10] = {0};
void TreeWidth(BTNode *t){
if (t == NULL) {
return;
}
if (deepth == 0) {
width[0] = 1;
}
if (t->lchild != NULL) {
width[deepth+1] += 1;
deepth += 1;
TreeWidth(t->lchild);
}
if (t->rchild != NULL) {
width[deepth+1] += 1;
deepth+=1;
TreeWidth(t->rchild);
}
deepth-=1;
}
void DestroyBTree(BTNode *& b)
{
if(b!=NULL)
{
DestroyBTree(b->lchild);
DestroyBTree(b->rchild);
free(b);
}
}
int main()
{
BTNode *b;
char x;
int h;
CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
printf("结点个数为%d\n",InOrderFX(b));
DispLeaf(b);
scanf("%c",&x);
findlevel(b,x);
printf("叶子结点个数是%d\n",constcount1);
TreeWidth(b);
int max;
for(int i=0;i<9;i++)
{
if(width[i]>max)
{
max=width[i];
}
}
printf("最大宽度是%d\n",max);
DestroyBTree(b);
return 0;
}