setTree method

ImmutableTree<T> setTree(
  1. Path relativePath,
  2. ImmutableTree<T> newTree
)

A tree with newTree at relativePath.

Implementation

ImmutableTree<T> setTree(Path relativePath, ImmutableTree<T> newTree) {
  if (relativePath.isEmpty) {
    return newTree;
  }
  final ChildKey front = relativePath.front!;
  final ImmutableTree<T> child = children[front] ?? ImmutableTree<T>.empty();
  final ImmutableTree<T> newChild = child.setTree(
    relativePath.popFront(),
    newTree,
  );
  final SortedMap<ChildKey, ImmutableTree<T>> newChildren = newChild.isEmpty
      ? children.remove(front)
      : children.insert(front, newChild);
  return ImmutableTree<T>._(value, newChildren);
}