diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-10-29 13:18:45 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-10-29 13:18:45 +0300 |
commit | 87818848122ac94995e359a8db3a0ed9a9173d42 (patch) | |
tree | cc86f6b34f0d9a6f64b963c6ef8d1252f1e61db9 /src/set.c |
Diffstat (limited to 'src/set.c')
-rw-r--r-- | src/set.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/set.c b/src/set.c new file mode 100644 index 0000000..b84ae6a --- /dev/null +++ b/src/set.c @@ -0,0 +1,40 @@ +#include <stdlib.h> + +#include "set.h" + +set *set_create() +{ + set *s = malloc(sizeof(set)); + s->parent = s; + s->rank = 0; + return s; +} + +void set_free(set *s) +{ + free(s); +} + +set *set_find(set *s) +{ + if(s->parent == s) + return s; + s->parent = set_find(s->parent); + return s->parent; +} + +void set_union(set *x, set *y) +{ + x = set_find(x); + y = set_find(y); + + if(x == y) return; + + if(x->rank > y->rank) { + y->parent = x; + } else { + x->parent = y; + } + + if(x->rank == y->rank) x->rank++; +} |