union-find/contiguous

View on GitHub
src/adt/ForestAmortizedTwoPasses.js

Summary

Maintainability
A
0 mins
Test Coverage
import {rankedtreeunion, _RankedTreeUniverse} from '../fundamentals/index.js';

export const union = rankedtreeunion;

export const find = (p, node) => {
    let it = node;

    for (; it !== p[it]; it = p[it]);

    while (p[node] !== it) {
        const parent = p[node];
        p[node] = it;
        node = parent;
    }

    return it;
};

export const Universe = _RankedTreeUniverse(union, find);