Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Ex. 2.5RRPN=PN slack evaluation Assume that objects in sequence of arguments in a function call are computed from left to right. So if x and

image text in transcribed
image text in transcribed
Ex. 2.5RRPN=PN slack evaluation Assume that objects in sequence of arguments in a function call are computed from left to right. So if x and y are functions in the call eval (+,x,y), then x is evalated belore y. Let L be a stack containing the reverse of an RPN (i.e, a PN) listing for an arithmetic expression tree. So the tree on the right has the PN listing below (with the + on the top of the stack, and 1 at the bottom): +,3,2,,,4,+,,+,,9,,11,10,,8,7,6,5,1. a) A recursive function to evaluate L is shown below, but line 4 is incomplete. Please complete it (and just assume that eval (op, x,y ) works correctly). function StackEval(;: L ): \{Please note that StackEval modifies stack L. \} input: stack L represents an arithmetic expression as described above; output: Returns the evaluation of L; t : PopFrom (L); i x is a number then return (x) alse 4 returnleval (x. endif end_StackEval: Let L be computed as described above for the tree shown on the right. b) Which are the first two leaves to be processed when StackEval runs on the reverse RPN list L for this tree? c) If the code in part a) is to work correctly, should eval(op,x,y) equal the infix expression x op y, or y0px ? d) Suppose eval's implementation gives the wrong answer to c). Please present repaired StackEval function. Let T be an arbitrary tree with a very specific representation that will be specified lour lines from now. Please use pseudocode or a real programming language of your choice to present a DFS procedure that will traverse T recursively - just like our pseudcode - and print the vertex names in a postorder listing So the print statement executes at the postorder exit time for each vertex The vertex names are 0,1,2,3,,n. For each vertex j, its children's names are stored in the array child j,] where: child j,0] is not a name, but is the number of children that j has. So this count is zero if j is a leaf. The names of j 's children are therefore: child [j,1]. child [j,2], ... child [j,k], where k equals child [j,0]. Assume that vertex 0 is the root, so that T will be DFSed via the instruction DFS(0); That is, this DFS procedure will have integer names as the vertex arguments. For that reason, the solution will use vertex identifiers such as i and j instead of u and v. Of course, all names are equally good. The point of this exercise is to demonstrate how easily pseudocode can be turned into real code in a real programming language. You can assume that the array child is globally accessible to your procedure

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions