Question: Java programming 1 . Implement the Node class, as follows: ` ` ` class Node int value int size int depth Node parent ` `
Java programming
Implement the Node class, as follows:
class Node
int value
int size
int depth
Node parent
Implement the treebased unionfind data structure, which should contain three basic methods:
a Implement the makeSet algorithm, which should take an int X and return an instance of the Node class, which should have X as its value, itself as a parent, a depth of and a size of
makeSetint x
v new Node
vvalue x
vparent v
vdepth
ysize
return v
b Implement the union algorithm, which should take two Nodes X and Y and join them together.
unionnode X node Y
Xparent Y
Ysize Xsize
return Y
c Implement the find algorithm, which should take a node X and return the root of the set that it is a member of and will also calculate the current depth of X in doing so
findnode X
node R X
while Rparent R
R Rparent
Xdepth
return R
The above pseudocode will function as a unionfind data structure, but is lacking two key heuristics that will optimize its performance:
Unionbysize: within unionmathrmxmathrmy check which of the two sets is smaller, and set that one to become a subset child of the other.
Path compression: within findX before returning R iterate through each node Z on the path between X and R and set Z s parent to equal R
a Implement a new version of union called union that will do the same thing as union but also satisfies the unionbysize heuristic
b Implement a new version of find called of find that will do the same thing as find but also satisfies the path compression heuristic.
Test your implementation with this process:
a Create an array of singleton sets ie instances of the Node class each with a value equal to its index in the array eg array should be a Node with value
b Create a copy of this array
c Call the operation unionfindarrayi findarrayi for all values of i from to within your first array
d Iterate through each node in this array and print its value, root, and depth
e Call the operation unionfindarrayi findarrayi for all values of i from to within the second array
f Iterate through each node in this array and print its value, root, and depth
Eg for the two disjoint sets to the right, the noderoot depth format would be:
; ; ; ; ; ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
