replaceNode function

bool replaceNode(
  1. FNode root,
  2. FNode old,
  3. FNode replacement
)

Replaces old with replacement in the tree with root root. Returns false if old is not found.

Implementation

bool replaceNode(FNode root, FNode old, FNode replacement) {
  final children = _mutableChildrenOf(root);
  if (children != null) {
    final idx = children.indexWhere((c) => c.id == old.id);
    if (idx >= 0) {
      children[idx] = replacement;
      return true;
    }
  }
  for (final child in childrenOf(root)) {
    if (replaceNode(child, old, replacement)) return true;
  }
  return false;
}