Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bc0b4b2
[data-structures] Feature: Adding Heaps in Java
LucasGdosR May 30, 2023
32c38b8
[data-structures] Enhancement: adds Median Heap to README
LucasGdosR May 30, 2023
20c5b27
[data-structures] Enhancement: improves Median Heap on README
LucasGdosR May 30, 2023
741a7b4
[data-structures] Fix: typos in Binary Heap README
LucasGdosR May 30, 2023
7cd5057
[data-structures] Enhancement: adds MinHeap test.
LucasGdosR May 30, 2023
50a9188
[data-structures] Enhancement: adds MaxHeap test.
LucasGdosR May 30, 2023
aaff2f2
[data-structures] Enhancement: adds MedianHeap test.
LucasGdosR May 30, 2023
5a31c09
[data-structures] Enhancement: refactor + makefile
LucasGdosR Jun 4, 2023
0dfdd67
[data-structures] Enhancement: adds generic heap.
LucasGdosR Jun 6, 2023
8c0c60f
[data-structures] Feature: BinaryTree in Java.
LucasGdosR Jun 9, 2023
c1352f5
[data-structures] Feature: BinaryTree Makefile.
LucasGdosR Jun 9, 2023
00bd2f9
[data-structures] Feature: RedBlackTree in Java.
LucasGdosR Jun 9, 2023
ea0e5ba
[data-structures] Enhancement: LLRB tree makefile.
LucasGdosR Jun 9, 2023
d9715c7
Merge branch 'ppenna:main' into binary-tree-java
LucasGdosR Jun 18, 2023
9f8b46d
Merge branch 'ppenna:main' into main
LucasGdosR Jun 18, 2023
b35938b
Merge branch 'ppenna:main' into heaps-java
LucasGdosR Jun 18, 2023
d45694f
Merge pull request #2 from LucasGdosR/binary-tree-java
LucasGdosR Jun 18, 2023
fa0dbd3
Merge pull request #3 from LucasGdosR/heaps-java
LucasGdosR Jun 18, 2023
3206c1a
[data-structures] Enhancement: UnionFind in Java.
LucasGdosR Jun 23, 2023
e718354
[data-structures] Enhancement: UnionFind Java makefile
LucasGdosR Jun 23, 2023
d36fd8a
Delete data-structures/red-black-tree/java directory
LucasGdosR Jun 23, 2023
629fa11
[data-structures] Enhancement: optimize UnionFind path compression
LucasGdosR Jun 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions data-structures/disjoint-set/java/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
JFLAGS = -g
JC = javac

.SUFFIXES: .java .class

.java.class:
$(JC) $(JFLAGS) $*.java

CLASSES = UnionFind.java

default: classes

classes: $(CLASSES:.java=.class)

clean:
$(RM) *.class
85 changes: 85 additions & 0 deletions data-structures/disjoint-set/java/UnionFind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package union_find;

public class UnionFind {
// Array indicating the root of the component each element is connected to.
private int[] id;
// The index of the root of the component indicates the whole component's size.
private int[] size;
// Auxiliary array for path compression.
private int[] toCompress;

public UnionFind(int n) {
id = new int[n];
size = new int[n];
toCompress = new int[n];
// Initialize every element to be the root of its own component,
// and the size of each component to 1.
for (int i = 0; i < n; i++) {
id[i] = i;
size[i] = 1;
}
}

// Connects two components into a single one.
public void union(int p, int q) {
// Get the root of the components.
p = find(p);
q = find(q);

// If the root is the same, the elements are already connected.
if (p == q)
return;

// To keep the structure balanced with limited depth,
// add the smaller component as a branch of the larger component.
// Remember to increment the size of the resulting root.
if (size[p] > size[q]) {
// q's root is p, since p's component is bigger.
id[q] = p;
// p's component's size is incremented, since it now has all elements in the previous component.
size[p] += size[q];
} else {
id[p] = q;
size[q] += size[p];
}
}

// Finds the root of the component to which an element is connected.
public int find(int p) {
// Count the number of iterations.
int i = 0;

// While the element is not the root of the component:
while (p != id[p]) {
// Remember the element.
toCompress[i++] = p;
// Get the element's parent.
p = id[p];
}

// Now make all elements traversed point directly to the root.
// Next time "find" is called, it will only take 1 iteration of the loop to find the root.
for (int j = 0; j < i; j++)
id[j] = p;

// Return the root.
return p;
}

// Checks if two elements are connected by checking if the root of both is the same.
public boolean connected(int p, int q) {
return find(p) == find(q);
}

public static void main(String[] args) {
UnionFind uf = new UnionFind(10);

assert !uf.connected(0, 1);

uf.union(0, 1);

assert uf.connected(0, 1);

assert uf.find(0) == 1;
}
}