Data Structures Implemented in TheAlgorithms/Java: A Comprehensive Catalog
The TheAlgorithms/Java repository contains production-quality implementations of 40+ data structures ranging from fundamental stacks and queues to advanced self-balancing trees, graph algorithms, distributed systems primitives (CRDTs), and specialized caches, all organized under src/main/java/com/thealgorithms/datastructures/.
The TheAlgorithms/Java repository is one of the most comprehensive open-source collections of computer science algorithms and data structures written in idiomatic Java. Whether you are preparing for technical interviews or researching implementation patterns, this codebase offers fully documented, runnable examples of both classic and modern data structures. This guide catalogs every major implementation category with specific file paths and usage examples derived directly from the source.
Tree-Based Data Structures
The src/main/java/com/thealgorithms/datastructures/trees/ directory contains sophisticated tree implementations spanning basic search trees to spatial indexing structures.
Binary Search Trees
The repository provides two pedagogical approaches to BSTs in [BSTIterative.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/BSTIterative.java) and [BSTRecursive.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/BSTRecursive.java). Both implement standard operations—insert, search, and delete—with the same API but different traversal strategies.
Self-Balancing Trees
For O(log n) guaranteed performance, the repository includes:
- AVL Tree: [
AVLTree.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/AVLTree.java) maintains strict balance factors through single and double rotations after insertions and deletions. - Red-Black Tree: [
RedBlackBST.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java) implements the color-bit invariant balancing used in Java's ownTreeMap.
Advanced Tree Structures
Specialized trees for specific computational problems include:
- Segment Tree: [
SegmentTree.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java) for range queries (sum, min, max) in logarithmic time. - Fenwick Tree (Binary Indexed Tree): [
FenwickTree.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/FenwickTree.java) for efficient prefix-sum updates and queries. - Trie (Prefix Tree): [
Trie.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/Trie.java) for fast string prefix matching. - K-D Tree: [
KDTree.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/KDTree.java) for multi-dimensional range searches and nearest-neighbor queries. - Quad-Tree: [
QuadTree.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/QuadTree.java) for spatial indexing of 2D points. - Splay Tree: [
SplayTree.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java) with self-adjusting properties that move accessed nodes to the root. - Treap: [
Treap.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/Treap.java) combining binary search tree ordering with heap priority for randomized balancing.
AVL Tree Usage Example:
import com.thealgorithms.datastructures.trees.AVLTree;
public class AVLTreeDemo {
public static void main(String[] args) {
AVLTree tree = new AVLTree();
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(10);
tree.insert(25);
System.out.println("Contains 25? " + tree.search(25)); // true
tree.delete(20);
System.out.println("Balance factors: " + tree.returnBalance());
}
}
Graph Data Structures
The src/main/java/com/thealgorithms/datastructures/graphs/ package provides both graph representations and classic algorithms.
Graph Representations
- Undirected Adjacency List: [
UndirectedAdjacencyListGraph.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/UndirectedAdjacencyListGraph.java) stores graphs with efficient neighbor lookup using linked lists. - Graph Utilities: [
Graphs.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java) serves as a base class providing common traversal primitives.
Graph Algorithms
The repository implements canonical algorithms including:
- Shortest Path: Dijkstra's algorithm ([
DijkstraAlgorithm.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java)), A* search ([AStar.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/AStar.java)), and Bellman-Ford ([BellmanFord.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/BellmanFord.java)). - Minimum Spanning Tree: Prim's ([
PrimMST.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/PrimMST.java)) and Kruskal's ([Kruskal.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Kruskal.java)) algorithms for weighted undirected graphs. - Strongly Connected Components: Tarjan's algorithm ([
TarjansAlgorithm.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java)). - Maximum Flow: Ford-Fulkerson implementation ([
FordFulkerson.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/FordFulkerson.java)). - Graph Coloring: Welsh-Powell algorithm ([
WelshPowell.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java)).
Linear Data Structures
Stack Implementations
Located in src/main/java/com/thealgorithms/datastructures/stacks/, the repository provides the [Stack.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/Stack.java) generic interface with multiple backing implementations:
- Array-backed: [
StackArray.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/StackArray.java) using fixed-size primitive arrays. - Dynamic Array: [
StackArrayList.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/StackArrayList.java) utilizingArrayListfor automatic resizing. - Linked List: [
StackOfLinkedList.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/StackOfLinkedList.java) with node-based storage. - Reverse Stack: [
ReverseStack.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java) demonstrating bottom-popping semantics.
Stack Usage Example:
import com.thealgorithms.datastructures.stacks.Stack;
import com.thealgorithms.datastructures.stacks.StackArrayList;
public class StackDemo {
public static void main(String[] args) {
Stack<Integer> stack = new StackArrayList<>();
stack.push(5);
stack.push(10);
stack.push(15);
System.out.println("Top element: " + stack.peek()); // 15
System.out.println("Popped: " + stack.pop()); // 15
System.out.println("Size: " + stack.size()); // 2
}
}
Queue Implementations
The src/main/java/com/thealgorithms/datastructures/queues/ directory contains diverse queue variants:
- Generic Interface: [
Queue.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/Queue.java) defining the contract forenqueue,dequeue, andpeek. - Circular Queue: [
CircularQueue.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java) for fixed-size ring buffer behavior. - Deque: [
Deque.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/Deque.java) supporting insertion and deletion at both ends. - Linked Queue: [
LinkedQueue.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java) using node chains. - Two-Stack Queue: [
QueueByTwoStacks.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/QueueByTwoStacks.java) implementing FIFO using two LIFO stacks. - Priority Queue: [
PriorityQueues.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java) backed by a max-heap. - Sliding Window Maximum: [
SlidingWindowMaximum.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/SlidingWindowMaximum.java) for algorithmic interview problems. - Token Bucket: [
TokenBucket.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/TokenBucket.java) for rate-limiting scenarios.
Queue Usage Example:
import com.thealgorithms.datastructures.queues.QueueByTwoStacks;
public class QueueDemo {
public static void main(String[] args) {
QueueByTwoStacks<Integer> q = new QueueByTwoStacks<>();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
System.out.println(q.dequeue()); // 1
System.out.println(q.peek()); // 2
System.out.println(q.size()); // 2
}
}
Linked List Variants
The src/main/java/com/thealgorithms/datastructures/lists/ package provides:
- Singly Linked List: [
SinglyLinkedList.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java) - Doubly Linked List: [
DoublyLinkedList.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java) - Circular Doubly Linked List: [
CircularDoublyLinkedList.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/lists/CircularDoublyLinkedList.java) - Cursor Linked List: [
CursorLinkedList.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java) with movable cursor support. - Skip List: [
SkipList.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/lists/SkipList.java) for probabilistic O(log n) indexing. - Merge Operations: [
MergeSortedSinglyLinkedList.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedList.java) and [FlattenMultilevelLinkedList.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/lists/FlattenMultilevelLinkedList.java).
Advanced and Specialized Structures
Dynamic Arrays
The [DynamicArray.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java) class in src/main/java/com/thealgorithms/datastructures/dynamicarray/ implements a resizable array with automatic capacity doubling and iterator support.
import com.thealgorithms.datastructures.dynamicarray.DynamicArray;
public class DynamicArrayDemo {
public static void main(String[] args) {
DynamicArray<String> arr = new DynamicArray<>();
arr.add("Alice");
arr.add("Bob");
arr.put(5, "Eve"); // auto-expands
System.out.println(arr.get(5)); // Eve
arr.remove(0);
}
}
Disjoint-Set Union (Union-Find)
Located in src/main/java/com/thealgorithms/datastructures/disjointsetunion/, these structures manage partitioned sets with near-constant time union and find operations:
- Generic Implementation: [
DisjointSetUnion.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java) - Size-Optimized: [
DisjointSetUnionBySize.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnionBySize.java) using union-by-size heuristics.
Conflict-Free Replicated Data Types (CRDTs)
For distributed systems, the src/main/java/com/thealgorithms/datastructures/crdt/ package implements eventually consistent data structures:
- Two-P-Set: [
TwoPSet.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java) for add-only and remove-only sets. - PN-Counter: [
PNCounter.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java) (Positive-Negative Counter) for distributed counting. - OR-Set: [
ORSet.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java) (Observed-Remove Set) for set reconciliation. - LWW-Element-Set: [
LWWElementSet.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java) using last-writer-wins semantics.
Cache Implementations
The src/main/java/com/thealgorithms/datastructures/caches/ directory provides production-ready eviction policies:
- LRU Cache: [
LRUCache.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java) (Least Recently Used) with O(1) get/put. - LFU Cache: [
LFUCache.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java) (Least Frequently Used). - MRU Cache: [
MRUCache.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java) (Most Recently Used). - FIFO/LIFO: [
FIFOCache.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java) and [LIFOCache.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java).
LRU Cache Usage Example:
import com.thealgorithms.datastructures.caches.LRUCache;
public class LRUCacheDemo {
public static void main(String[] args) {
LRUCache<Integer, String> cache = new LRUCache<>(3);
cache.put(1, "One");
cache.put(2, "Two");
cache.put(3, "Three");
cache.get(2); // marks as recent
cache.put(4, "Four"); // evicts key 1
System.out.println(cache); // contains 2,3,4
}
}
Circular Buffers
The [CircularBuffer.java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java) class in src/main/java/com/thealgorithms/datastructures/buffers/ implements a fixed-size ring buffer for streaming data scenarios.
Summary
- TheAlgorithms/Java organizes 40+ data structure implementations under
src/main/java/com/thealgorithms/datastructures/with consistent package hierarchies for trees, graphs, lists, stacks, and queues. - Tree structures range from basic BSTs to advanced spatial indices like KD-Trees and Quad-Trees, including self-balancing AVL and Red-Black trees with rotation-based rebalancing.
- Graph support includes both adjacency list representations in
UndirectedAdjacencyListGraph.javaand comprehensive algorithm implementations including Dijkstra, A*, MST, and Max Flow. - Linear collections offer multiple backing strategies (array, linked list, two-stack) for stacks and queues, plus specialized variants like Deques, Priority Queues, and Skip Lists.
- Advanced structures cover Dynamic Arrays with iterator support, Union-Find with path compression, distributed systems primitives (CRDTs), and cache eviction policies (LRU, LFU, MRU).
Frequently Asked Questions
What tree data structures are available in TheAlgorithms/Java?
The repository implements ten distinct tree structures in src/main/java/com/thealgorithms/datastructures/trees/, including Binary Search Trees (iterative and recursive), self-balancing AVL and Red-Black trees, Segment Trees for range queries, Fenwick Trees for prefix sums, Tries for string matching, KD-Trees for spatial data, Quad-Trees for 2D indexing, Splay Trees with self-adjusting properties, and randomized Treaps.
Does TheAlgorithms/Java include production-ready cache implementations?
Yes, the src/main/java/com/thealgorithms/datastructures/caches/ package contains fully functional cache implementations with O(1) operations, including LRU (Least Recently Used), LFU (Least Frequently Used), MRU (Most Recently Used), FIFO, and LIFO eviction policies, suitable for understanding cache semantics or adapting into production systems.
What distributed systems data structures are implemented?
The repository includes Conflict-Free Replicated Data Types (CRDTs) in src/main/java/com/thealgorithms/datastructures/crdt/, specifically Two-P-Sets, PN-Counters for distributed counting, OR-Sets for observed-remove semantics, and LWW-Element-Sets using last-writer-wins resolution, enabling eventually consistent distributed state management.
How are graph algorithms organized in the repository?
Graph algorithms are separated from representations in src/main/java/com/thealgorithms/datastructures/graphs/, with UndirectedAdjacencyListGraph.java providing the base structure, while standalone classes implement Dijkstra's algorithm, A* search, Bellman-Ford, Prim's and Kruskal's MST algorithms, Tarjan's SCC detection, and Ford-Fulkerson maximum flow.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →