From 87818848122ac94995e359a8db3a0ed9a9173d42 Mon Sep 17 00:00:00 2001 From: kartofen Date: Sat, 29 Oct 2022 13:18:45 +0300 Subject: implemented --- src/set.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/set.c (limited to 'src/set.c') 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 + +#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++; +} -- cgit v1.2.3