Implement LeafNode and InteriorNode classes for the expression tree as discussed on this page Use this template:  Please don't change any function names Add any methods if necesssary TODO: Remove the pass statements and implement the methods. ''' class LeafNode: def __init__(self, data): self.data = data def postfix(self): return str(self) def __str__(self): return str(self.data) def prefix(self): pass def infix(self): pass def value(self): return self.data class InteriorNode: def __init__(self, op, left_op, right_op): self.op = op self.left_op = left_op self.right_op = right_op def postfix(self): return self.left_op.postfix() + " " + self.right_op.postfix() + " " + self.op def prefix(self): pass def infix(self): pass def value(self): pass if __name__ == "__main__": # TODO: (Optional) your test code here. a = LeafNode(4) b = InteriorNode('+', LeafNode(2), LeafNode(3)) c = InteriorNode('*', a, b) c = InteriorNode('-', c, b)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Implement LeafNode and InteriorNode classes for the expression tree as discussed on this page

Use this template: 

Please don't change any function names
Add any methods if necesssary
TODO: Remove the pass statements and implement the methods.
'''
class LeafNode:
def __init__(self, data):
self.data = data
def postfix(self):
return str(self)
def __str__(self):
return str(self.data)
def prefix(self):
pass
def infix(self):
pass
def value(self):
return self.data
class InteriorNode:
def __init__(self, op, left_op, right_op):
self.op = op
self.left_op = left_op
self.right_op = right_op
def postfix(self):
return self.left_op.postfix() + " " + self.right_op.postfix() + " " +
self.op
def prefix(self):
pass
def infix(self):
pass
def value(self):
pass
if __name__ == "__main__":
# TODO: (Optional) your test code here.
a = LeafNode(4)
b = InteriorNode('+', LeafNode(2), LeafNode(3))
c = InteriorNode('*', a, b)
c = InteriorNode('-', c, b)

316
The constructor for Leaf Node expects an integer as an argument, whereas the con-
structor for Interior Node expects a character-based operator symbol and two other
nodes as arguments.
Here is a short tester program that illustrates the use of the node classes:
from expressiontree import Leaf Node, InteriorNode
a = Leaf Node (4)
b
InteriorNode('+',
Leaf Node (2), Leaf Node (3))
c = InteriorNode('*',
a, b)
c = InteriorNode('-', c, b)
=
print("Expect ((4* (2+3)) - (2+3)):", c.infix())
print("Expect - *4 +23+23
:", c.prefix())
print("Expect 4 2 3+*23 + -
:", c.postfix())
print("Expect 15
:", c.value())
You now develop one of the traversal methods for both classes and leave the others as
exercises for you. The method postfix returns the string representation of an expres-
sion in postfix form. In the case of a LeafNode, that is the string representation of the
node's integer.
class LeafNode(object):
"""Represents an integer.
def init (self, data):
self. data data
=
def postfix (self):
return str(self)
def str (self):
return str(self. data)
An InteriorNode's postfix string contains the postfix strings of its two operand nodes,
followed by the node's operator.
class Interior Node (object):
"""
""Represents an operator and its two operands."
def init (self, op, leftOper, rightOper):
self. operator = op
self. leftOperand=left0per
self. rightOperand= rightOper
def postfix (self):
"11"1
Trees
return self. leftoperand.postfix()+""+\
self. rightOperand.postfix() + " " + \
self. operator
Chapter 10
The design pattern of the postfix methods of Interior Node and Leaf Node is like the one
used for the traversals of binary trees. The only difference is that in this application, an
expression tree is never empty, so a leaf node is the base case. The other expression tree
traversals have a similar design and are left as exercises for you.
Transcribed Image Text:316 The constructor for Leaf Node expects an integer as an argument, whereas the con- structor for Interior Node expects a character-based operator symbol and two other nodes as arguments. Here is a short tester program that illustrates the use of the node classes: from expressiontree import Leaf Node, InteriorNode a = Leaf Node (4) b InteriorNode('+', Leaf Node (2), Leaf Node (3)) c = InteriorNode('*', a, b) c = InteriorNode('-', c, b) = print("Expect ((4* (2+3)) - (2+3)):", c.infix()) print("Expect - *4 +23+23 :", c.prefix()) print("Expect 4 2 3+*23 + - :", c.postfix()) print("Expect 15 :", c.value()) You now develop one of the traversal methods for both classes and leave the others as exercises for you. The method postfix returns the string representation of an expres- sion in postfix form. In the case of a LeafNode, that is the string representation of the node's integer. class LeafNode(object): """Represents an integer. def init (self, data): self. data data = def postfix (self): return str(self) def str (self): return str(self. data) An InteriorNode's postfix string contains the postfix strings of its two operand nodes, followed by the node's operator. class Interior Node (object): """ ""Represents an operator and its two operands." def init (self, op, leftOper, rightOper): self. operator = op self. leftOperand=left0per self. rightOperand= rightOper def postfix (self): "11"1 Trees return self. leftoperand.postfix()+""+\ self. rightOperand.postfix() + " " + \ self. operator Chapter 10 The design pattern of the postfix methods of Interior Node and Leaf Node is like the one used for the traversals of binary trees. The only difference is that in this application, an expression tree is never empty, so a leaf node is the base case. The other expression tree traversals have a similar design and are left as exercises for you.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Mergesort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education