remove method

ImmutableTree<T> remove(
  1. Path relativePath
)

A tree without the value and subtree at relativePath.

Implementation

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