deleteNode method

List<Node<T>> deleteNode(
  1. String key, {
  2. Node<T>? parent,
})

Deletes an existing node identified by specified key. This method returns a new list with the specified node removed.

Implementation

List<Node<T>> deleteNode(String key, {Node<T>? parent}) {
  List<Node<T>> _children = parent == null ? this.children : parent.children;
  List<Node<T>> _filteredChildren = [];
  Iterator iter = _children.iterator;
  while (iter.moveNext()) {
    Node<T> child = iter.current;
    if (child.key != key) {
      if (child.isParent) {
        _filteredChildren.add(child.copyWith(
          children: deleteNode(key, parent: child),
        ));
      } else {
        _filteredChildren.add(child);
      }
    }
  }
  return _filteredChildren;
}