Chinaunix首页 | 论坛 | 博客
  • 博客访问: 587062
  • 博文数量: 68
  • 博客积分: 5070
  • 博客等级: 大校
  • 技术积分: 1312
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-11 14:20
文章分类

全部博文(68)

文章存档

2011年(3)

2010年(30)

2009年(17)

2008年(18)

我的朋友

分类: C/C++

2008-04-07 17:44:26

#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);
}
阅读(1084) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-04-29 22:27:57

一个错误的程序,还贴出来干什么?