Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

REITTEN as follows: Tree processing Let T be an arbitrary (NOT necessarily binary) tree. For each vertex v in T, let v.val a tain a

image text in transcribed
REITTEN as follows: Tree processing Let T be an arbitrary (NOT necessarily binary) tree. For each vertex v in T, let v.val a tain a pre-loaded number a) Write a simple recursive DFS procedure that prints the val numbers in postorder, so that T.val is printed last. (Part a intended to be a push problem,' which is not an exercise that requires a stack, but is instead a relatively easy exercise to push you in the directed needed b) Let backpack be an identifter that initialy stores the numerical value x. Write a x-9, 2 simple postorder DFS procedure Rotate(T) inputs the root of an arbitrary tree T with the val entries described in part a. And suppose that this Rotate procedure can read the identifier backpack, and can write to it as well. to solve part b). Please see if you can make that happen) 1, X-2 4 Rotate(T) must leave the physical tree T unchanged, but must change the val 1I entries in T so that a postorder printing of T's new vertex assignments will / / produce a new sequence of numbers that begins with the input value X. and 4 then continues with the original sequence as printed in part a, so that the first 0 / \ / 9 8 6 8 3 element printed in part a is the second in the new sequence, and the second in a is the third in this new listing, etc. The very last value to be printed in part a, which is 53 the root's T.val, is no longer stored in T, and should be assigned to the identifier backpack/ See the example, where the the first tree on the right is the input tree, and X = 9, The second tree shows the rotated values with the leftmost leaf set to 9 because that is the first value to be printed by a postorder printing of the vertex values. Do not write out all of the data and read it back into the tree. Hint: Think of walking along the tree edges of the tree to execute a DFS of T while wearing the backpack. So you start your trip with X sitting in the backpack At the postorder exit time of a vertex w, you printed v.val in part a. But do is remember this v.val entry and reassign it to the next vertex you postorder processed in a. And what value do lt should be the last number you printed in a right belore you printed v.val. So when you reach u in part b, the elue you want to assign to u.val? Answer. It should be in your backpack! Conceptually, you crush this th swap statement in part b. That is, you steal the logic you assign to problem without thinking by changing and correctness of part a by altering the behavior of the print instruction. Begin with the code: e print statement in part a into a global backpack initialized to the value X; procedure Rotatel:T: REITTEN as follows: Tree processing Let T be an arbitrary (NOT necessarily binary) tree. For each vertex v in T, let v.val a tain a pre-loaded number a) Write a simple recursive DFS procedure that prints the val numbers in postorder, so that T.val is printed last. (Part a intended to be a push problem,' which is not an exercise that requires a stack, but is instead a relatively easy exercise to push you in the directed needed b) Let backpack be an identifter that initialy stores the numerical value x. Write a x-9, 2 simple postorder DFS procedure Rotate(T) inputs the root of an arbitrary tree T with the val entries described in part a. And suppose that this Rotate procedure can read the identifier backpack, and can write to it as well. to solve part b). Please see if you can make that happen) 1, X-2 4 Rotate(T) must leave the physical tree T unchanged, but must change the val 1I entries in T so that a postorder printing of T's new vertex assignments will / / produce a new sequence of numbers that begins with the input value X. and 4 then continues with the original sequence as printed in part a, so that the first 0 / \ / 9 8 6 8 3 element printed in part a is the second in the new sequence, and the second in a is the third in this new listing, etc. The very last value to be printed in part a, which is 53 the root's T.val, is no longer stored in T, and should be assigned to the identifier backpack/ See the example, where the the first tree on the right is the input tree, and X = 9, The second tree shows the rotated values with the leftmost leaf set to 9 because that is the first value to be printed by a postorder printing of the vertex values. Do not write out all of the data and read it back into the tree. Hint: Think of walking along the tree edges of the tree to execute a DFS of T while wearing the backpack. So you start your trip with X sitting in the backpack At the postorder exit time of a vertex w, you printed v.val in part a. But do is remember this v.val entry and reassign it to the next vertex you postorder processed in a. And what value do lt should be the last number you printed in a right belore you printed v.val. So when you reach u in part b, the elue you want to assign to u.val? Answer. It should be in your backpack! Conceptually, you crush this th swap statement in part b. That is, you steal the logic you assign to problem without thinking by changing and correctness of part a by altering the behavior of the print instruction. Begin with the code: e print statement in part a into a global backpack initialized to the value X; procedure Rotatel:T

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

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

gpt 3 9 9 .

Answered: 1 week ago

Question

Draft a proposal for a risk assessment exercise.

Answered: 1 week ago