Evaluation of Expression Tree
Evaluation of Expression Tree
Given a simple expression tree, consisting of basic binary operators i.e., + , – ,* and / and some integers, evaluate the expression tree.
Examples:
Input :
Root node of the below tree
Output :
100 Input :
Root node of the below tree
Output :
110
We strongly recommend you to minimize your browser and try this yourself first.
As all the operators in the tree are binary hence each node will have either 0 or 2 children. As it can be inferred from the examples above , the integer values would appear at the leaf nodes , while the interior nodes represent the operators.
To evaluate the syntax tree , a recursive approach can be followed .
Algorithm :
Let t be the syntax tree
If t is not null then
If t.info is operand then
Return t.info
Else
A = solve(t.left)
B = solve(t.right)
return A operator B
where operator is the info contained in t
The time complexity would be O(n), as each node is visited once. Below is a C++ program for the same:
#include <iostream>
#include <cstdlib>
using namespace std; typedef struct node{
string s;
node *left;
node *right;
node(string x): s(x), left(NULL), right(NULL){}
}Node; // Utility function to return the integer value
// of a given string
int toInt(string s){
int len = s.length();
int num = ;
for(int i = ; i < len; i++){
num = num * + (s[i]-'');
}
return num;
} // Check which operator to apply
int calculate(const char *c, int lval, int rval){
int ans;
switch(*c){
case '+': ans = lval + rval; break;
case '-': ans = lval - rval; break;
case '*': ans = lval * rval; break;
case '/': ans = lval / rval; break;
}
return ans;
} // This function receives a node of the syntax tree
// and recursively evaluates it
int eval(Node *root){
// empty tree
if(root == NULL)
return ;
// leaf node i.e, an integer
if(root->left == NULL && root->right == NULL)
return toInt(root->s);
// Evaluate left subtree
int lval = eval(root->left);
// Evaluate right subtree
int rval = eval(root->right);
return calculate((root->s).c_str(), lval, rval);
} int main()
{
// create a syntax tree
node *root = new node("+");
root->left = new node("*");
root->left->left = new node("");
root->left->right = new node("");
root->right = new node("-");
root->right->left = new node("");
root->right->right = new node("");
cout << eval(root) << endl; delete(root); root = new node("+");
root->left = new node("*");
root->left->left = new node("");
root->left->right = new node("");
root->right = new node("-");
root->right->left = new node("");
root->right->right = new node("/");
root->right->right->left = new node("");
root->right->right->right = new node(""); cout << eval(root);
system("pause");
return ;
}
100
110
参考:http://www.geeksforgeeks.org/evaluation-of-expression-tree/
Evaluation of Expression Tree的更多相关文章
- Expression Tree Basics 表达式树原理
variable point to code variable expression tree data structure lamda expression anonymous function 原 ...
- Expression Tree 扩展MVC中的 HtmlHelper 和 UrlHelper
表达式树是LINQ To everything 的基础,同时各种类库的Fluent API也 大量使用了Expression Tree.还记得我在不懂expression tree时,各种眼花缭乱的A ...
- 使用Expression Tree构建动态LINQ查询
这篇文章介绍一个有意思的话题,也是经常被人问到的:如何构建动态LINQ查询?所谓动态,主要的意思在于查询的条件可以随机组合,动态添加,而不是固定的写法.这个在很多系统开发过程中是非常有用的. 我这里给 ...
- Reflection和Expression Tree解析泛型集合快速定制特殊格式的Json
很多项目都会用到Json,而且大部分的Json都是格式固定,功能强大,转换简单等,标准的key,value集合字符串:直接JsonConvert.SerializeObject(List<T&g ...
- .NET Expression Tree
Expression Tree 第一个简单的例子. [TestMethod] public void GodTest() { Expression<Func<int, int, int&g ...
- 转载Expression Tree揭秘
概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHiber ...
- 深入学习C#匿名函数、委托、Lambda表达式、表达式树类型——Expression tree types
匿名函数 匿名函数(Anonymous Function)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...
- Expression Tree Build
The structure of Expression Tree is a binary tree to evaluate certain expressions.All leaves of the ...
- 表达式树(Expression Tree)
饮水思源 本文并非原创而是下面网址的一个学习笔记 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/e ...
随机推荐
- HDU 4052 Adding New Machine (线段树+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4052 初始给你w*h的矩阵,给你n个矩形(互不相交),按这些矩形尺寸把初始的矩形扣掉,形成一个新的'矩 ...
- poj3687
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9028 Accepted: 2444 De ...
- 【三支火把】---CDS5516舵机调试以及自己的感想!
我依然坚持,任何一次自己的心有感触都要及时的记录下来,这样你的努力才是真正的努力. 这两天一直在用STM32调试CDS5516舵机,其实很简单,但是却花了将近两天的时间,过程之曲折我就不说了,先总结一 ...
- Unity3D之Mecanim动画系统学习笔记(十一):高级功能应用
动作游戏 还记得读书的时候熬夜打<波斯王子>的时光,我们的王子通过跳跃穿过墙壁的小洞.在高层建筑上进行攀爬和跳跃,还有在操作失误掉下高楼和触发必死机关后使用时之沙的时光倒流功能回归死亡之前 ...
- LINUX的一些常用操作
CentOs6.7关闭防火墙(SecureCRT连接不上) 解决方法:______________________________________一.开启SSH以root用户登录Linux,打开终端, ...
- Mac使用指南
1.csrutil命令 简单来说 是苹果在新系统后加入的一个安全机制. Rootless讨论的前提是假定root账户是OS X(或者其他Unix系统)中对抗恶意程序保护操作系统的最后一道防线.意思是一 ...
- Line去年营收超5亿美元 远超竞争对手WhatsApp
原文地址: http://news.cnblogs.com/n/206072/ 凭借着修改表情取悦国际用户的做法,日本移动消息应用 Line 在全球的用户总数已经超过 4 亿.Line.微信.What ...
- Oracle DataGuard搭建(一)
第一次搭建oracle dataguard.学oracle很长时间,却没有完整的搭过dg,说起来让人笑.总得有第一次,而且第一次总是很痛苦的. 数据库版本: Oracle Database 11g E ...
- MVC4 EF6 MYSQL
在MVC的框架下连接mysql数据库 将EF框架升级到EF6 将NEW JSON升级到与之相匹配的版本 然后进行相应的配置就可以了
- HDU 4121 Xiangqi 模拟题
Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...
Output :
Output :