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 ...
随机推荐
- redis神器
redis是内存型数据库,数据保存在内存中,通过tcp直接存取,优势是速度快,并发高,缺点是数据类型有限,查询功能不强,一般用作缓存. redis具有持久化机制,可以定期将内存中的数据持久化到硬盘上. ...
- [iOS UI进阶 - 2.0] 彩票Demo v1.0
A.需求 1.模仿“网易彩票”做出有5个导航页面和相应功能的Demo 2.v1.0 版本搭建基本框架 code source:https://github.com/hellovoidworld/H ...
- CoordinatorLayout的简单应用(材料设计新控件)
CoordinatorLayout字面意思为:协调布局,一般作为根布局使用.关于这个布局,记录一下两个用法,备忘. 一.配合 FloatingActionBar 使用 <?xml version ...
- SQL SERVER 2008/2012/2012R2/2014 设置开启远程连接(sa配置)
本文方案适用于Microsoft Sql Server 2008/2012/2012 r2/2014版本,以下简称MSSQLSERVER. MSSQL默认是不允许远程连接,并且禁用sa账户的.如果想要 ...
- .NET下的延迟加载
在应用中有很多实例可能需要延迟创建对象, 比如设计模式中的单例模式就是一种非常常见的情况.如果不考虑线程安全我们通常会编写如下代码: public class SingleInstance { pri ...
- 让WPS支持VHDL的关键词加粗
WPS的VBA在这里下载:http://bbs.wps.cn/forum.php?mod=viewthread&tid=22347925 语法高亮是参考Word的,这篇文章:http://bl ...
- <Android>关于EditText中setInputType和setSingleLine的冲突
近期自己开发了一个带有删除button的EditText,一方面须要设置为SingleLine,还有一方面又须要设置输入类型,起先在xml文件里设置了android:inputType类型,在自己定义 ...
- 杭电 2034 人见人爱A-B
http://acm.hdu.edu.cn/showproblem.php?pid=2034 人见人爱A-B Time Limit: 2000/1000 MS (Java/Others) Mem ...
- 现在的SEO最须要会点啥
如今的SEO最须要会点啥,会飞天,会遁地,NONONO,不须要你这么流弊,咳咳,不瞎扯.在以往SEO的就是从搜索引擎中获取免费流量.是啊,曾经多好弄啊.而如今在我们不但须要流量还须要把流量进行转换,毕 ...
- php 下载远程图片 的几种方法(转)
1.获取远程文件大小及信息的函数 function getFileSize($url){ $url = parse_url($url); if($fp = @fso ...