Question

Sc Python 1 Task 2 3 Consider a binary tree of N vertices 4 such that children of node K are 2* K + 1. Vertex 1 is the root K
Python 1 Task 2 A node is said to be at level X if the length of the shortest path between that node and root is X - 1. So, t
For example, given array A such that: A[2] = e A[3] 7 A[4]-8 the function should return 2. Explanation: Nodes at level 1: [1]
The sum of associated values of nodes at level 2 equals 7, and this is the maximum sum among all levels. Assume that: . N is
Sc Python 1 Task 2 3 Consider a binary tree of N vertices 4 such that children of node K are 2* K + 1. Vertex 1 is the root Kand 2 of the tree and each node has an integer value associated with it. Such a tree may be represented as an array of N integers by writing down values from consecutive nodes For example, the tree below 8 Test might be represented as an array o A node is said to be at level X if the length of the shortest path 2
Python 1 Task 2 A node is said to be at level X if the length of the shortest path between that node and root is X - 1. So, the root is at level 1, the children of the root are at level 2, and so on. Your task is to find the smallest level number X such that the sum of all the nodes at level X is maximal. Write a function: def solution(A) that, given an array A consisting of N integers denoting a tree of N vertices, returns the level with the maximal sum of values of its vertices. If there is more than one of such levels, your function should return the smallest level. Note that A[K denotes value associated with node K+1
For example, given array A such that: A[2] = e A[3] 7 A[4]-8 the function should return 2. Explanation: Nodes at level 1: [1], sum equals-1 Nodes at level 2: [2, 3], sum equals 7 +0 Nodes at level 3: [4, 51, sum equals 7+ The sum of associated values of nodes at level 2 equals 7, and this is the maximum sum among all levels. Test O Assume that: . N is an integer within the range [1..1,000] . each element of 2
The sum of associated values of nodes at level 2 equals 7, and this is the maximum sum among all levels. Assume that: . N is an integer within the range [1..1,000]; . each element of array A is an integer within the range I-100,000..100,000]. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment. 2
0 0
Add a comment Improve this question Transcribed image text
Answer #1

EXPLANATION IN CODE COMMENTS:

import queue
q = queue.Queue(20)

def solution(A):
#q to keep track of levels and nodes at that level
#First push the root node level
q.put(1)
#s to track the global maximum sum so far
s = -10000
#lvl to track the global maximum sum level
lvl = 0
#to iterate all levels
k=1
#exit condition variable for loop
flag = True
while flag:
#lsum keeps track of sum at each level
lsum = 0
#Gets no. of nodes at current level
siz = q.qsize()
#loop at all nodes at current level
while siz>0:
v = q.get()
#if that node does not exist in the array break the loop
if v > len(A):
#all nodes are traversed , exit all loops
flag = False
break
#add it to lsum
lsum+=A[v-1]
#push its child nodes into queue
q.put(2*v)
q.put(2*v+1)
siz-=1
#if globalSum < localSum then update globalSum and lvl of that sum
if s<lsum:
s=lsum
lvl = k
#increase the level
k+=1
#return level
return lvl
  
a = [-1,7,0,7,-8]
print(solution(a))

ktuzzy Spyder (Python 3.J) Eile Edit Searh So un Debug Consales Projects Tools View Help s Varisble explorer roject explorerktuzzy Spyder (Python 3.J) Eile Edit Searh So un Debug Consales Projects Tools View Help gig Venable explorer roject explorerIn [32]: runfile(C:/Users/Rogue/Desktop/sumLevelB.py, wdir-C:/Users/Rogue/ Desktop)

Add a comment
Know the answer?
Add Answer to:
Sc Python 1 Task 2 3 Consider a binary tree of N vertices 4 such that children of node K are 2* K + 1. Vertex 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • s children nodes Finding the specific od'sbothers DAodging whether the specifie node is leaf or by...

    s children nodes Finding the specific od'sbothers DAodging whether the specifie node is leaf or by the adjacent matrix, then the matrix gr Gwith n vertices, ㅲt is 10. Ifthe binary tree is stored by the Judge if the node is on the same level D Find the node position acording to its eenial method, which operation is easy to implement? 10 l Answer are Il True or False ( 10 POINTS) 2. The insertion and deletion 3. Sequential storage...

  • . [25 pts.] Tree node with largest value children. Consider a complete ternary tree where each...

    . [25 pts.] Tree node with largest value children. Consider a complete ternary tree where each node apart from the leaves has exactly 3 children and is associated with a numeric key k. a [15 pts.] Write the pseudo-code of a procedure that returns the node whose children have the largest sum of keys, i.e. the score of a node is the sum of its children key values. Note that leaves would not be considered as they do not have...

  • True or False? 1. In a 2-3 tree, the last node that splits is a leaf...

    True or False? 1. In a 2-3 tree, the last node that splits is a leaf that already contains two entries. 2. In a red-black tree, a red node cannot have red children. 3. The vertices in a graph may only have one topological order. 4. In a weighted graph, the shortest path between two given vertices has the largest edge-weight sum.

  • k-d tree Background One generalization of binary trees is the k-d tree, which stores k-dimensional data....

    k-d tree Background One generalization of binary trees is the k-d tree, which stores k-dimensional data. Every internal node of a k-d tree indicates the dimension d and the value v in that dimension that it discriminates by. An internal node has exactly two children, containing data that is less-than-or-equal and data that is greater than v in dimension d. For example, if the node distinguishes on dimension 1, value 107, then the left child is for data with y...

  • Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct...

    Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct TreeNode { int val; TreeNode *left; TreeNode *right; }; The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct...

  • 4. Suppose a B+ tree with 4 levels having exactly 7 keys in each node. There...

    4. Suppose a B+ tree with 4 levels having exactly 7 keys in each node. There is a record for each key 1, 2, . . . , k ? 1, k where k is the number of data records. How many nodes should be examined to find records with keys in the range [82, 113]?

  • 1. Suppose that d ≥ 2 is an integer constant. In a d-ary tree, each node...

    1. Suppose that d ≥ 2 is an integer constant. In a d-ary tree, each node has at most d nonempty subtrees. For example, the trees discussed along with heaps had d = 2. We can represent a nearly complete d-ary tree with n nodes using an array whose indexes range from 0 to n−1. (This is different from Cormen’s arrays, whose indexes range from 1 to n.)       Suppose that i is the index of a node in the...

  • Consider a tree with 13 nodes: the root has 4 children and each of these children has 2 children. Consider the following greedy algorithm for vertex cover: add the node with the highest degree to the...

    Consider a tree with 13 nodes: the root has 4 children and each of these children has 2 children. Consider the following greedy algorithm for vertex cover: add the node with the highest degree to the vertex cover, remove all edges incident to this node, and repeat until there are no edges left. What approximation ratio is achieved by the algorithm on this graph? Give your answer to 2 decimal places. Can someone help me with this question? Thanks.

  • Suppose that d ≥ 2 is an integer constant. In a d-ary tree, each node has...

    Suppose that d ≥ 2 is an integer constant. In a d-ary tree, each node has at most d nonempty subtrees. For example, the trees discussed along with heaps had d = 2. We can represent a nearly complete d-ary tree with n nodes using an array whose indexes range from 0 to n−1. (This is different from Cormen’s arrays, whose indexes range from 1 to n.)       Suppose that i is the index of a node in the array....

  • Question 1 (4 points) Answer the following questions. a. Draw a recursion tree for the recurrence...

    Question 1 (4 points) Answer the following questions. a. Draw a recursion tree for the recurrence T(n) T(an) + T(1- a)n cn, where 0 α < 1 and c > 0 are constants. Draw the nodes at three levels, i.e., level 0, 1, and 2 (root node is level 0).

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT