#include
#include
#include
typedef struct node
{
char data;
struct node *lchild,*rchild;
}BiNode,*BiTree;
void CreateTree(BiTree *root,const char source[],int start,int end);
void print(BiTree root);
main()
{
BiTree root=NULL;
char buffer[] = "((a+b)+c*(d+e)+f)*(g+h) ";
CreateTree(&root,buffer,0,strlen(buffer));
print(root);
system( "pause ");
}
void print(BiTree root)
{
if(NULL != root)
{
print(root-> lchild);
printf( "%4c ",root-> data);
print(root-> rchild);
}
}
void CreateTree(BiTree *root,const char s[],int start,int end)
/*½¨Á¢±í´ïʽµÄ¶þ²æÊ÷ */
{
int k = 0;
int i = start;
if(start > end)
{
*root = NULL;
return;
}
while(s[i] != 0)
{
if( '( ' == s[i])
k++;
if( ') ' == s[i])
k--;
if(0 == k)
break;
}
/*create root node */
*root = (BiNode *)malloc(sizeof(BiNode));
if(NULL == *root)
return;
(*root)-> data = s[i];
CreateTree(&((*root)-> lchild),s,start+1,i-2);
CreateTree(&((*root)-> rchild),s,i+2,end-1);
}
阅读(1108) | 评论(1) | 转发(0) |