博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

取个标题想半天

没有什么东西是可以不通过努力奋斗来获得
luojingqing.cublog.cn


表达式转二叉树
#include   <string.h>
#include   <stdio.h>
#include   <stdlib.h>

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)
/*&frac12;¨&Aacute;&cent;±í&acute;&iuml;&Ecirc;&frac12;&micro;&Auml;&para;&thorn;&sup2;&aelig;&Ecirc;÷         */
{
        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);
}

发表于: 2008-04-07 ,修改于: 2008-04-07 17:44,已浏览193次,有评论1条 推荐 投诉


网友评论
内容:
一个错误的程序,还贴出来干什么?
本站网友评论于:2008-04-29 22:27:57 (218.75.44.★)

发表评论