question-mark
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);
this.tree.treeModel.update();

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

7reactions
dlemoingcommented, Jan 31, 2018

You can use something like this:

deleteNode(node: TreeNode) : void {
    if (node.parent != null) {
        node.parent.data.children.splice(node.parent.data.children.indexOf(node.data), 1)
        this.tree.treeModel.update()
    }
}
6reactions
AbhiThakarecommented, Sep 21, 2017

@sulhome

You can try as below

deleteNode(node, tree){
  let parentNode = node.realParent ? node.realParent : node.treeModel.virtualRoot;
        _.remove(parentNode.data.children, function (child) {
            return child === node.data;
        });
        tree.treeModel.update();
        if (node.parent.data.children.length === 0) {
            node.parent.data.hasChildren = false;
        }
  }

and call deleteNode function from your HTML on node.

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

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: https://www.youtube.com/playlist?list=PL1w8k37X_6L-E23tn3d6oXLW63pS8-5rmBinary 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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found