#include<stdio.h>
#include<malloc.h>
#define max 20
typedef struct tnode
{ char data;
struct tnode *lchild,*rchild;
}BTNode;
void createbtnode(BTNode *&bt,char *&str)
{ int k,j=0,top=-1;
char ch;
BTNode *p=NULL,*st[max];
bt=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(bt==NULL)
bt=p;
else
{
switch(k)
{
case 1: st[top]->lchild=p;break;
case 2: st[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
char biggestelem(BTNode *bt)
{
char max1,max2;
if(bt->lchild==NULL && bt->rchild==NULL)
return bt->data;
else
{
if(bt->data>max1)
max1=bt->data;
max2=biggestelem(bt->lchild);
if(max2>max1)
max1=max2;
max2=biggestelem(bt->rchild);
if(max2>max1)
max1=max2;
return max1;
}
}
void main()
{
BTNode *bt;
char *str="a(b(e(g),f),c(,I))";
char x;
createbtnode(bt,str);
x=biggestelem(bt);
printf("%c",x);
}
求最大值