Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions

Question

Evaluate the integral. 5x + 3x - 2 x + 2x .3 dx

Answered: 1 week ago

Question

Explain all drawbacks of application procedure.

Answered: 1 week ago

Question

2. List the advantages of listening well

Answered: 1 week ago