deleteNode<T> method

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

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

Implementation

List<Node> deleteNode<T>(String key, {Node? parent}) {
  List<Node> _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<T>(key, parent: child),
        ));
      } else {
        _filteredChildren.add(child);
      }
    }
  }
  return _filteredChildren;
}