Customer Service Chat
Get quote & make Payment
Expression Tree, Programming Languages
For this assignment you will read a file expression.txt and create an expression tree. The expression will be a valid infix expression with the all the necessary parentheses so that there is no ambiguity in the order of the expression. You will evaluate the expression and print the result. You will also write the prefix and postfix versions of the same expression without any parentheses.
In an expression tree the nodes are either operators or operands. The operators will be in the set [''+'', ''-'', ''*'', ''/'']. The operands will be either integers or floating point numbers. All the operand nodes will be leaves of the expression tree. All the operator nodes will have exactly two children.
The outline of your program will be as follows:
class Stack (object):
class Node (object):
class Tree (object):
def __init__ (self):
def createTree (self, expr):
def evaluate (self, aNode):
def preOrder (self, aNode):
def postOrder (self, aNode):
The function createTree() will take as input parameter an infix expression with parentheses as a String and create an Expression Tree from it. Assume that the expression string is valid.
You will take the expression string and break it into tokens. There are four different kinds of tokens - left parenthesis, right parenthesis, operator, and operand. When we read a left parenthesis we are starting a new expression and when we read a right parenthesis we are ending an expression. Here is the algorithm that you will use:
If the current token is a left parenthesis add a new node as the left child of the current node. Push current node on the stack and make current node equal to the left child.
If the current token is an operator set the current node''s data value to the operator. Push current node on the stack. Add a new node as the right child of the current node and make the current node equal to the right child.
If the current token is an operand, set the current node''s data value to the operand and make the current node equal to the parent by popping the stack.
If the current token is a right parenthesis make the current node equal to the parent node by popping the stack if it is not empty.
For the input file this is what your program will output:
( ( 8 + 3 ) * ( 7 - 2 ) ) = 55
Prefix Expression: * + 8 3 - 7 2
Postfix Expression: 8 3 + 7 2 - *
Posted Date: 4/20/2012 9:52:36 AM | Location : United States
Ask an Expert
Expression Tree, Assignment Help, Ask Question on Expression Tree, Get Answer, Expert's Help, Expression Tree Discussions
Write discussion on Expression Tree
Your posts are moderated
Write your message here..
Shell script to delete all files from system have size zero, Normal 0 ...
Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4
Discuss on unix kernel components, Question 1 Discuss on UNIX kernel co...
Question 1 Discuss on UNIX kernel components 2 Explain process creation and process termination 3 When do a Deadlock occur? What are the Necessary Conditions for Deadlock
Java virtual machine, Expertsmind.com is expert in java assignment help ...
Expertsmind.com is expert in java assignment help JAVA Virtual Machine A Java exclusive device or java virtual machine (JVM) is an exclusive device able of undertaking Jav
Java packages, Expertsmind brings you unique solution in java assignments ...
Expertsmind brings you unique solution in java assignments Packages A Java packages is a process for planning Java sessions into namespaces just like the segments of Modu
ActionScript, looking for help with a flash game assignment.
looking for help with a flash game assignment.
Artificial intelligence, Describe briefly how you might program a computer ...
Describe briefly how you might program a computer to exhibit fear or sorrow.
Adding new features to a simple shell implementation, You will be working o...
You will be working on creating both a realistic project environment and on adding new features to a simple shell implementation provided in shell.tar.z. After you un-tar it, let's
ARM LANGUAGE, I WANT TO MAKE PROJECT IN ARM LANGUAGE CAN YOU HELP ME
I WANT TO MAKE PROJECT IN ARM LANGUAGE CAN YOU HELP ME
I have to create three batch files, I have to create a bank atm machine usi...
I have to create a bank atm machine using MS-DOS. I am struggling to create LIST ALL ACCOUNTS ROLL-BACK TRANSACTIONS ACCESS AN ACCOUNT
Biology, Mamalian skin
Accounting Assignment Help
Economics Assignment Help
Finance Assignment Help
Statistics Assignment Help
Physics Assignment Help
Chemistry Assignment Help
Math Assignment Help
Biology Assignment Help
English Assignment Help
Management Assignment Help
Engineering Assignment Help
Programming Assignment Help
Computer Science Assignment Help
Why Us ?
~24x7 hrs Support
~Quality of Work
~Time on Delivery
~Privacy of Work
Human Resource Management
Literature Review Writing Help
Terms & Conditions
Copyright by ExpertsMind IT Educational Pvt. Ltd.