importabcfromabcimportABC,abstractmethod"""This is the interface for the expression tree Node.You should not remove it, and you can define some classes to implement it."""classNode(ABC):@abstractmethod# define your fields heredefevaluate(self)->int:passclassMyNode(Node):def__init__(self,val):self.val=valself.left=Noneself.right=Nonedefevaluate(self)->int:x=self.valifx.isdigit():returnint(x)left,right=self.left.evaluate(),self.right.evaluate()ifx=='+':returnleft+rightifx=='-':returnleft-rightifx=='*':returnleft*rightifx=='/':returnleft//right"""This is the TreeBuilder class.You can treat it as the driver code that takes the postinfix inputand returns the expression tree represnting it as a Node."""classTreeBuilder(object):defbuildTree(self,postfix:List[str])->'Node':stk=[]forsinpostfix:node=MyNode(s)ifnots.isdigit():node.right=stk.pop()node.left=stk.pop()stk.append(node)returnstk[-1]"""Your TreeBuilder object will be instantiated and called as such:obj = TreeBuilder();expTree = obj.buildTree(postfix);ans = expTree.evaluate();"""
/** * This is the interface for the expression tree Node. * You should not remove it, and you can define some classes to implement it. */abstractclassNode{publicabstractintevaluate();// define your fields hereprotectedStringval;protectedNodeleft;protectedNoderight;};classMyNodeextendsNode{publicMyNode(Stringval){this.val=val;}publicintevaluate(){if(isNumeric()){returnInteger.parseInt(val);}intleftVal=left.evaluate();intrightVal=right.evaluate();if(Objects.equals(val,"+")){returnleftVal+rightVal;}if(Objects.equals(val,"-")){returnleftVal-rightVal;}if(Objects.equals(val,"*")){returnleftVal*rightVal;}if(Objects.equals(val,"/")){returnleftVal/rightVal;}return0;}publicbooleanisNumeric(){for(charc:val.toCharArray()){if(!Character.isDigit(c)){returnfalse;}}returntrue;}}/** * This is the TreeBuilder class. * You can treat it as the driver code that takes the postinfix input * and returns the expression tree represnting it as a Node. */classTreeBuilder{NodebuildTree(String[]postfix){Deque<MyNode>stk=newArrayDeque<>();for(Strings:postfix){MyNodenode=newMyNode(s);if(!node.isNumeric()){node.right=stk.pop();node.left=stk.pop();}stk.push(node);}returnstk.peek();}};/** * Your TreeBuilder object will be instantiated and called as such: * TreeBuilder obj = new TreeBuilder(); * Node expTree = obj.buildTree(postfix); * int ans = expTree.evaluate(); */
/** * This is the interface for the expression tree Node. * You should not remove it, and you can define some classes to implement it. */classNode{public:virtual~Node(){};virtualintevaluate()const=0;protected:// define your fields herestringval;Node*left;Node*right;};classMyNode:publicNode{public:MyNode(stringval){this->val=val;}MyNode(stringval,Node*left,Node*right){this->val=val;this->left=left;this->right=right;}intevaluate()const{if(!(val=="+"||val=="-"||val=="*"||val=="/"))returnstoi(val);autoleftVal=left->evaluate(),rightVal=right->evaluate();if(val=="+")returnleftVal+rightVal;if(val=="-")returnleftVal-rightVal;if(val=="*")returnleftVal*rightVal;if(val=="/")returnleftVal/rightVal;return0;}};/** * This is the TreeBuilder class. * You can treat it as the driver code that takes the postinfix input * and returns the expression tree represnting it as a Node. */classTreeBuilder{public:Node*buildTree(vector<string>&postfix){stack<MyNode*>stk;for(autos:postfix){MyNode*node;if(s=="+"||s=="-"||s=="*"||s=="/"){autoright=stk.top();stk.pop();autoleft=stk.top();stk.pop();node=newMyNode(s,left,right);}else{node=newMyNode(s);}stk.push(node);}returnstk.top();}};/** * Your TreeBuilder object will be instantiated and called as such: * TreeBuilder* obj = new TreeBuilder(); * Node* expTree = obj->buildTree(postfix); * int ans = expTree->evaluate(); */