Question: Write a method construct that accepts an integer n as a parameter and that constructs a new tree of integers with n nodes numbered 0

Write a method construct that accepts an integer n as a parameter and that constructs a new tree of integers with n nodes numbered 0 through (n - 1) such that an in-order traversal of the tree will produce the values in sequential order. The tree should be balanced in that the number of values in any node's left subtree should always be within one of the number of values in its right subtree. If there is an extra value in one of a node's two subtrees, it should appear in the right subtree.

For example, given a variable tree of type IntTree, the call of tree.construct(7); should produce the first tree shown below. If the call had been tree.construct(10);, the tree's state should be that of the second tree shown below.

Write a method construct that accepts an integer n as a parameter

Your method should replace any existing tree. It should construct an empty tree if passed a value of 0 and should generate an IllegalArgumentException if passed a negative value.

Assume that you are adding this method to the IntTree class as defined below:

public class IntTree {
 private IntTreeNode overallRoot;
 ...
}

I'm practicing for my CS final and I've been trying to solve this practice problem for almost two days now and I just can't figure it out.

We are suppose to use x = change(x)

Call tree.construct(7); Tree 1 tree.construct (10); 4 1 7 6

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!