Essential Data Structures and Algorithmic Templates

A classic strcuture for managing dynamic connectivity and equivalence classes. Initialization int parent[N]; void initUnionFind() { for (int i = 1; i <= n; ++i) { parent[i] = i; } } Path Compression Find int findRoot(int x) { return parent[x] == x ? x : parent[x] = findRoot(parent[x]); } Union by Root void unite(int a, ...

Posted on Wed, 12 Aug 2026 16:17:03 +0000 by dibyajyotig

FHQ Treap: A Non-Rotating Balanced Binary Tree Implementation

Data Structure DefinitionThe FHQ Treap (Fredman, Hendler, and Zhou Treap) relies on a randomized heap priority to maintain balance without requiring complex tree rotations. Each node in the structure maintains essential metadata: pointers to left and right children, the node's value, a random priority weight, and the size of the subtree rooted ...

Posted on Wed, 17 Jun 2026 17:45:38 +0000 by Backara_Drift

Self-Balancing Binary Search Tree Implementations

Self-Balancing Tree Structures Self-balancing binary search trees maintain logaritmhic height during insertions and deletions. This ensures efficietn search, insertion, and deletion operations. Below are implementations for three common variants: SBT, Treap, and Splay trees. Size Balanced Tree (SBT) #include <iostream> #include <cstdli ...

Posted on Sat, 06 Jun 2026 18:01:49 +0000 by Shaba1

Algorithm Solutions: Path Search, String Ranking, and Knapsack Problems

D - Path Traversal A straightforward depth-first search approach can solve this traversal problem. int nodes, edges, max_steps, min_cost, max_cost; vector<int> valid_endpoints; vector<pair<int, int>> graph[MAX_NODES]; void traverse(int current_node, int current_cost, int steps_taken) { if (current_cost > max_cost) retu ...

Posted on Fri, 29 May 2026 19:58:58 +0000 by volant

Implementing Balanced Trees: Rotational vs Non-rotational Treaps

P6136 Enhanced Balanced Tree Template After struggling with rotational Treaps, I've concluded they're overly complex. FHQ Treaps offer a much cleaner implementation, with roughly half the code size. Rotational Treap Implementation Structure and data definitions: const int INF=1e18; struct TreeNode { int left, right; int value, priority; ...

Posted on Sun, 10 May 2026 18:15:42 +0000 by spaceknop