Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[HOW TO] Delete a node from a tree

See original GitHub issue

I want to delete a node from a tree. Given nodeToDelete, this method doesn’t work

The splice method doesn’t work, as soon as you call update() the item well be back

const itemIndex = nodeToDelete.index;
nodeToDelete.parent.children.splice(itemIndex, 1); // item disappear
this.tree.treeModel.update(); // item comes back again

If I use the array that I am binding to the tree then I can delete elements from the root as such

const itemIndex = nodeToDelete.index;
this.nodes.splice(itemIndex, 1);

What if the node I want to delete is not in the root of the nodes array i.e. it is a child of another node?

The first option clearly much better but it doesn’t work, the delete is not persisted in the tree.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

dlemoingcommented, Jan 31, 2018

You can use something like this:

deleteNode(node: TreeNode) : void {
    if (node.parent != null) {, 1)
AbhiThakarecommented, Sep 21, 2017


You can try as below

deleteNode(node, tree){
  let parentNode = node.realParent ? node.realParent : node.treeModel.virtualRoot;
        _.remove(, function (child) {
            return child ===;
        if ( === 0) {
   = false;

and call deleteNode function from your HTML on node.

<tree-root   #tree
      <ng-template #treeNodeTemplate let-node>
        <span title="{{}}">{{ }}</span>
        <span class="pull-right">{{ childrenCount(node) }}</span>
        <button (click)="deleteNode(node, tree)">-</button>
      <ng-template #loadingTemplate>Loading, please hold....</ng-template>

Hope this will help

Pleas let me know output.

Thanks Abhi.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deletion in Binary Search Tree - GeeksforGeeks
1) Node to be deleted is the leaf: Simply remove it from the tree. 50 50 · 2) Node to be deleted has...
Read more >
Deletion from BST (Binary Search Tree) - Techie Delight
Case 2: Deleting a node with two children: call the node to be deleted N . Do not delete N . Instead, choose...
Read more >
Delete a node from Binary Search Tree - YouTube
Key moments. View all · delete a non leaf node · delete a non leaf node · delete a non leaf node ·...
Read more >
Deletion in Binary Tree | Delete a node in Single Traversal
Complete Playlist on Trees Data Structures: Tree Deleting a ...
Read more >
How to Delete a Node from a Binary Search Tree - YouTube
In this video I walk through how to delete nodes from a binary search tree. Specifically I explain how to delete : the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found