MODULE-4
MODULE-4
MODULE-4
struct node {
int key;
struct node *left,
*right;
};
struct node* search(struct node* root, int
key)
{
{
parent[j]=i;
}
Weighting union and Collapsing
find
• Weighting rule for union(i, j): If the number of nodes in tree i is less
than the number in tree j then make j the parent of i; otherwise make
i the parent of j.
• we maintain a count field in the root of every tree.
• If i is a root node, then count[i] equals the number of nodes in that
tree. Since all nodes but the roots of trees have a nonnegative
number in the parent field, we can maintain the count in the parent
field of the roots as a negative number.
void union2(int i, int j)
{
/* union the sets with roots i and j, union the sets with roots i and j, i !=
j, the weighting rule*/
/* parent[i] = -count[i] and parent[j] = -count[j] */