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.
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 6Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started