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))
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...
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 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 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. 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 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 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]?
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 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....
[Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...
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).