1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
sergey [27]
2 years ago
10

Consider a binary search tree where each tree node v has a field v.sum which stores the sum of all the keys in the subtree roote

d at v.
(a) Modify the Insert operation (given below) to insert an element so that all the .sum fields of the tree are correct after applying the Insert operation. (Assume that all the .sum fields were correct before the Insert operation.) Your modified algorithm should take the same time as the original Insert operation. Explain your modification and give pseudo-code for the modified algorithm.

function Insert(T,z)

y <-- LocateParent(T,z)

z.parent <-- y

if (y = NIL) then T.root <-- z ;

else if (z.key< y.key) then y.left <-- z

else y.right <-- z

(b) Argue for the correctness of your algorithm. Explain why all the .sum fields are correct after running your modified Insert operation.

(c) Analyze the running time of your modified algorithm in terms of the size n and the height h of the binary search tree.
Computers and Technology
1 answer:
AVprozaik [17]2 years ago
6 0

Answer:

Each time you insert a new node, call the function to adjust the sum.

This method has to be called each time we insert new node to the tree since the sum at all the

parent nodes from the newly inserted node changes when we insert the node.

// toSumTree method will convert the tree into sum tree.

int toSumTree(struct node *node)

{

if(node == NULL)

return 0;

// Store the old value

int old_val = node->data;

// Recursively call for left and right subtrees and store the sum as new value of this node

node->data = toSumTree(node->left) + toSumTree(node->right);

// Return the sum of values of nodes in left and right subtrees and

// old_value of this node

return node->data + old_val;

}

This has the complexity of O(n).

Explanation:

You might be interested in
After adding an image to her flyer, Danica played around to see which layout would look the best. At one point, her text was on
Norma-Jean [14]

Answer:

Position Feature

Wrap text feature

Picture tools

Explanation:

3 0
3 years ago
Read 2 more answers
Page No
ratelena [41]

Answer:

ila bgfffsg hqhffa hhfw ygga gha yqvvty1uh' fttyavt h5tfauv gtta76rq

5 0
3 years ago
What principle of interaction is John using?
Rainbow [258]

Answer:

but which john

Explanation:

man people don't make sense you more man

8 0
3 years ago
Read 2 more answers
Change the shape fill color to Dark Red. It is the first option in the Standard Colors section of the color palette.
maxonik [38]
Is this a question ?
3 0
3 years ago
How many bits would be used to count the students in class today?There are 10 students
Vikentia [17]

Answer:

4 bits

Explanation:

With 4 bits you can count to 15, because 2⁴=16. The maximum number you can express is always one less, i.e., 16-1=15.

In general, with n bits you can count to 2ⁿ-1.

7 0
3 years ago
Read 2 more answers
Other questions:
  • Which of the following is important to do when downloading a game to your
    8·1 answer
  • _____ is a web application server that provides the ability to connect web servers to multiple data sources.
    9·1 answer
  • What is this tool called?
    5·2 answers
  • Complete the paragraph describing characteristics of an audio mixer
    8·2 answers
  • Which email client feature allows you to store the names and information of people you contact frequently?
    5·1 answer
  • Respond to the following in three to five sentences. Select one netiquette guideline. Explain how this helps you draft more effe
    9·1 answer
  • You learned that properly edited resumes are necessary for making a good impression on a university or a potential employer. Dis
    13·1 answer
  • Some commands listed in a menu cannot be selected.<br><br><br> True or False
    13·2 answers
  • When creating a study schedule, why is it important to be realistic about how much time everything requires?
    8·2 answers
  • Which software programs should students avoid using to create and submit course work? (Choose all that apply). Word, Pages, Numb
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!