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 ...
随机推荐
- STL学习系列六:List容器
List简介 list是一个双向链表容器,可高效地进行插入删除元素. list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符.it++(ok), it+5(err) #include ...
- Spring MVC Framework 注解
ControllerAdvice Spring MVC Framework会把 @ControllerAdvice注解内部使用 @ExceptionHandler.@InitBinder.@Model ...
- Ecshop 学习之路一 2016年6月30日
以前下载ecshop 都是在ecshop官网上下载,前后台模板都很难看.功能也不太齐全,这次在模板堂下载了ecshop 模板 仿小米的.做一个简单的电商网站. 页面结构还是挺简单的.功能也齐全.用ec ...
- IOS学习网址
iOS定位和位置信息获取 http://www.cnblogs.com/496668219long/p/4471757.html iOS开发系列--并行开发其实很容易 http://www.cnblo ...
- 更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found
在Xcode中当你在更新了你得证书而再重新编译你的程序,真机调试一直会出现 Code Sign error: Provisioning profile ‘XXXX’ can't be found是不是 ...
- Web项目的三层架构和MVC架构异同
http://www.cnblogs.com/zhhh/archive/2011/06/10/2077519.html 又看到有人在问三层架构和MVC的关系,感觉这种问题有点教条化了.因为它们都在逻辑 ...
- HDOJ 1151 Air Raid
最小点覆盖 Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 嗯,记录一些eclipse的快捷键
alt+/:自动补全 ctrl+/:注释 // 再按一下取消注释 ctrl+shift+\:区块注释 /* */ ctrl+shift+\:取消区块注释 ctrl+shift+f:格式化代码 ctrl ...
- 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大
Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...
- target=_parent与target=_top的区别与用途
转自:http://www.taoshaw.com/taoshaw/article.asp?id=1868 在手动改HTML代码时经常会用到target参数,常用的有两个target=_blank 和 ...