递归算法(二)——前缀转后缀
#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;
}
递归算法(二)——前缀转后缀的更多相关文章
- 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树
另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...
- 关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 '*' 的处理
一.删除前缀 '*' #include<iostream> #include<cstdio> using namespace std; //主函数 int main() { ] ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- Android删除指定路径下指定前缀或后缀的文件
微信公众号:CodingAndroid CSDN:http://blog.csdn.net/xinpengfei521声明:本文由CodingAndroid原创,未经授权,不可随意转载! 需求 我们在 ...
- C# 输出一个字符串的前缀、后缀和它的子串(信息内容安全 实验一)
一.什么是前后缀 字符串的前缀:符号串左部的任意子串(或者说是字符串的任意首部) 字符串的后缀:符号串右部的任意子串(或者说是字符串的任意尾部) 举例:比如 101110 它的前缀就是空串.1.10. ...
- POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)
给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...
- POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)
题目链接:http://poj.org/problem?id=2752 题意:给你一个字符串,求出所有前缀后缀(既是前缀又是后缀的子串)的长度 思路:首先整个字符串肯定既是前缀又是后缀,为最大的前缀后 ...
- 【分治-前缀积后缀积】JS Window @2018acm徐州邀请赛G
问题 G: JS Window 时间限制: 2 Sec 内存限制: 512 MB 题目描述 JSZKC has an array A of N integers. More over, he has ...
- HDU6025 Coprime Sequence —— 前缀和 & 后缀和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6025 Coprime Sequence Time Limit: 2000/1000 MS (Java/ ...
随机推荐
- RDIFramework.NET ━ 9.7 操作权限项管理 ━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.7 操作权限项管理 -Web部分 随着经济全球化趋势的发展和企业间竞争的加剧,企业对管理要求不断变化.提高,越来越多的信息都表 ...
- Linux下安装APache
1:在图形界面下下载apache 安装包,我下的是 httpd-2.2.9.tar.gz 源码安装包,地址是http://httpd.apache.org/download.cgi 2:用:gzip ...
- Linux下使用shell实现上传linux下某个目录下所有文件到ftp
首先我们需要搞清楚单个文件怎么上传,把这个单文件上传到ftp上的实现命名为一个:upload_to_ftp_command.sh 之后,需要弄清楚怎么实现遍历一个目录下的所有文件的,把这个遍历某个目录 ...
- Java程序员从笨鸟到菜鸟之(五十一)细谈Hibernate(二)开发第一个hibernate基本详解
在上篇博客中,我们介绍了<hibernate基本概念和体系结构>,也对hibernate框架有了一个初步的了解,本文我将向大家简单介绍Hibernate的核心API调用库,并讲解一下它的基 ...
- java mybatis 中sql 模糊查询
示例: like concat('%',#{groupName},'%') //-------------- <select id="findList" resultType ...
- 关于华擎X99+5820K
受到之前Intel I7-5775C的困扰,于是直接整套平台换掉. 把Z97+I7-5775C+DDR3换成了X99+5820K+DDR4. 但是依然不理想,又是另外一个坑. 组装好后安装系统的过程 ...
- 文件上传插件uploadify详解
官网:http://www.uploadify.com/ 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同时上传,上传进行进度显示,删除已上传文件. 要求使用jquery1.4或以上 ...
- 解决ADB端口占用问题
方式一5037为adb默认端口,若5037端口被占用,查看占用端口的进程PIDC:\Users\wwx229495>netstat -aon|findstr 5037 TCP 127. ...
- OnScrollListener分页加载
scrollState有三种状态,分别是SCROLL_STATE_IDLE.SCROLL_STATE_TOUCH_SCROLL.SCROLL_STATE_FLING *SCROLL_STATE_ ...
- DDL、DML、DCL的理解
1.DDL 1-1.DDL的概述 DDL(Data Definition Language 数据定义语言)用于操作对象和对象的属性,这种对象包括数据库本身,以 ...