源码: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;
}

递归算法(二)——前缀转后缀的更多相关文章

  1. 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树

    另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...

  2. 关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 '*' 的处理

    一.删除前缀 '*' #include<iostream> #include<cstdio> using namespace std; //主函数 int main() { ] ...

  3. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  4. Android删除指定路径下指定前缀或后缀的文件

    微信公众号:CodingAndroid CSDN:http://blog.csdn.net/xinpengfei521声明:本文由CodingAndroid原创,未经授权,不可随意转载! 需求 我们在 ...

  5. C# 输出一个字符串的前缀、后缀和它的子串(信息内容安全 实验一)

    一.什么是前后缀 字符串的前缀:符号串左部的任意子串(或者说是字符串的任意首部) 字符串的后缀:符号串右部的任意子串(或者说是字符串的任意尾部) 举例:比如 101110 它的前缀就是空串.1.10. ...

  6. POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)

    给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...

  7. POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)

    题目链接:http://poj.org/problem?id=2752 题意:给你一个字符串,求出所有前缀后缀(既是前缀又是后缀的子串)的长度 思路:首先整个字符串肯定既是前缀又是后缀,为最大的前缀后 ...

  8. 【分治-前缀积后缀积】JS Window @2018acm徐州邀请赛G

    问题 G: JS Window 时间限制: 2 Sec  内存限制: 512 MB 题目描述 JSZKC has an array A of N integers. More over, he has ...

  9. HDU6025 Coprime Sequence —— 前缀和 & 后缀和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6025 Coprime Sequence Time Limit: 2000/1000 MS (Java/ ...

随机推荐

  1. C# “快捷方式” 实现程序开机启动

    添加引用: COM : Windows Script Host Object Model Name: Interop.IWshRuntimeLibrary 添加命名空间: using IWshRunt ...

  2. rocketmq简单搭建

    摘要: 简单的部署管理RocketMQ队列 (nameserver broker在同一机器上) RocketMQ 是alibaba开源的消息队列. 本文使用的是开源版本v3.18 系统: centos ...

  3. Lucene学习总结

    在使用Lucene前,我们先大致熟悉下Lucene的几个核心类. 核心索引类: public class IndexWriter 索引过程的中心组件,把它想象成一个可以对索引进行写操作的对象. pub ...

  4. POJ 2876 Cantoring Along

    Description The Cantor set was discovered by Georg Cantor. It is one of the simpler fractals. It is ...

  5. 基于webrtc的视频通话时webrtc的接口调用流程

    场景: 1.A call B 2.B answer 3.A connected with B 共同的步骤: A 和 B 都需要初始化webrtc模块,创建peerconnectionfactory 步 ...

  6. [JavaScript]JavaScript处理iframe的动作

    随着W3C一声令下,几年前使用非常频繁的frameset + frame已完成使命,光荣退伍.作为frameset的替代方案(姑且这么称吧),iframe的使用也多了起来.较frameset方案,if ...

  7. laravel框架总结(七) -- 数据库操作

      1.使用DB门面进行基本操作 一旦你设置好了数据库连接,就可以使用 DB facade 来进行查找.DB facade 提供每个类型的查找方法:select.update.insert.delet ...

  8. 前端工程师的PS默认工作区

    右侧依次是信息.图层.历史记录,如下图:

  9. Flex Excel下载

    最近做Flex里的Excel下载,用as3xls进行Excel导出后,Excel修改编辑后老出现:不能以当前格式保存...若要保存所做的更改,请单击“确定”,然后将其另存为最新的格式. 最后通过JAV ...

  10. Servlet 实现上传文件以及同时,写入xml格式文件和上传

    package com.isoftstone.eply.servlet; import java.io.BufferedReader; import java.io.BufferedWriter; i ...