remove method

bool remove(
  1. String path
)

Removes the value stored at path. Returns true if a value existed.

Implementation

bool remove(String path) {
  final segments = _segments(path);
  var node = _root;
  final stack = <(String, _TrieNode<T>)>[];
  for (final seg in segments) {
    stack.add((seg, node));
    final child = node.children[seg];
    if (child == null) return false;
    node = child;
  }
  if (!node.hasValue) return false;
  node
    ..value = null
    ..hasValue = false;

  // Prune empty leaf nodes.
  for (var i = stack.length - 1; i >= 0; i--) {
    final (seg, parent) = stack[i];
    final child = parent.children[seg]!;
    if (!child.hasValue && child.children.isEmpty) {
      parent.children.remove(seg);
    } else {
      break;
    }
  }
  return true;
}