• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
千里之行,始于足下。
探索程序的世界
博客园    首页    新随笔    联系   管理    订阅  订阅
递归算法(二)——前缀转后缀
介绍前缀转后缀的两种方法。

源码:pretopost.cpp

#include "stdafx.h"
#include <stdio.h>
#include <stack>

/************************************************************************/
/* 前缀转后缀                                                           */
/************************************************************************/

bool is_digitoralpha(char c)
{
    return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

//************************************
// Method:    递归转换方法
// FullName:  pretopost1
// Access:    public 
// Returns:   void
// Qualifier:
// Parameter: char * str
//************************************
char* pretopost1(char *str)
{
    char op = *str++;//第一位是父结点
    if (is_digitoralpha(*str))
        printf("%c", *str++);//左孩子是操作数,直接打印
    else
        str = pretopost1(str);//递归处理左子树
    if (is_digitoralpha(*str))
        printf("%c", *str++);//右孩子是操作数,直接打印
    else
        str = pretopost1(str);//递归处理右子树
    printf("%c", op);//最后打印父结点
    return str;
}

struct node
{
    char data;
    node *left, *right;
};

void print_post(node *pnode)
{
    if (pnode)
    {
        if (pnode->left)
        {
            print_post(pnode->left);
        }
        if (pnode->right)
        {
            print_post(pnode->right);
        }
        printf("%c", pnode->data);
    }
}

void delete_tree(node *pnode)
{
    if (pnode)
    {
        if (pnode->left)
        {
            delete_tree(pnode->left);
        }
        if (pnode->right)
        {
            delete_tree(pnode->right);
        }
        delete pnode;
    }
}

//************************************
// Method:    构造表达式树方法
// FullName:  pretopost2
// Access:    public 
// Returns:   void
// Qualifier:
// Parameter: char * str
//************************************
void pretopost2(char *str)
{
    std::stack<node*> stk;//存放树结点的栈
    node *top, *root;
    top = root = NULL;
    while (*str)
    {
        bool num;//是否是操作数
        char c;
        c = *str;
        if (!stk.empty())
        {
            top = stk.top();
            if (top->left && top->right)//如果当前结点的左右子树均满,则弹出此结点
            {
                stk.pop();
                continue;
            }
        }
        str++;
        num = is_digitoralpha(c);
        if (num || top)
        {
            node *newnode;
            if (!top->left || !top->right)//建立新的结点,将其安插在top的孩子上
            {
                newnode = new node();
                newnode->data = c;
                newnode->left = NULL;
                newnode->right = NULL;
                if (!top->left)
                    top->left = newnode;
                else
                    top->right = newnode;
            }            
            if (!num)//如果是操作符,则变更当前top结点,使其指向新结点(操作符)
            {
                stk.push(newnode);
            }
        }
        else
        {
            top = new node();//如果top是空,意味着栈为空,则初始化
            top->data = c;
            top->left = NULL;
            top->right = NULL;
            root = top;//放置根结点
            stk.push(top);
        }
    }
    print_post(root);
    delete_tree(root);//递归删除
}

int main(int argc, char* argv[])
{
    char *pre = "+*ab*-c/def";
    printf("pre: %s\n", pre);
    printf("====================\n");
    printf("post: ");
    pretopost1(pre);
    printf("\n");
    printf("====================\n");
    printf("post: ");
    pretopost2(pre);
    printf("\n");
    return 0;
}
posted on 2015-08-29 00:44  bajdcc  阅读(378)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3