delete method

void delete({
  1. bool recursive = false,
})

Removes this node from the tree.

Moves every child in this.children to this.parent.children and removes this from this.parent.children.

Example:

/*
rootNode
  |-- childNode1
  │     |-- grandChildNode1
  │     '-- grandChildNode2
  '-- childNode2

childNode1.delete() is called, the tree becomes:

rootNode
  |-- childNode2
  |-- grandChildNode1
  '-- grandChildNode2
*/

Set recursive to true if you want to delete the entire subtree. (Ps: if the subtree is too large, this might take a while.)

If parent is null, this method has no effects.

Implementation

void delete({bool recursive = false}) {
  if (isRoot) return;

  if (recursive) {
    clearChildren().forEach((child) => child.delete(recursive: true));
  } else {
    _parent!.addChildren(clearChildren());
  }
  _parent!.removeChild(this);
}