Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3. [11 marks] A leaf-to-root node path is a sequence of connected nodes v,2, ..., vk where vi is a leaf and Uk is the

image text in transcribed
image text in transcribed
3. [11 marks] A leaf-to-root node path is a sequence of connected nodes v,2, ..., vk where vi is a leaf and Uk is the root of a tree. Consider the problem! we will call the maximum-sum leaf-to-root problem: Given as input the root r of a tree (not necessarily binary) in which every node stores an integer value, return the value of the maximum leaf-to-oot node path, where the value of a leaf-to-root node path is the sum of values of nodes in the path. For example, the path J-E-B-A has value 1 +2+2+ 1 = 6, but the maximum-surn leaf-to-root path is M-F-B-A with value 4+4+2+1-11. Your algorithm is not responsible for finding the optimal path, just its value. Your algorithm must not modify the tree. Node e.value Design an algorithm that solves the max-sum leaf-to-root problem. For a node v use v.value to denote the value stored in w: v.isLeaf has value true if node v is a leaf and it has value false otherwise. To access the children of a node v use the following pseudocode: for each child c of u do You must. )7 marks Write pseudocode for the algorithm. b) [4 mark] Compute the worst-case time complexity of your algorithm as a function of the total number n of nodes in the tree. You must explain how you computed the time complexity, and give the order of the time complexity of the algorithm. 3. [11 marks] A leaf-to-root node path is a sequence of connected nodes v,2, ..., vk where vi is a leaf and Uk is the root of a tree. Consider the problem! we will call the maximum-sum leaf-to-root problem: Given as input the root r of a tree (not necessarily binary) in which every node stores an integer value, return the value of the maximum leaf-to-oot node path, where the value of a leaf-to-root node path is the sum of values of nodes in the path. For example, the path J-E-B-A has value 1 +2+2+ 1 = 6, but the maximum-surn leaf-to-root path is M-F-B-A with value 4+4+2+1-11. Your algorithm is not responsible for finding the optimal path, just its value. Your algorithm must not modify the tree. Node e.value Design an algorithm that solves the max-sum leaf-to-root problem. For a node v use v.value to denote the value stored in w: v.isLeaf has value true if node v is a leaf and it has value false otherwise. To access the children of a node v use the following pseudocode: for each child c of u do You must. )7 marks Write pseudocode for the algorithm. b) [4 mark] Compute the worst-case time complexity of your algorithm as a function of the total number n of nodes in the tree. You must explain how you computed the time complexity, and give the order of the time complexity of the algorithm

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

Strategic Database Technology Management For The Year 2000

Authors: Alan Simon

1st Edition

155860264X, 978-1558602649

More Books

Students also viewed these Databases questions