Change project structure to a Maven Java project + Refactor (#2816)

This commit is contained in:
Aitor Fidalgo Sánchez
2021-11-12 07:59:36 +01:00
committed by GitHub
parent 8e533d2617
commit 9fb3364ccc
642 changed files with 26570 additions and 25488 deletions

View File

@@ -0,0 +1,35 @@
package com.thealgorithms.datastructures.disjointsets;
public class DisjointSets<T> {
public Node<T> MakeSet(T x) {
return new Node<T>(x);
}
;
public Node<T> FindSet(Node<T> node) {
if (node != node.parent) {
node.parent = FindSet(node.parent);
}
return node.parent;
}
public void UnionSet(Node<T> x, Node<T> y) {
Node<T> nx = FindSet(x);
Node<T> ny = FindSet(y);
if (nx == ny) {
return;
}
if (nx.rank > ny.rank) {
ny.parent = nx;
} else if (ny.rank > nx.rank) {
nx.parent = ny;
} else {
nx.parent = ny;
ny.rank++;
}
}
}

View File

@@ -0,0 +1,13 @@
package com.thealgorithms.datastructures.disjointsets;
public class Node<T> {
public int rank;
public Node<T> parent;
public T data;
public Node(T data) {
this.data = data;
parent = this;
}
}