Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the following problem: You are given a pointer to the root r of a binary tree, where each vertex v has pointers v.lc and
Consider the following problem: You are given a pointer to the root r of a binary tree, where each vertex v has pointers v.lc and v.rc to the left and right child, and a value Val(v) > 0. The value NIL represents a null pointer, showing that v has no child of that type. You wish to find the maximum total sum among all paths from r to a leaf A divide-and-conquer algorithm for this problem is as follows: The algorithm will take a node r as an input and return the maximum total sum from r to a leaf node. First, recurse on the left child r.lc to get the maximum total sum among all paths from r.lc to a leaf and recurse on the right child r.rc to get the maximum total sum among all paths from r.rc to a leaf Then return the maximum of the recursive calls and add on the value of r. MaxPath(r) 2. leftweight -MaxPath(r.lc) 3. else: 4. leftweight 0 6, rightweight AAaxPath(r.rc) 7. else: rightweight =0 9. return max(leftweight, rightweight) + Value(r). (a) Give a recurrence and then solve it using the master theorem for the time complexity of the above algorithm when the input is a complete binary tree with n vertices.(10 points) (b) Give a recurrence for an arbitrary tree with n vertices in terms of the sizes of the left subtree (L) and right subtree (R) and use induction to show that the worst-case runtime is O(n). (10 points) Consider the following problem: You are given a pointer to the root r of a binary tree, where each vertex v has pointers v.lc and v.rc to the left and right child, and a value Val(v) > 0. The value NIL represents a null pointer, showing that v has no child of that type. You wish to find the maximum total sum among all paths from r to a leaf A divide-and-conquer algorithm for this problem is as follows: The algorithm will take a node r as an input and return the maximum total sum from r to a leaf node. First, recurse on the left child r.lc to get the maximum total sum among all paths from r.lc to a leaf and recurse on the right child r.rc to get the maximum total sum among all paths from r.rc to a leaf Then return the maximum of the recursive calls and add on the value of r. MaxPath(r) 2. leftweight -MaxPath(r.lc) 3. else: 4. leftweight 0 6, rightweight AAaxPath(r.rc) 7. else: rightweight =0 9. return max(leftweight, rightweight) + Value(r). (a) Give a recurrence and then solve it using the master theorem for the time complexity of the above algorithm when the input is a complete binary tree with n vertices.(10 points) (b) Give a recurrence for an arbitrary tree with n vertices in terms of the sizes of the left subtree (L) and right subtree (R) and use induction to show that the worst-case runtime is O(n). (10 points)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started