Question
Complete each of the functions below in C language. The text is below for reference: // lab7-q2.h // Cannot modify this file typedef struct ds
Complete each of the functions below in C language.
The text is below for reference:
// lab7-q2.h
// Cannot modify this file
typedef struct ds {
int size; /* size of disjoint set */
int *parent; /* parent of each element */
int *tree_size; /* size of a tree in the disjoint set */
} DS;
/* Init a disjoint set with a given size #1 #2 */
/* All node should point to itself */
DS* ds_init(int size);
/* Find the root of node x #3 */
/* Path compression is needed */
/* Return the root of node x */
int ds_find_set(DS *S, int x);
/* Check if x and y belong to the same set #4 */
/* Return 1 if they are belong to the same set */
/* Return 0 if they are not belong to the same set */
int ds_same_set(DS* S, int x, int y);
/* Union the set containing x and the set containing y #5 #6 */
/* Put the set containing y as the subtree of the set of containing x */
/* If both sets have the same set, put set containing y as the subtree of set containing x */
/* You need to update tree_size */
void ds_union(DS *S, int x, int y);
/* Union the set containing x and the set containing y #7 #8 */
/* Put the smaller set as the subtree of the larger set */
/* Remember to update tree_size */
void ds_union_by_size(DS *S, int x, int y);
/* Free the disjoint set */
/* Return NULL pointer */
DS *ds_free(DS *S);
You are going to implement the disjoint set abstract data type using forest implementation. The header file "lab7-92.h" and the description of the functions are as follows. Your program should not contain main().Name your program as lab7-92.c". // lab7-92.h // Cannot modify this file typedef struct ds { int size; /* size of disjoint set */ int *parent; 1* parent of each element */ int *tree_size; /* size of a tree in the disjoint set */ } DS; /* Init a disjoint set with a given size #1 #2 */ /* All node should point to itself */ DS* ds_init(int size); /* Find the root of node * #3 */ /* Path compression is needed */ /* Return the root of node x */ int ds_find_set(DS *5, int x); 1* Check if x and y belong to the same set #4 */ /* Return 1 if they are belong to the same set */ /* Return 0 if they are not belong to the same set */ int ds_same_set(DS* S, int x, int y); /* Union the set containing x and the set containing y #5 #6 */ /* Put the set containing y as the subtree of the set of containing x */ 1* If both sets have the same set, put set containing y as the subtree of set containing x */ /* You need to update tree_size */ void ds_union(DS *s, int x, int y); 1* Union the set containing x and the set containing y #7 #8 */ 1* Put the smaller set as the subtree of the larger set */ 1* Remember to update tree_size */ void ds_union_by_size(DS *5, int x, int y); /* Free the disjoint set */ /* Return NULL pointer */ DS *ds_free (DS *5)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