find method

T? find(
  1. T element
)

Finds the representative (root) of the set containing the given element

Implements path compression for optimal performance. Returns null if the element doesn't exist.

Implementation

T? find(T element) {
  final node = _nodes[element];
  if (node == null) return null;

  // Path compression: make all nodes on the path point directly to the root
  if (node.parent != null) {
    node.parent = _nodes[_findRoot(node)]!;
  }

  return _findRoot(node);
}