Question
could someone do this within minutes and could you please explain/ annotation to each part this is java by the way a) Create a class
could someone do this within minutes and could you please explain/ annotation to each part
this is java by the way
a)
Create a class called SetPartition. It should have:
an inner class called Node, as described above;
an array of Node objects, one for each element of the set;
a constructor SetPartition (int numElements). 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)
Add a method public int getRoot (int x) to your class, 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. It should merge the subsets containing x and y into a single subset. If x and y are in the same subset already, there is nothing to do. If they are in different subsets, it should merge their trees by making xs root be the parent of ys 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)
Write a method public int depth (int x) which returns the depth of xs node in its tree (giving each root depth zero). 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 SetPartition 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 SetPartition 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 SetPartitions 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)
In this part, we will improve our set partition data structure by making the trees shallower. To do this, make a subclass ImprovedSetPartition of SetPartition. 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.)
Please again can you explain how to do this step by step if you do not mind
Step 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