Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

construct

Related Links:
Author: Robert Baxter

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.

Call Tree
tree.construct(7);
               +---+
               | 3 |
           ___ +---+ ___
         /               \
     +---+               +---+
     | 1 |               | 5 |
     +---+               +---+
    /     \             /     \
+---+     +---+     +---+     +---+
| 0 |     | 2 |     | 4 |     | 6 |
+---+     +---+     +---+     +---+
tree.construct(10);
               +---+
               | 4 |
           ___ +---+ ___
         /               \
     +---+               +---+
     | 1 |               | 7 |
     +---+               +---+
    /     \             /     \
+---+     +---+     +---+     +---+
| 0 |     | 2 |     | 5 |     | 8 |
+---+     +---+     +---+     +---+
               \         \         \
               +---+     +---+     +---+
               | 3 |     | 6 |     | 9 |
               +---+     +---+     +---+

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;
    ...
}
Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.