源码: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. wkhtmltopdf 中文参数详解

    linux:wkhtmltopdf [OPTIONS]… [More input files] windows:wkhtmltopdf.exe [OPTIONS]… [More input files ...

  2. [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. Pyhton 学习总结 21 :fileinput模块

    fileinput模块可以对一个或多个文件中的内容进行迭代.遍历等操作.该模块的input()函数有点类似文件readlines()方法,区别在于前者是一个迭代对象,需要用for循环迭代,后者是一次性 ...

  4. AppleWatch___学习笔记(二)UI布局和UI控件

    1.UI布局 直接开发,你会发现Apple Watch并不支持AutoLayout,WatchKit里有个类叫做WKInterfaceGroup,乍一看像是UIView,但是这货其实是用来布局的.从 ...

  5. NET中的类型和装箱/拆箱原理

    谈到装箱拆箱,DebugLZQ相信给位园子里的博友一定可以娓娓道来,大概的意思就是值类型和引用类型的相互转换呗---值类型到引用类型叫装箱,反之则叫拆箱.这当然没有问题,可是你只知道这么多,那么Deb ...

  6. 解决PowerDesigner 生成Sql2005-2012 找不到sysproperties表的问题

    造成此问题的原因是由于Sql 2005 删除了系统表 sysproperties 而改用 sys.extended_properties 表所致 ,微软的目的不再去猜测网上有二种解决方式 但不符合本人 ...

  7. JAVA线程锁lock下Condition高级使用-多个Condition的整合使用

    import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.uti ...

  8. 读书笔记 1 of Statistics :Moments and Moment Generating Functions (c.f. Statistical Inference by George Casella and Roger L. Berger)

    Part 1: Moments Definition 1 For each integer $n$, the nth moment of $X$, $\mu_n^{'}$ is \[\mu_{n}^{ ...

  9. Reflection

    Reflection 反射能在运行时获取一个类的全部信息,并且可以调用类方法,修改类属性,创建类实例. 而在编译期间不用关心对象是谁 反射可用在动态代理,注解解释,和反射工厂等地方. -------- ...

  10. python 字符串 转 dict

    比直接eval更好的方法>>>import ast >>>ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'k ...