Question: could you answer this as soon as possible this is java language by the way a) Create a class called BasicPart. This should have: an
could you answer this as soon as possible
this is java language by the way
a)
Create a class called BasicPart. This should have:
an inner class called Node
an array of node objects, one for each element of the set, also
finally for this part a constructor BasicPart (int numElements). For this the constructor should set up the array and the Nodes to represent the partition of the set {0, 1, . . . , numElements 1} where the subsets are {0}, {1}, . . . , and {numElements 1}.
b)
Next you should add a method public int getRoot(int x) to the class you have made, which returns the value at the root of the tree containing x. (Note that, until youve written merge(), below, the result of getRoot() wont be very interesting, as each value will be in a subset containing only itself.)
c)
Add a method public boolean inSameSubset (int x, int y) that determines whether x and y are in the same subset of the partition.
d)
Add a method public void merge(int x, int y) to your class. This should merge the subsets that contain x and y into a single subset. If x and y are in the same subset already, there will be nothing to do. On the outcome that they are in different subsets, it should merge their trees by making x's root be the parent of y's root. Note that you dont need to change the array; just the nodes. For example, merge (4,2) should turn the first partition below ({0, 1, 3, 4} and {2, 5}) into the second one ({0, 1, 2, 3, 4, 5}). merge (5,1) should turn the first partition into the third (another representation of {0, 1, 2, 3, 4, 5}).

e)
Next write a method public int depth (intx). This will return the depth of x's node in its tree, this will give each root depth 0.
Write a method public int maxDepth () which returns the greatest depth of any node.
f)
Write a main method that does the following:
. Creates a BasicPart with 1 000 elements, calls merge(0,1), merge(1,2), . . . , merge(998,999) and reports the maximum node depth of the tree.
Creates a BasicPart with 1 000 elements, calls merge(998,999), merge(997,998), . . . , merge(0,1) and reports the maximum node depth of the tree.
Creates 1 000 BasicPart each with 1 000 elements, makes 750 random merges on each one and reports the average maximum node depth of these trees. (The answer should be about 6163.)
g)
For this part, we will improve our set partition data structure by making the trees shallower.
To do this, make a subclass ImprovedBasicPart of BasicPart. Override the getRoot method so that, as it navigates up a tree, it does the following to each node it visits. If the nodes grandparent (i.e., its parents parent) is not null, make the nodes grandparent be its new parent. Repeat the tests from the previous part on your new subclass. You may find that the results of the first two tests dont change, but the average maximum depth of the random merges should be much lower. (In my case, it dropped by a factor of about ten, but dont worry if you get different results: the answer will depend on how you wrote the code for merge and how you interpreted the instructions in this part.)
2 0 5 4 2 4 | 3 3 5 3
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
